IOS如何更新UITableViewCell [英] IOS how to update UITableViewCell

查看:79
本文介绍了IOS如何更新UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作一个自定义UITableViewCell:

i make a custom 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;

typeTapped method:

typeTapped method:

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

所以如何在其他线程中更新UITableViewCell。(如果我调用reloadDate它将停止停止)(我可以滚动tableview来更新它)

so how can i update UITableViewCell in other thread.(if i call reloadDate it is will drop-dead halt)(i can scroll tableview to update it)

推荐答案

你可以完成大部分的处理工作后台,但更新UI的任何内容都必须在主线程上发生。

You can do most of your processing in the background but anything that updates the UI has to happen on the main thread.

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

Doing background processing and then updating the UI can be done easily using Grand Central Dispatch (GCD).

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

但是。在表格视图中有几个问题。

However. In a table view there's a couple of gotchas.

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

Table cells are re-used. This mean that your nice UIButton in your UITableViewCell may not be the one you think it is once the download has completed. It's the same object, but has been reconfigured to display the content from another row.

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

For these case your best bet is to use GCD to update the NSArray or other data structure is feeding from and then ask the table view to do the refresh for the cell.

//在UITableViewDelegate中。

// In the 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];
        });
    });

}

这篇关于IOS如何更新UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆