IOS如何更新UITableViewCell

zt9*_*788 2 drawing uibutton uitableview ios

我做一个自定义UITableViewCell:

 SListViewCell * cell = nil;
//    cell = [listView dequeueReusableCellWithIdentifier:CELL];
if (!cell) {
    cell = [[[SListViewCell alloc] initWithReuseIdentifier:CELL] autorelease];
}
listView.separatorColor = [UIColor clearColor];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = index;
[btn setFrame:CGRectMake(0, 0, 42, 42)];
//setBtnImage will download image from network. when the UIButton click it is will change button image 
[self setBtnImage:index btn:btn];
[btn addTarget:self action:@selector(typeTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
return  cell;
Run Code Online (Sandbox Code Playgroud)

typeTapped方法:

-(void)typeTapped:(id)sender
{
   UIButton* btn = (UIButton)sender;
   //download image from network in ohter thread and change button' image.
   NSThread * thread = [Thread alloc] initWith:@selecter(downloadImage:)....
   [thread start];
   [thread release];
}
//download image 
-(void)downloadImage:(UIButton*)btn....
{
  //download
  UIImage* image= ....
  //UITableView is not update
  //it is will update when i scroll tableview.
  [btn setImage:image ......];
}
Run Code Online (Sandbox Code Playgroud)

所以我怎么能在其他线程中更新UITableViewCell.(如果我调用reloadDate它会停止死机)(我可以滚动tableview来更新它)

Pie*_*ard 6

您可以在后台完成大部分处理,但更新UI的任何内容都必须在主线程上进行.

使用Grand Central Dispatch(GCD)可以轻松完成后台处理,然后更新UI.

- (void)typeTapped:(id)sender
{
    UIButton* btn = (UIButton*)sender;
    // Use Grand Central Dispatch to do the processing in the background.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        // Do download operation here... Something like:
        NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://some_url.com/some.image"]];
        UIImage* image = [UIImage imageWithData:imageData];

        //Go back to the main thread to update the UI. this is very important!
        dispatch_async(dispatch_get_main_queue(), ^{
            [btn setImage:image forState:UIControlStateNormal];
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

然而.在表格视图中有几个陷阱.

表格细胞被重复使用.这意味着您的UITableViewCell中的漂亮UIButton可能不是您认为下载完成后的UIButton.它是同一个对象,但已重新配置为显示另一行的内容.

对于这些情况,最好的办法是使用GCD更新NSArray或其他数据结构,然后请求表视图对单元格进行刷新.

//在UITableViewDelegate中.

@implementation SomeViewController
{
  NSMutableArray* _tableData;
}

// ...

// Someone tapped on the cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        // Do download operation here... Something like:
        NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://some_url.com/some.image"]];
        UIImage* image = [UIImage imageWithData:imageData];
        if([_tableData count] < indexPath.row) {
            _tableData[indexPath.row] = image;
        }
        //Go back to the main thread to update the UI. this is very important!
        dispatch_async(dispatch_get_main_queue(), ^{
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        });
    });

}
Run Code Online (Sandbox Code Playgroud)