objective-c - 如何用来自两个不同对象类型的两个数组的数据填充 UITableView?

标签 objective-c ios xcode

目前我使用这种方法填充 UITableView :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CharNameCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    MyObject *obj = (MyObject*)[self.characters objectAtIndex:indexPath.row];
    cell.textLabel.text = obj.name;

    return cell;
}

但是,如果您有来自两种不同类型的两个不同数组,并且您想在单元格中显示每个数组的属性,该怎么办?

伪代码:

我的对象1
我的对象2

cellTextLabel.text = Myobject1.name;
cellTextLabel.text = MyObject2.name;

假设每个对象都有一个 name 属性。我知道我上面的语法不正确,但我认为你应该明白这一点。

最佳答案

我建议您将所有对象存储在 NSMutableArray 中。这将是您的数据模型。然后,您可以遍历数组以在 UITableView 中显示数据。如果需要,请使用自省(introspection)来找出您的对象属于哪种类。

id currentObject = [myArray objectAtIndex:indexPath.row];

if ([currentObject isKindOfClass:[MyObject1 class]]){
    //set properties or do stuff with MyObject1
}else if ([currentObject isKindOfClass:[MyObject2 class]]){
    //do stuff with Object2
}

这只是一个建议。有很多方法可以做到这一点,但这完全取决于您的应用程序以及您使用的持久性等。希望这会有所帮助。

关于objective-c - 如何用来自两个不同对象类型的两个数组的数据填充 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11386181/

相关文章:

iphone - 如何使用 NSLog(@"Inside of the iPhone Simulator") 进行调试?

xcode - 由于 xcodebuild 失败 : "Carthage binary is not found." 无法启动 WebDriverAgent

ios - acos和cos的度数未给出正确的结果

iphone - 如何按价格对应用内购买的产品进行排序?

ios - 当使用@dynamic在类别中添加@property时,getter/setter如何实现

ios - Adcolony 广告并非每次都有效

ios - 如何将 Swift 中的 Core Data 与 Cloudkit 以及在许多设备上同步

iphone - 当uitextview使用google api加载当前位置时停止activityIndi​​cator

iOS:在#define 中检查系统版本并做不同的事情,编译器错误

iphone - 这在iphone编程中意味着什么?