reloadRowsAtIndexPaths 副作用?
reloadRowsAtIndexPaths side effects?
我有一个 UITableView,里面装满了包含复选标记的 UITableViewCell。
当我点击一个单元格时,如果它被标记,它就会变成未标记并且 vice-versa。
到目前为止一切顺利。现在我想在每次单击任何其他单元格时更新第一个单元格(假设第一个单元格是特殊的)。我修改了 didSelectRowAtIndexPath 的代码(见下文)并且它正常工作,除了我在我单击的单元格上出现以下不良行为:
如您所见,单元格顶部的白线已经消失...
这是我的代码(我猜)关键函数的样子:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [myLabels objectAtIndex:indexPath.row];
bool isChecked = false;
if ([currentValues[indexPath.row] boolValue]) {
isChecked = true;
}
cell.accessoryType = isChecked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
bool isChecked = false;
if ([currentValues[indexPath.row] boolValue]) {
isChecked = false;
currentValues[indexPath.row] = [NSInteger numberWithBool:false];
// below is the part of code that causes trouble
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// end of problematic code
}
else {
isChecked = true;
currentValues[indexPath.row] = [NSInteger numberWithBool:true];
// similarly...
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
cell.accessoryType = isChecked ? UITableViewCellAccessoryCheckmark :UITableViewCellAccessoryNone;
}
NB 如果我重新加载整个table,那么我就没有这个问题了。但我觉得了解那里发生的事情对我来说可能是值得的。
NB2 我试过调用和不调用 [self.tableView beginUpdates] 和 [self.tableView endUpdates] 但它似乎没有差异。
你 didSelectRowAtIndexPath
会稍微改变一下,比如,
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
bool isChecked = false;
if ([currentValues[indexPath.row] boolValue]) {
isChecked = false;
currentValues[indexPath.row] = [NSInteger numberWithBool:false];
// below is the part of code that causes trouble
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
//MYCHANGES
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// end of problematic code
}
else {
isChecked = true;
currentValues[indexPath.row] = [NSInteger numberWithBool:true];
// similarly...
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
//MYCHANGES
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
cell.accessoryType = isChecked ? UITableViewCellAccessoryCheckmark :UITableViewCellAccessoryNone;
}
我有一个 UITableView,里面装满了包含复选标记的 UITableViewCell。
当我点击一个单元格时,如果它被标记,它就会变成未标记并且 vice-versa。
到目前为止一切顺利。现在我想在每次单击任何其他单元格时更新第一个单元格(假设第一个单元格是特殊的)。我修改了 didSelectRowAtIndexPath 的代码(见下文)并且它正常工作,除了我在我单击的单元格上出现以下不良行为:
如您所见,单元格顶部的白线已经消失...
这是我的代码(我猜)关键函数的样子:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [myLabels objectAtIndex:indexPath.row];
bool isChecked = false;
if ([currentValues[indexPath.row] boolValue]) {
isChecked = true;
}
cell.accessoryType = isChecked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
bool isChecked = false;
if ([currentValues[indexPath.row] boolValue]) {
isChecked = false;
currentValues[indexPath.row] = [NSInteger numberWithBool:false];
// below is the part of code that causes trouble
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// end of problematic code
}
else {
isChecked = true;
currentValues[indexPath.row] = [NSInteger numberWithBool:true];
// similarly...
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
cell.accessoryType = isChecked ? UITableViewCellAccessoryCheckmark :UITableViewCellAccessoryNone;
}
NB 如果我重新加载整个table,那么我就没有这个问题了。但我觉得了解那里发生的事情对我来说可能是值得的。
NB2 我试过调用和不调用 [self.tableView beginUpdates] 和 [self.tableView endUpdates] 但它似乎没有差异。
你 didSelectRowAtIndexPath
会稍微改变一下,比如,
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
bool isChecked = false;
if ([currentValues[indexPath.row] boolValue]) {
isChecked = false;
currentValues[indexPath.row] = [NSInteger numberWithBool:false];
// below is the part of code that causes trouble
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
//MYCHANGES
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
// end of problematic code
}
else {
isChecked = true;
currentValues[indexPath.row] = [NSInteger numberWithBool:true];
// similarly...
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
//MYCHANGES
[self.tableView beginUpdates];
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
cell.accessoryType = isChecked ? UITableViewCellAccessoryCheckmark :UITableViewCellAccessoryNone;
}