【问题标题】:How to add dictionary inside of an array to UItableview using insertRowsAtIndexPaths?如何使用 insertRowsAtIndexPaths 将数组内的字典添加到 UItableview?
【发布时间】:2012-04-28 19:05:07
【问题描述】:

如何使用 insertRowsAtIndexPathsgetnewdata 的结果添加到我现有的 tableView 中?我找到了几个例子,但无法弄清楚。

- (void)getfirstdata {
     [...] //get JSON data
     self.data = jsonResult; //a dictionary inside of an array
     [self.tableView reloadData];
}

- (void)getnewdata {
     [...] //get new JSON data which has to be added to top of the table
     self.data = jsonnewResult; //a dictionary inside of an array
     [self.tableView reloadData];
}

(UITableViewCell *) tableView: (UITableView *) table_view cellForRowAtIndexPath: (NSIndexPath *) index_path
    static NSString *CellIdentifier = @"Cell";
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    id celldata = [self.data objectAtIndex:[index_path row]];
    cell.textLabel.text = [celldata valueForKeyPath:@"user.name"];
    cell.detailTextLabel.text = [celldata objectForKey:@"text"];

return cell;
}

【问题讨论】:

  • 您必须是 ruby​​ 开发人员:-)。 Objective-c 约定是 camelCase。

标签: iphone arrays uitableview dictionary cell


【解决方案1】:

为了获得新的,你想添加到你的数据中,所以......

- (void)getnewdata {
     [...] //get new JSON data which has to be added to top of the table
     [self.data addObjectsFromArray:jsonResult];
     [self.tableView reloadData];
}

确保 self.data 是一个 NSMutableArray。

编辑

我突然想到,您可能希望新数据放在首位......

NSMutableArray *newModel = [NSMutableArray arrayWithArray:jsonResult];
[newModel addObjectsFromArray:self.data];
self.data = newModel;

【讨论】:

  • 感谢这个工作正常,不幸的是没有 insertRowsAtIndexLevel
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2021-10-29
  • 2018-04-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
相关资源
最近更新 更多