場景:一個彈出層,包含一個Tableview,每一行為一個選擇條件,且只能選擇一個。選中後文體有顏色變化,後面還會有對勾。選擇另一個後,前一個恢復成普通狀態。
示例代碼:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}注意: lastIndexPath為私有變量,頁面第一次加載為nil,需追加判斷。