ios – reloadRowsAtIndexPaths时保持偏移量

栏目: IOS · 发布时间: 7年前

内容简介:在reloadRowsAtIndexPaths:withRowAnimation:之后,tableView使用tableView:estimatedHeightForRowAtIndexPath:中给出的估计高度确定其偏移量.因此,除非您返回的值是准确的,否则实现它将导致tableView的偏移量在重新加载后发生变化.我没有实现tableView:estimatedHeightForRowAtIndexPath:问题得到解决.翻译自:https://stackoverflow.com/questions/2

我正在尝试重新加载一个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];
}

这就是我在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];

但它只是跳,任何建议?

P.S

我跟着这个帖子,但它没有解决我的问题 Calling reloadRowsAtIndexPaths removes tableView contentOffset

在reloadRowsAtIndexPaths:withRowAnimation:之后,tableView使用tableView:estimatedHeightForRowAtIndexPath:中给出的估计高度确定其偏移量.因此,除非您返回的值是准确的,否则实现它将导致tableView的偏移量在重新加载后发生变化.我没有实现tableView:estimatedHeightForRowAtIndexPath:问题得到解决.

翻译自:https://stackoverflow.com/questions/27102887/maintain-offset-when-reloadrowsatindexpaths


以上所述就是小编给大家介绍的《ios – reloadRowsAtIndexPaths时保持偏移量》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Rework

Rework

Jason Fried、David Heinemeier Hansson / Crown Business / 2010-3-9 / USD 22.00

"Jason Fried and David Hansson follow their own advice in REWORK, laying bare the surprising philosophies at the core of 37signals' success and inspiring us to put them into practice. There's no jarg......一起来看看 《Rework》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码