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:
-(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 ......];
}
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)
You can do most of your processing in the background but anything that updates the UI has to happen on the main thread.
Doing background processing and then updating the UI can be done easily using Grand Central Dispatch (GCD).
However. In a table view there’s a couple of gotchas.
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.
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.
// In the UITableViewDelegate.