ios – reloadRowsAtIndexPaths时保持偏移量

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

内容简介:在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时保持偏移量》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Learning JavaScript

Learning JavaScript

Shelley Powers / Oreilly & Associates Inc / 2006-10-17 / $29.99

As web browsers have become more capable and standards compliant, JavaScript has grown in prominence. JavaScript lets designers add sparkle and life to web pages, while more complex JavaScript has led......一起来看看 《Learning JavaScript》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具