1

I am trying to create a UIPickerView inside UITableViewCell, and it works fine. I have set the tag for UIProgressView as indexPath.row. Currently i have 3 rows i am using this UIProgressView when downloading a file using NSURLConnection

When NSURLConnection calls the connection:didReceiveData: method, i try to update the progress by matching the indexPath.row, But, i am not sure why the last row only gets updated

Please let me know

- (void)download:(NSString*)rowId
{
    NSURL *url;

    if ([rowId intValue] == 0 ){
        url = [NSURL URLWithString:@"http://192.168.0.29/iphone/video/video_10.mov"];
    }
    else if ([rowId intValue]  == 1){
        url = [NSURL URLWithString:@"http://192.168.0.29/iphone/video/video_10.mov"];
    }
    else if ([rowId intValue]  == 2){
        url = [NSURL URLWithString:@"http://192.168.0.29/iphone/video/video_10.mov"];
    }



    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    connection = nil;

}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)recievedData {
    if (data==nil) {
        data = [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:recievedData];
    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[data length]];
    float progress = [resourceLength floatValue] / [self.filesize floatValue];
    NSLog(@"%d inside ",myId);
    UIProgressView* downPreView = (UIProgressView*)[self.view viewWithTag:myId];
    downPreView.progress = progress;

 }


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
//-------------------------------------------------------------------------------
{
    static NSString *CellIdentifier = @"DownloadingCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    self.progressView = [[[UIProgressView alloc]initWithFrame:CGRectMake(80,70,170,15)]autorelease];
    [self.progressView setProgressViewStyle:UIProgressViewStyleBar];
    self.progressView.tag = indexPath.row;
    self.progressView.progress = 0.0f;
    myId = indexPath.row;
    NSString *aid = [NSString stringWithFormat:@"%d",myId];

    NSLog(@"%@",aid);






    [cell.contentView addSubview:self.progressView];
    [self performSelectorOnMainThread:@selector(download:) withObject:aid waitUntilDone:NO];


    if (indexPath.row == 0)
    {
        cell.textLabel.text = @"Beautiful Dreamer";
        cell.detailTextLabel.text = @"David Lehman";
        cell.imageView.image = [UIImage imageNamed:@"download_poem_icon.png"];

    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = @"Friends a Friendship";
        cell.detailTextLabel.text = @"Denise Levertov";

        cell.imageView.image = [UIImage imageNamed:@"download_poem_icon.png"];

    }
    else {
        cell.textLabel.text = @"Love and Friendship";
        cell.detailTextLabel.text = @"Dorianne Laux";
        cell.imageView.image = [UIImage imageNamed:@"download_poem_icon.png"];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}
HelmiB
  • 12,303
  • 5
  • 41
  • 68
user198725878
  • 6,266
  • 18
  • 77
  • 135
  • I think The problem is: u have written : [self.view viewwithTag:] instruction. – Loquatious Mar 03 '12 at 11:05
  • But you should get the progressbar object accrding to the cell by: [cell.contentView viewWithTag:]. And also u should access the cell also. – Loquatious Mar 03 '12 at 11:07
  • @Simha.hb: thanks for the reply...actually when i print myId value it always shows as 2( so the last row)why is that showing 2 always?actually it has to be first 0,1,and then 2.pls let me know – user198725878 Mar 03 '12 at 11:09
  • Why you initialized always myId = 1; in cellForRowAtIndexPath method? – Loquatious Mar 03 '12 at 11:14
  • sorry that was a typo . fixed now – user198725878 Mar 03 '12 at 11:15
  • You can ask me in our chat room if you have any query at: http://chat.stackoverflow.com/rooms/8469/room-for-casual-chat – Loquatious Mar 03 '12 at 11:17
  • You should put `addSubview:` and all allocation object inside `cell==nil` statement. otherwise, you have a big leak there. plus you are not releasing `UIProgressView` – HelmiB Mar 03 '12 at 11:38

1 Answers1

2

Try to create custom class like below:

@protocol CustomConnectionDelegate

- (void)connection:(CustomConnection *)theConnection didReceiveData:(NSData *)recievedData;

@end
@interface CustomConnection:NSURLConnection
{
    int myId;
    id<CustomConnectionDelegate>myDelegate;
}
@property(nonatomic, readwrite) int myId;
@property(nonatomic, retain) id<CustomConnectionDelegate>myDelegate;

.

@implementation CustomConnection
@synthesize myDelegate, myId;

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate WithRowId:(int)rowId
{
    self.myDelegate = delegate;
    myId = rowId;
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)recievedData{
    [self.delegate connection:self didReceiveData:recievedData];
}
Nag_iphone
  • 967
  • 7
  • 22
Loquatious
  • 1,791
  • 12
  • 21