Swift cell滑动数据丢失,图片重叠,cell重用问题的解决

Swift cell滑动数据丢失,图片重叠,cell重用问题的解决,第1张

概述公司的项目,需要做一个类似QQ空间动态展示的一个页面如下图(忽略UI :))~ 上图可以看出一个Section分为三个row,第一个row是头像、名字、时间。第二个row是文字、图片。第三行是点赞加评论按钮。 首先是cell重用问题,使得第二个row的图片重叠,如下图: 解决方法:(用Storyborad写布局的小伙伴,这里最好不要在这个cell中拖拽控件进去) 这个方法是通过删除重用的cell的

公司的项目,需要做一个类似QQ空间动态展示的一个页面如下图(忽略UI :))~

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="1.png" alt="wKioL1ap5A-yy3WzAAEWRX6kUvc257.png" src="http://s2.51cto.com/wyfs02/M00/7A/7A/wKioL1ap5A-yy3WzAAEWRX6kUvc257.png">

上图可以看出一个Section分为三个row,

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" alt="i_f01.gif" src="http://img.baidu.com/hi/face/i_f01.gif">

第一个row是头像、名字、时间。

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" alt="i_f01.gif" src="http://img.baidu.com/hi/face/i_f01.gif">

第二个row是文字、图片。

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" alt="i_f01.gif" src="http://img.baidu.com/hi/face/i_f01.gif">

第三行是点赞加评论按钮。

首先是cell重用问题,使得第二个row的图片重叠,如下图:

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="2.jpg" alt="wKioL1ap5XKQngRsAAIt0wumJfs105.jpg" src="http://s3.51cto.com/wyfs02/M01/7A/7B/wKioL1ap5XKQngRsAAIt0wumJfs105.jpg">

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" alt="t_0001.gif" src="http://img.baidu.com/hi/tsj/t_0001.gif">

解决方法:(用Storyborad写布局的小伙伴,这里最好不要在这个cell中拖拽控件进去)

这个方法是通过删除重用的cell的所有子视图,从而得到一个没有特殊格式的cell,供其他cell重用。

IndetifIErname = "blogCell"

var cell2:BlogCelltableVIEwCell? = tableVIEw.dequeueReusableCellWithIDentifIEr(IndetifIErname) as? BlogCelltableVIEwCell

var blogimage:UIImageVIEw?

if cell2 == nil{

cell2 = BlogCelltableVIEwCell(style:UItableVIEwCellStyle.Default,reuseIDentifIEr:IndetifIErname)

}

else{

//删除所有子视图

while(cell2?.contentVIEw.subvIEws.last != nil){

cell2?.contentVIEw.subvIEws.last?.removeFromSupervIEw()

}

}

//其他赋值代码

cell重用导致数据丢失

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" alt="t_0026.gif" src="http://img.baidu.com/hi/tsj/t_0026.gif">

完成点赞效果,点击按钮点赞按钮时候,颜色改变,然后提示点赞成功如下图

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="3.jpg" alt="wKioL1ap6ROin6X9AAHag5IDf88767.jpg" src="http://s5.51cto.com/wyfs02/M01/7A/7B/wKioL1ap6ROin6X9AAHag5IDf88767.jpg">


然后奇怪的是,上滑动或下滑动之后,颜色就会变回原来的颜色,但是数据已经提交到服务器,整个tablevIEw reloadData之后会显示已经点赞。

解决方式

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="4.jpg" alt="wKioL1ap6_XjCwTPAACIWCnWw8Y299.jpg" src="http://s4.51cto.com/wyfs02/M02/7A/7B/wKioL1ap6_XjCwTPAACIWCnWw8Y299.jpg">

新建一个NSMutableSet(),用来存放你点赞的blog的ID,点赞,存进去,取消点赞,删除(普通的数据丢失,在dIDSelecetRow里面存放indexPath就好了)反正存一个唯一的标记。

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="5.jpg" alt="wKiom1ap7T6gcS5zAAGeG_HQE7A727.jpg" src="http://s5.51cto.com/wyfs02/M02/7A/7C/wKiom1ap7T6gcS5zAAGeG_HQE7A727.jpg">

然后在加载cell的最后做一个判断,判断所有的blogID中哪些存起来,那么就显示点赞

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="6.jpg" alt="wKiom1ap7TCg2Qq6AACntJ5nYA8406.jpg" src="http://s4.51cto.com/wyfs02/M01/7A/7C/wKiom1ap7TCg2Qq6AACntJ5nYA8406.jpg">

困扰了3天的问题终于解决了~感谢一下帮助我的文章

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" alt="t_0002.gif" src="http://img.baidu.com/hi/tsj/t_0002.gif">

http://www.2cto.com/kf/201304/204114.html

650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" alt="t_0002.gif" src="http://img.baidu.com/hi/tsj/t_0002.gif">

http://www.jianshu.com/p/c2a9c449d93f

如果有小白想看Swift自适应高度的cell和Label请点进我的博客列表,寻找“Swift 自适应cell与Label的高度”的这篇文章~谢谢大家关注!本人菜鸟,希望大家多多批评与指教~

总结

以上是内存溢出为你收集整理的Swift cell滑动数据丢失,图片重叠,cell重用问题的解决全部内容,希望文章能够帮你解决Swift cell滑动数据丢失,图片重叠,cell重用问题的解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1080232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存