2

Currently I fill a UITableView using this method:

- (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;
}

But what could one do if you had two different arrays from two different types and you wanted to display a property from each in the cells?

Pseudocode:

MyObject1 MyObject2

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

Assuming that each object has a name property. I know my syntax above isn't correct, but I think you should get the drift.

4

1 回答 1

0

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

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
}

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

于 2012-07-08T20:03:54.110 回答