问题,只有最后一行得到更新

时间:2012-03-03 10:49:50

标签: iphone uitableview ios4 uiprogressview

我正在尝试在UIPickerView内创建一个UITableViewCell,它运行正常。 我已将UIProgressView的标记设为indexPath.row。目前我有3行 我在使用UIProgressView

下载文件时使用此NSURLConnection

NSURLConnection调用connection:didReceiveData:方法时,我会尝试通过匹配indexPath.row来更新进度,但是,我不确定为什么最后一行只会更新

请告诉我

- (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;
}

1 个答案:

答案 0 :(得分:2)

尝试创建如下所示的自定义类:

@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];
}