mte*_*t88 7 reload uitableview ios
我正在尝试重新加载一个tableViewCell,但每次我都滚动到顶部...我不是在添加或删除单元格,我只想更改所选单元格的颜色.
这是我在cellForRowAtIndexPath中所做的:
SMPChoiceViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChoiceCell" forIndexPath:indexPath];
SMPChoice *choice = self.choices[indexPath.row - 1];
cell.choiceTextLabel.text = choice.text;
if ([self.selectedChoices indexOfObject:choice] != NSNotFound) {
cell.choiceTextLabel.textColor = [UIColor purpleColor];
} else {
cell.choiceTextLabel.textColor = [UIColor blackColor];
}
Run Code Online (Sandbox Code Playgroud)
这就是我在didSelectRowAtIndexPath中所做的
if ([self.selectedChoices indexOfObject:choice] != NSNotFound) {
[self.selectedChoices removeObject:choice];
} else {
[self.selectedChoices addObject:choice];
}
CGPoint offSet = [tableView contentOffset];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView setContentOffset:offSet animated:NO];
Run Code Online (Sandbox Code Playgroud)
但它只是跳,任何建议?
PS我跟着这个线程,但它没有解决我的问题调用reloadRowsAtIndexPaths删除tableView contentOffset
Dan*_*aug 16
因为某些神秘的原因,表视图在使用估计的行高重新加载某些单元格后确定新的偏移量,您希望确保tableView:estimatedHeightForRowAtIndexPath为已经渲染的单元格返回正确的数据.要实现此目的,您可以在字典中缓存看到的行高,然后使用这个正确的数据(或者对尚未加载的单元格的估计).
fileprivate var heightForIndexPath = [NSIndexPath: CGFloat]()
fileprivate let averageRowHeight: CGFloat = 300 //your best estimate
//UITableViewDelegate
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
heightForIndexPath[indexPath] = cell.frame.height
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return heightForIndexPath[indexPath] ?? averageRowHeight
}
Run Code Online (Sandbox Code Playgroud)
(非常感谢eyuelt的洞察力,估计行高用于确定新的偏移量.)
eyu*_*elt 10
我知道这是一个老问题,但我有同样的问题,无法在任何地方找到答案.
之后reloadRowsAtIndexPaths:withRowAnimation:,tableView使用给定的估计高度确定其偏移量tableView:estimatedHeightForRowAtIndexPath:.因此,除非您返回的值是准确的,否则实现它将导致在重新加载后tableView的偏移量发生变化.我没有实现tableView:estimatedHeightForRowAtIndexPath:,问题得到解决.
| 归档时间: |
|
| 查看次数: |
3346 次 |
| 最近记录: |