在popover中将项添加到表视图

时间:2011-12-14 12:51:33

标签: objective-c ios xcode uitableview uipopovercontroller

在我的应用程序中,我有一个数组,它在循环中从json响应在Main_View_Controller中创建: 的 Main_View_Controller.m

NSMutableArray *Cities = [[NSMutableArray alloc] init];
while (ItemsFromParsedResponse = (NSDictionary *)[enumerator nextObject]) {
        AppDelegate *dataCenter = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        dataCenter.CityLabel = [ItemsFromParsedResponse objectForKey:@"label"];
        [Cities addObject:dataCenter.CityLabel];
        dataCenter = nil;
    }
AppDelegate *dataCenter = (AppDelegate*)[[UIApplication sharedApplication] delegate];
dataCenter.CityInfo = Cities;

此数组必须在包含TableView的Popover中显示。我试图将数组委托给popover,就像将它委托给AppDelegate一样,但它不起作用。如果我在CityList_Popover_Contoller中读取dataCenter.CityInfo,它的值为零。

CityList_Popover_Controller.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{//some standard code

AppDelegate *dataCenter = (AppDelegate*)[[UIApplication sharedApplication] delegate];
cell.textLabel.text = [dataCenter.CityInfo objectAtIndex:indexPath.row];
[tableView reloadData];
return cell;
}

如果CityInfo不是零,我该如何加载?如何跟踪此数组中的更改并根据数组中的新数据动态更新表内容?

很抱歉,如果我的问题太简单了,但我花了很多时间才能让它发挥作用。

感谢您的任何建议!

1 个答案:

答案 0 :(得分:1)

只需简单的if-check即可:

AppDelegate *dataCenter = appDelegate;
if ([dataCenter.CityInfo objectAtIndex:indexPath.row])
{
     cell.textLabel.text = [dataCenter.CityInfo objectAtIndex:indexPath.row];
}
else 
{
     NSLog(@"Whoops, null data at row %i", indexPath.row);
}
// NEVER call reload data here, you are 
// already reloading data when this 
// method is called, will end up in corruption.
// [tableView reloadData];
return cell;