2012-04-28 79 views
0

如何將getnewdata的結果添加到我現有的tableView中使用insertRowsAtIndexPaths?我找到了幾個例子,但無法弄清楚。如何使用insertRowsAtIndexPaths將數組內的字典添加到UItableview?

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

你必須是一個Ruby開發者:-)。 Objective-c約定是camelCase。 – danh 2012-04-28 19:13:54

回答

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; 
+0

感謝這工作正常,不幸的是沒有** insertRowsAtIndexLevel ** – 2012-04-29 10:37:14

+0

你是什麼意思? – danh 2012-04-29 14:15:26