tableView中的两种类型的单元格 - 优美

时间:2013-09-11 08:51:52

标签: ios objective-c uitableview coding-style

你如何优雅地在同一个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案例中每个案例特有的行。

3 个答案:

答案 0 :(得分:2)

您可以根据类型实例化单元格,但将单元格定义为UITableViewCell并在方法结束时返回实例,从而做得更好。这也允许您为这些单元格可能共享的每个公共属性只写一行。类似的东西:

UICustomTableViewCell *cell = nil; //This cell type is a common super class of both cell classes
NSDictionary *cellInfo = [_userInformation objectAtIndex:indexPath.row];
NSString *cellType = [cellInfo objectForKey:@"type"];
if ([cellType isEqualToString:kProfileImage]) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"profileImageCell"];
}
else 
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"AccountCell"];
    ((AccountCell*) cell).textField.placeholder = [cellInfo objectForKey:@"textFieldPlaceholder"];
    ((AccountCell*) cell).textField.delegate = self;
    ((AccountCell*) cell).textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}
cell.delegate = self;
cell.descriptionLabel.text = [cellInfo objectForKey:@"cellLabelText"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//Any other common properties can by assigned here

return cell;

在此示例中,两个单元格类型都有一个共同的“委托”属性。

答案 1 :(得分:1)

您可以在开头将您的单元格声明为“id”或UITableViewCell。

NSDictionary *cellInfo = [_userInformation objectAtIndex:indexPath.row];
NSString *cellType = [cellInfo objectForKey:@"type"];
UITableViewCell* cell = nil;
if ([cellType isEqualToString:kProfileImage]) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"profileImageCell"];
}
else {
    cell = [tableView dequeueReusableCellWithIdentifier:@"AccountCell"];
    cell.textField.placeholder = [cellInfo objectForKey:@"textFieldPlaceholder"];
        cell.textField.delegate = self;
    cell.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
}

cell.descriptionLabel.text = [cellInfo objectForKey:@"cellLabelText"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

return cell;

但是,如果要调用这些自定义类的方法或属性,则可能需要某种类型转换。

((AccountCell*) cell).accountCellProperty = @"smth";

答案 2 :(得分:1)

重构上述代码的最佳方法是:

NSDictionary *cellInfo = [_userInformation objectAtIndex:indexPath.row];
NSString *cellType = [cellInfo objectForKey:@"type"];
UITableViewCell *aCell = nil;

if ([cellType isEqualToString:kProfileImage]) {
    aCell = [tableView dequeueReusableCellWithIdentifier:@"profileImageCell"];
} else {
    aCell = [tableView dequeueReusableCellWithIdentifier:@"AccountCell"];
    ((ProfileImageCell *)aCell).descriptionLabel.text = [cellInfo objectForKey:@"cellLabelText"];
    ((ProfileImageCell *)cell).textField.clearButtonMode = UITextFieldViewModeWhileEditing;


}

aCell.textField.delegate = self;
aCell.descriptionLabel.text = [cellInfo objectForKey:@"cellLabelText"];
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
return aCell;

注意: * 1。您不应该在方法中使用return multiple语句。 2.((ProfileImageCell )aCell):这是将“aCell”对象强制转换为“ProfileImageCell”类型的简单方法,因为“textField”不是UITableViewCell的属性。