1

您将如何在同一个 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 正文中保留针对每种情况的特定行。

4

3 回答 3

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;

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

于 2013-09-11T08:55:53.837 回答
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 多个语句。2. ((ProfileImageCell )aCell):这是一种将“aCell”对象类型转换为“ProfileImageCell”类型的简单方法,因为“textField”不是 UITableViewCell 的属性。

于 2013-09-11T09:07:45.173 回答
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";
于 2013-09-11T08:58:50.730 回答