Ios 从服务器获取数据后在后台更新正确的UITableViewCell
我的UITableViewCells有一个UIButton作为其附件视图。在Ios 从服务器获取数据后在后台更新正确的UITableViewCell,ios,Ios,我的UITableViewCells有一个UIButton作为其附件视图。在cellforrowatinexpath中,我使用块从后台服务器获取数据。调用块时,我根据检索到的数据设置单元格中按钮的selected属性 当用户快速上下滚动TableView时,某些按钮状态有时设置不正确。我认为这是因为块内使用了错误的UITableViewCell(它是一个重用的单元格,而不是实际的单元格,现在可能在屏幕外) 如何确保我所在区块内的单元格是我想要的单元格 下面是我的代码的简化版本,其中包含以下重要部
cellforrowatinexpath
中,我使用块从后台服务器获取数据。调用块时,我根据检索到的数据设置单元格中按钮的selected
属性
当用户快速上下滚动TableView时,某些按钮状态有时设置不正确。我认为这是因为块内使用了错误的UITableViewCell(它是一个重用的单元格,而不是实际的单元格,现在可能在屏幕外)
如何确保我所在区块内的单元格是我想要的单元格
下面是我的代码的简化版本,其中包含以下重要部分:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//FoundUserCell is a subclass of UITableViewCell
FoundUserCell *cell = [tableView dequeueReusableCellWithIdentifier:[self cellIdentifierTableView:tableView]];
//set the textLabel (this works already)
cell.textLabel = @"something";
//fetch info from the DB that I need to state the state of the button
//theButton is a property of FoundUserCell
//theButton is always set to a FoundUserCell's accessoryView
[self fetchInBackgroundWithBlock:^(int buttonState) {
//I think that, at this point, cell is sometimes no longer the cell I want
if(buttonState == 0)
cell.theButton.selected = NO;
else
cell.theButton.selected = YES;
//WHAT CAN I DO INSIDE THIS BLOCK TO MAKE SURE THAT THE CELL I'M SETTING IS THE CELL I WANT?
}];
return cell;
}
基本上,如果单元格不是正确的单元格,我不想在块内设置按钮的状态。是的,单元格将被重新使用(更改),因此您必须在块中获得正确的单元格
[self fetchInBackgroundWithBlock:^(int buttonState) {
UITableViewCell *rightCell = [tableView cellForRowAtIndexPath:indexPath];
// code with rightCell
}];