UITableView重新加载不在顶部添加行

时间:2014-08-02 06:46:37

标签: ios objective-c uitableview

我使用以下代码将row始终放在顶部,然后重新加载UITableView中的数据。但它没有在顶部添加row

car = [[NSMutableArray alloc] initWithObjects:@"volvo",@"volvo1", nil];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [car count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellidentifier"];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellidentifier"];
}

cell.textLabel.text = [car objectAtIndex:indexPath.row];


if(!indexPath.row == 0)
{

[self.tableview beginUpdates];
NSIndexPath *indexpath1 = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indextoadd = [NSArray arrayWithObject:indexpath1];

[self.tableview insertRowsAtIndexPaths:indextoadd withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableview endUpdates];
[self.tableview reloadData];

}

return cell;
}

提前致谢。

1 个答案:

答案 0 :(得分:2)

删除此代码:

[self.tableview beginUpdates];
 NSIndexPath *indexpath1 = [NSIndexPath indexPathForRow:0 inSection:0];
 NSArray *indextoadd = [NSArray arrayWithObject:indexpath1];

[self.tableview insertRowsAtIndexPaths:indextoadd withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableview endUpdates];
[self.tableview reloadData];

来自cellForRowAtIndexPath:并将其放在其他地方。

cellForRowAtIndexPath:
指定

创建或出列单元格,它不是将对象插入数组并重新加载表的地方

相关问题