【问题标题】:Adding items to table view in popover在弹出窗口中将项目添加到表视图
【发布时间】:2012-01-20 05:54:02
【问题描述】:

在我的应用程序中,我有一个数组,它在 Main_View_Controller 从循环中的 json 响应中创建: 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;

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

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 不是零,我如何才能加载它?以及如何跟踪该数组的变化并根据数组中的新数据动态更新表格内容?

对不起,如果我的问题太简单了,但我花了很多时间才使它起作用。

感谢您的建议!

【问题讨论】:

    标签: objective-c ios xcode uitableview uipopovercontroller


    【解决方案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;
    

    【讨论】:

    • 不幸的是它没有。我忘了说:填充数组的方法是通过TextField的-editingChaged事件调用的,用户在其中写入城市。来自服务器的新响应来了,并用新数据填充数组。
    • @Akki 然后,当您从服务器获得响应时,只需在委托方法或其他方法中调用 [tableView reloadData] 即可。
    猜你喜欢
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 2020-04-26
    • 2017-01-01
    • 2012-05-30
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多