举个例子,在 iOS 开发中,经常会遇到在设置 tableViewCell 时需要根据 indexPath 来设置不同 cell ,一般代码可能会是下面这个样子:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// 设置第一种 cell
if (indexPath.section == 3 && indexPath.row != 2) {
// cell 代码
return cell;
}
// 设置第二种 cell
if (indexPath.section == 3 && indexPath.row == 2) {
// cell 代码
return cell;
}
// 设置其他 cell
return cell;
}
当然,举的例子比较简单, if...else...也不多,这种场景在开发中很多,一般使用 if...else...或者 switch 时会使代码非常多,求大神们能不能给个思路,遇到这种场景尽量不要出现这种嵌套代码,最好给个实例膜拜膜拜 ^_^