首页 > 在tableView中插入一个cell

在tableView中插入一个cell

在tableView的顶部第0行插入一个cell
过程是这样的:使用百度推送每次会推送来一条消息,页面监控推送信息,将新推送来的消息加入到该页面原有的tableView中(加入到顶端的第0行),这个怎么破??
-(void)addRows{

NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[indexPaths addObject: indexPath];

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

}
貌似是要用这句 [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];但是怎么编辑新插入的这个cell啊.........


没仔细看你代码,我写代码都遵循 MVC,模型驱动视图
你想要在第0行增加东西,你就去你的模型数组,在第0个位置增加模型
你这种直接增加一个 cell 就是面向过程的编程思想
如果是我会增加一个模型,然后让 tableView 刷新
或者这个 cell 比较特殊我就用 headerView 去做


insertRowsAtIndexPaths之后,同时得更新dataSource和delegate中对应的方法


感觉loveuqian说的很好啊,去改变数据,然后刷新界面。不用插入吧,毕竟你的cell是用来展示数据的


找到出问题的位置了, 做过修改之后可以实现在tableView的第0行插入一个新的cell

-(void)addRows{
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
   [indexPaths addObject: indexPath];
//这个位置应该在修改tableView之前将数据源先进行修改,否则会崩溃........必须向tableView的数据源数组中相应的添加一条数据
   [self.tableView beginUpdates];
   [self.tableView insertRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationLeft];
   [self.tableView endUpdates];
}
【热门文章】
【热门文章】