//前提:在UITableView中,分4个section,每个section中只有一行(自行脑补界面)
//在UITableView中,单击记录cell0后,改变cell0原来的状态;点击cell1后,cell0恢复原来的状态,cell1的状太改变。
//其中的NSIndexPath *lastIndexPath为实例变量
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int newSection = (int)[indexPath section];//点击某一行后,newSectioon记录下点击某一section
int oldSection = (lastIndexPath != nil) ? (int)[lastIndexPath section] : -1;//当第一次点击section时,lastIndexPath = nil,oldSection = -1
if (newSection != oldSection){
CorrectiveFeedbackCell *newCell = [tableView cellForRowAtIndexPath:indexPath];//自定义的CorrectiveFeedbackCell,可以根据实际情况定义:newCell为点击的newSection的那一行
newCell.mainImageView.image = [UIImage imageNamed:@"pay_success_small"];//改变newCell的状态
CorrectiveFeedbackCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];//第一次点击时没有创建oldCell;
oldCell.mainImageView.image = [UIImage imageNamed:@""];//
lastIndexPath = [indexPath copy];//将点击的某行,拷贝到lastIndexPath
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadData];
}
后记:感觉这个方法是直接改cell的状态,好像不是很符合MVC设计模式,当然这个方法比较适合写静态又比较小改变的TablView(本人是小白,笔记中若有错误,欢迎指出,大家一起探讨探讨)