您将如何在同一个 tableView 中优雅地编码两种类型的单元格?
显然我可以这样做:
NSDictionary *cellInfo = [_userInformation objectAtIndex:indexPath.row];
NSString *cellType = [cellInfo objectForKey:@"type"];
if ([cellType isEqualToString:kProfileImage]) {
ProfileImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"profileImageCell"];
cell.descriptionLabel.text = [cellInfo objectForKey:@"cellLabelText"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
else {
AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountCell"];
cell.descriptionLabel.text = [cellInfo objectForKey:@"cellLabelText"];
cell.textField.placeholder = [cellInfo objectForKey:@"textFieldPlaceholder"];
cell.textField.delegate = self;
cell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
return nil;
但是我的老师总是告诉我不要写两次,如果它在两种情况下都发生,并且如你所见,两种情况下有 3 行是相同的。我想将它们移到 if 之外,并且只在 if 正文中保留针对每种情况的特定行。