ios - 子类化 UITableViewCell (并且可能使用 id)

标签 ios uitableview

当我在 TableView 中设置自定义单元格时,我试图找到一种不重复代码的方法。我目前有一个 BaseCell 类,它是 UITableViewController 的子类,然后是 BaseCell 的两个子类:CellTypeOne 和 CellTypeTwo。这是我目前正在使用的设置:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *type = [self.rowTypes objectAtIndex:indexPath.row];

    if ([type isEqualToString:@"Type 1"]) {
        CellTypeOne *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];

        cell.picture.image = // An image
        cell.caption.text = // A caption

        cell.author.text = // An author

        return cell;
    }

    else if {[type isEqualToString:@"Type 2"]) {
        CellTypeTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];

        cell.picture.image = // An image
        cell.caption.text = // A caption

        cell.number.text = // A number

        return cell;
    }
}

由于设置的某些属性对于任一单元格类型都是相同的,我想知道是否有办法执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *type = [self.rowTypes objectAtIndex:indexPath.row];

    id cell;

    if ([type isEqualToString:@"Type 1"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];

        // Cast cell to (CellTypeOne *)?

        cell.author.text = // An author
    }

    else if {[type isEqualToString:@"Type 2"]) {
        CellTypeTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];

        // Cast cell to (CellTypeTwo *)?

        cell.number.text = // A number
    }

    cell.picture.image = // An image
    cell.caption.text = // A caption

    return cell;
}

这样我就可以为每个子类进行类特定设置,然后为两个子类进行通用设置。我让它工作的唯一方法是每次需要使用它时转换单元格,如下所示:

[(BaseCell *)cell picture].image = // An image

必须在每一行上转换单元格似乎比一开始就有几行重复的代码需要更多的工作,但我只是想找出解决此问题的最佳方法。

最佳答案

您可以将 cell 变量设置为 BaseCell* 类型,以避免在 ifs 之外进行强制转换:

BaseCell *cell;

if ([type isEqualToString:@"Type 1"]) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];
    [[cell author] setText:/*an author*/];
} else if {[type isEqualToString:@"Type 2"]) {
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];
    [[cell number] setText: /* a number */];
}

cell.picture.image = // An image
cell.caption.text = // A caption

或者,您可以保留id的类型,并在ifs之外使用方法调用语法而不是成员(点)语法:

id cell;
// ... the code from your post
[[cell picture] setImage: /* an image */];
[[cell caption] setText: /* a caption */];

最后,您可以通过在 if 中声明其他变量来完全避免强制转换方括号:

BaseCell *cell;

if ([type isEqualToString:@"Type 1"]) {
    CallTypeOne *cellOne = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];
    cellOne.author.text = // An author
    cell = cellOne;
} else if {[type isEqualToString:@"Type 2"]) {
    CellTypeTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];
    cellTwo.number.text = // A number
    cell = cellTwo;
}

关于ios - 子类化 UITableViewCell (并且可能使用 id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9476062/

相关文章:

读取 Json 后 iOS PickerView 为空

objective-c - 您如何知道用户何时选择通过AirPlay播放视频?

ios - 多 View Controller - iOS 7

ios - Swift 2 - prepareForSegue 无值

ios - Tableview 数组未在 iOS Swift 中正确加载?

ios - AVAssetWriter 为影片输出添加绿框

iphone - 将数组复制到字符串并按顺序显示字符串

ios - 如何让选中的tableview单元格像Vine App一样没有颜色

ios - UITableView reloadSection 导致缺少显示的单元格

ios - 检测 UITableViewCell 是否为重用单元格