在写切换城市页面时,遇到了一个很诡异的bug。
每次进入该页面,应在已选择的城市对应的cell上勾选,其他的应为非勾选状态,于是这样的写了代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"city" forIndexPath:indexPath];
CityAndAreaInfo* info = [cityArray objectAtIndex:indexPath.row];
cell.textLabel.text = info.cityName;
if (!_selectedCity || [_selectedCity.cityName isEqualToString:@""]) {
if (indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
_selectedCity = info;
}
}else {
if ([info.cityName isEqualToString:_selectedCity.cityName]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
return cell;
}
但结果有点纠结,每次都有一两没有选中的城市勾上了,而且调试过程中也没有发现问题。
思来想去,觉得这只能是UITableViewCell的初始状态不一定是None,也可能是CheckMark,于是改成这样子就可以了。
if ([info.cityName isEqualToString:_selectedCity.cityName]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else
cell.accessoryType = UITableViewCellAccessoryNone;
抱歉,有点标题党的感觉,但这貌似是苹果bug。。。