ios – reloadRowsAtIndexPaths时保持偏移量

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

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

查看所有标签

猜你喜欢:

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

C程序设计语言

C程序设计语言

Brian W. Kernighan、Dennis M. Ritchie / 机械工业出版社 / 2006-8-1 / 35.00元

在计算机发展的历史上,没有哪一种程序设计语言像C语言这样应用广泛。本书是C语言的设计者之一Dennis M.Ritchie和著名计算机科学家Brian W.Kernighan合著的一本介绍C语言的权威经典著作。我们现在见到的大量论述C语言程序设计的教材和专著均以此书为蓝本。本书第1版中介绍的C语言成为后来广泛使用的C语言版本——标准C的基础。人们熟知的“hello,World"程序就是由本书首次引......一起来看看 《C程序设计语言》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

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

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具