2012-03-03 29 views
1

我想在UITableViewCell內部創建一個UIPickerView,它工作正常。 我已將UIProgressView的標記設置爲indexPath.row。目前,我有3行 下載使用NSURLConnection問題,只有最後一行得到更新

NSURLConnection調用connection:didReceiveData:方法的文件時,我用這UIProgressView,我嘗試通過匹配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; 
} 
+0

我覺得問題是:你寫了:[self.view viewwithTag:]指令。 – 2012-03-03 11:05:39

+0

但是你應該通過[cell.contentView viewWithTag:]獲取accrding到單元格的進度條對象。而且你也應該訪問這個單元格。 – 2012-03-03 11:07:00

+0

@ Simha.hb:謝謝你的回覆......實際上,當我打印myId值時,它始終顯示爲2(所以最後一行)爲什麼總是顯示2?實際上它必須先是0,1,然後是2 .pls讓我知道 – user198725878 2012-03-03 11:09:47

回答

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]; 
} 
相關問題