多个方法命名错误。涉及多个 类

Multiple methods named error. Multiple classes involved

代码的最后一行 "return [cell height]" 给我一个错误 "Multiple methods named "height" found with mismatched result, parameter type or attributes"。此代码在 32 位上运行良好,但在 64 位(模拟器)上运行不佳。

请高手帮忙解决一下。还有其他帖子提到了这一点,但它们只有一种类型的 class 可以进行类型转换。我在数组 self.cells

中有不同的 classes

我可以检查每个 "cell" 的 class 类型,然后 return 相应的高度,但这是一个繁琐的代码。有更好的方法吗?

非常感谢帮助。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:  (NSIndexPath *)indexPath
{
    id cell = self.cells[indexPath.row];
    if ([cell isKindOfClass:[TripCell class]])
    {
        return 150;
    }
    return [cell height];
}

您的 "cell" 被声明为 "id" 类型。这意味着编译器根本不知道它是什么类型的对象。因此,如果您将消息 "height" 发送到声明为 id 的对象,编译器会假定它是它知道的名为 "height" 的方法之一。如果您有不同的 "height" 方法和不同的 return 类型,那么编译器不知道这个调用会 return。

例如,如果您有一个方法 - (int) height 在一个 class 中,而另一个方法 - (CGFloat) height 在另一个 class 中,编译器不知道是否value returned by the method 是 int 或 CGFloat。

解决方案是:将 "id" 中的单元格转换为特定类型,最好是正确的类型。或者不要对名为 "height".

的方法使用不同的 return 类型