iOS之如何处理TableView cell内容不一致以及cell复用重叠的问题
来源:互联网 发布:bios密码 作用 知乎 编辑:程序博客网 时间:2024/04/28 08:52
1.问题描述:自己在做之前iOS项目之Swfit新闻App的时候,发现解析出来的Json串中的model数据存在不一致的情况,就是说有的cell有label和图片,有的cell仅仅只有label。那如何处理这种情况呢?
解决方案:我在用Alamofire网络库对url进行解析的时候,对解析出来的数据会进行一次判断,如果某一项解析出来的imageUrl为nil的话,我会对model中对应的某项的imageUrl设置为”empty”,然后在TableView的绘制cell的回调方法中对某一行的cell进行赋值的时候,对imageUrl进行判断如果url为empty,则只为该cell的label进行赋值运算,反之则都赋值。这样就可以处理cell中内容不一致的情况。部分代码如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let initIdentifier = "MyThemeCell" var cell = tableView.dequeueReusableCellWithIdentifier(initIdentifier) as? ThemeDataTableViewCell let url = self.item[indexPath.row].newsImageName if cell == nil{ tableView.registerNib(UINib(nibName: "ThemeDataTableViewCell", bundle: nil), forCellReuseIdentifier: initIdentifier) cell = tableView.dequeueReusableCellWithIdentifier(initIdentifier) as? ThemeDataTableViewCell } //利用SDWebImage异步加载图片 if url != "empty"{ cell!.themeImageUrl.sd_setImageWithURL(NSURL(string: url)) } cell!.themeLabel.text = self.item[indexPath.row].newsTitle return cell! }
2.问题描述:在对cell进行绘制的时候,会对cell进行复用,这样可以提高TableView的性能。但是在这里,由于有的cell有图片有的cell没有图片,在对TableView进行上拉或者下拉的时候,会出现一个奇怪的现象—那就是之前没图片的cell竟然有图片了,而且多拉几次,图片还会发生变化,这就是cell重用所产生的问题。
解决方案:方法其实也很简单,就是在对cell进行绘制的时候,根据该cell的imageUrl的有无,来返回两种不同的cell。一种是系统自带的UITableViewCell,一种是自定义的ThemeDataTableViewCel。部分代码如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let url = self.item[indexPath.row].newsImageName if url == "empty"{ let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") cell.textLabel!.text = self.item[indexPath.row].newsTitle cell.textLabel?.numberOfLines = 3 cell.textLabel?.font = UIFont.systemFontOfSize(CGFloat(15)) return cell }else{ let initIdentifier = "MyThemeCell" var cell = tableView.dequeueReusableCellWithIdentifier(initIdentifier) as? ThemeDataTableViewCell if cell == nil{ tableView.registerNib(UINib(nibName: "ThemeDataTableViewCell", bundle: nil), forCellReuseIdentifier: initIdentifier) cell = tableView.dequeueReusableCellWithIdentifier(initIdentifier) as? ThemeDataTableViewCell } cell!.themeImageUrl.sd_setImageWithURL(NSURL(string: url)) cell?.themeLabel.text = self.item[indexPath.row].newsTitle cell?.selectionStyle = UITableViewCellSelectionStyle.None return cell! } }</span>
3.详见代码部分(ThemeDataViewController+TableView.swift)
源码:我的Github
- iOS之如何处理TableView cell内容不一致以及cell复用重叠的问题
- ios tableView多个cell处理键盘遮挡内容视图的问题
- ios开发笔记之如何让tableView根据文本内容动态改变cell的高度
- tableView的cell的复用问题
- 如何处理Tableview中cell的单选问题
- iOS 开发之如何编辑tableView上的cell
- iOS 之 TableView如何刷新指定的cell 或section
- tableView的分割线及其cell重叠问题
- iOS tableView的cell的重用问题
- IOS中TableView的cell显示问题
- tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,cell复用出现问题
- tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,cell复用出现问题
- ios UICollectionView的cell重叠
- iOS 解决使用Cell的复用导致视图重叠的问题
- tableView以及cell的使用
- UITableView 的cell重叠问题
- IOS中tableView的cell的复用机制-许笑欢
- ios的tableview的cell
- 从一个action跳转到另一个action
- 杭电3085 Nightmare Ⅱ(双向bfs)(曼哈顿距离)
- 神经网络编程入门
- Spring Bean在BeanFactory生命周期
- JS之理解组合继承模式和寄生式组合继承的区别
- iOS之如何处理TableView cell内容不一致以及cell复用重叠的问题
- treegrid-3.0特性详解
- Android学习之高德地图 一(定位)
- 机器学习之回归
- hive使用rank实现topN的查询
- iOS navigation UIBarButtonItem 风格
- Uboot 2014.07 makefile分析
- 学习日记--PopupWindow简单功能实现
- zzulioj 1831: 周末出游 (vector&&dfs)