我在 UITableView 中有自定义 UITableViewCell 单元格。当用户选择了一个单元格并且该单元格被滚动出屏幕,然后又滚动回到屏幕中时,选定的单元格是空白的。
我知道在重复使用单元格时我需要设置单元格中的所有值,但我无法弄清楚这一点。
Iamge 1 代表用户选择单元格:
图 2 表示单元格在屏幕上滚动并返回屏幕时
(忽略图像模糊的事实,只是保护我的客户数据)
注意:我希望能够选择表格,不建议取消高亮单元格的能力。我实际上希望在将选定的单元格滚动回屏幕时能够看到蓝色单元格。
任何帮助将不胜感激。
编辑:添加一些代码:
// Define a property to keep track of the cell that was selected last using the NSIndexPath
@property (nonatomic, strong) NSIndexPath *lastSelectedIndexPath;
///////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Keeps track of the lastest selected entry
self.lastSelectedIndexPath = indexPath;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
IPCSearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
// Cast the cell to the custom cell
IPCSearchResultTableViewCell *searchResultCell = (IPCSearchResultTableViewCell *)cell;
// RepositoryIndex is a coredata object with information
MyCoreDataEntity *entity = [self.fetchedResultsController objectAtIndexPath:indexPath];
searchResultCell.label1.text = entity.attribute1;
searchResultCell.label2.text = entity.attribute2;
searchResultCell.label3.text = entity.attribute3;
searchResultCell.label4.text = entity.attribute4;
}
*注意:上面的代码现在似乎可以工作了。我不再遇到这个问题,也不知道为什么*