ios - 在tableView中插入一个cell
高洛峰
高洛峰 2017-04-17 17:14:18
[iOS讨论组]

在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啊.........

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(4)
迷茫

找到出问题的位置了, 做过修改之后可以实现在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];
}
大家讲道理

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

大家讲道理

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

迷茫

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号