【发布时间】:2015-09-26 16:17:36
【问题描述】:
我用 27 个静态 UITableViewCells 制作了一个大型 UITableView。选择UITableViewCell 时,应将其accessoryType 设置为UITableViewAccessoryCheckmark,并将最后选择的UITableViewCell 的accessoryType 设置为UITableViewAccessoryNone:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// get an NSInteger from NSUserDefaults
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// get the NSInteger again
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:savedInteger inSection:0]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
lastCell.accessoryType = UITableViewAccessoryNone;
cell.accessoryType = UITableViewAccessoryCheckmark;
// save indexPath.row in NSUserDefaults and do some other stuff
当您选择一个靠近最后选择的UITableViewCell 的UITableViewCell 但当最后选择的UITableViewCell 距离大约 11 行左右时,这工作正常,UITableViewCell *lastCell 是nil 和复选标记不会消失。
为什么会这样,我该如何解决?提前致谢。
【问题讨论】:
-
不要显式调用
didSelectRowAtIndexPath。 -
你应该在
cellForRowAtIndexPath中设置一个单元格的accessoryType。 -
在您的模型对象中有 1 个 bool,您可以从该 bool 处理附件视图。如果 bool 为真,则显示它,否则不要..
标签: ios objective-c uitableview accessorytype