![](/img/trans.png)
[英]How to combine two arrays with different types of entities into one json object?
[英]How can you fill a UITableView with data from two arrays of two different object types?
目前,我使用此方法填充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;
}
但是,如果您有兩種不同類型的兩個不同數組,並且想要在每個單元格中顯示一個屬性,那該怎么辦?
偽代碼:
MyObject1 MyObject2
cellTextLabel.text = Myobject1.name; cellTextLabel.text = MyObject2.name;
假設每個對象都有一個name屬性。 我知道我上面的語法不正確,但是我認為您應該有所幫助。
然后,我建議您將所有對象存儲在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
}
這只是一個建議。 有很多方法可以做到這一點,但這都取決於您的應用程序以及所使用的持久性等。希望這對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.