IOS 自定义TableViewCell 加载过多的问题

iOS 码拜 6年前 (2015-05-11) 399次浏览 0个评论

我使用了单元格的重用机制了,但是我当我在tableview cellForRowAtIndexPath 的方法中打印indexPath.row时,发现重用机制不仅没有起作用,还乱套了,下面是我打印出的日志,datalist 里面总共只用12条,正常不是应该屏幕能够加载几条就使用几条么?求解答,万分感谢,需要的话我可以提供源代码。
2015-05-01 20:45:32.697 Custom Cell[3729:125567] indexpath = 0
2015-05-01 20:45:32.698 Custom Cell[3729:125567] indexpath = 1
2015-05-01 20:45:32.699 Custom Cell[3729:125567] indexpath = 2
2015-05-01 20:45:32.700 Custom Cell[3729:125567] indexpath = 3
2015-05-01 20:45:32.701 Custom Cell[3729:125567] indexpath = 4
2015-05-01 20:45:32.702 Custom Cell[3729:125567] indexpath = 5
2015-05-01 20:45:32.703 Custom Cell[3729:125567] indexpath = 6
2015-05-01 20:45:32.704 Custom Cell[3729:125567] indexpath = 7
2015-05-01 20:45:32.704 Custom Cell[3729:125567] indexpath = 8
2015-05-01 20:45:32.705 Custom Cell[3729:125567] indexpath = 9
2015-05-01 20:45:32.706 Custom Cell[3729:125567] indexpath = 10
2015-05-01 20:45:32.707 Custom Cell[3729:125567] indexpath = 11
2015-05-01 20:45:32.708 Custom Cell[3729:125567] indexpath = 0
2015-05-01 20:45:32.709 Custom Cell[3729:125567] indexpath = 1
2015-05-01 20:45:32.710 Custom Cell[3729:125567] indexpath = 2
2015-05-01 20:45:32.710 Custom Cell[3729:125567] indexpath = 3
2015-05-01 20:45:32.711 Custom Cell[3729:125567] indexpath = 4
2015-05-01 20:45:32.712 Custom Cell[3729:125567] indexpath = 5
2015-05-01 20:45:32.713 Custom Cell[3729:125567] indexpath = 6
2015-05-01 20:45:32.714 Custom Cell[3729:125567] indexpath = 7
2015-05-01 20:45:32.714 Custom Cell[3729:125567] indexpath = 8
2015-05-01 20:45:32.715 Custom Cell[3729:125567] indexpath = 9
2015-05-01 20:45:32.716 Custom Cell[3729:125567] indexpath = 10
2015-05-01 20:45:32.717 Custom Cell[3729:125567] indexpath = 11
2015-05-01 20:45:32.717 Custom Cell[3729:125567] indexpath = 0
2015-05-01 20:45:32.718 Custom Cell[3729:125567] indexpath = 1
2015-05-01 20:45:32.719 Custom Cell[3729:125567] indexpath = 2
2015-05-01 20:45:32.720 Custom Cell[3729:125567] indexpath = 3
2015-05-01 20:45:32.721 Custom Cell[3729:125567] indexpath = 4
2015-05-01 20:45:32.722 Custom Cell[3729:125567] indexpath = 5
2015-05-01 20:45:32.723 Custom Cell[3729:125567] indexpath = 6
2015-05-01 20:45:32.723 Custom Cell[3729:125567] indexpath = 7
2015-05-01 20:45:32.724 Custom Cell[3729:125567] indexpath = 8
2015-05-01 20:45:32.725 Custom Cell[3729:125567] indexpath = 9
2015-05-01 20:45:32.726 Custom Cell[3729:125567] indexpath = 10
2015-05-01 20:45:32.726 Custom Cell[3729:125567] indexpath = 11
2015-05-01 20:45:32.728 Custom Cell[3729:125567] indexpath = 0
2015-05-01 20:45:32.728 Custom Cell[3729:125567] indexpath = 1
2015-05-01 20:45:32.729 Custom Cell[3729:125567] indexpath = 2
2015-05-01 20:45:32.730 Custom Cell[3729:125567] indexpath = 3
2015-05-01 20:45:32.731 Custom Cell[3729:125567] indexpath = 4
2015-05-01 20:45:32.732 Custom Cell[3729:125567] indexpath = 5
2015-05-01 20:45:32.732 Custom Cell[3729:125567] indexpath = 6
2015-05-01 20:45:32.733 Custom Cell[3729:125567] indexpath = 7
2015-05-01 20:45:32.734 Custom Cell[3729:125567] indexpath = 8
2015-05-01 20:45:32.735 Custom Cell[3729:125567] indexpath = 9
2015-05-01 20:45:32.736 Custom Cell[3729:125567] indexpath = 10
2015-05-01 20:45:32.737 Custom Cell[3729:125567] indexpath = 11
2015-05-01 20:45:32.738 Custom Cell[3729:125567] indexpath = 0
2015-05-01 20:45:32.739 Custom Cell[3729:125567] indexpath = 1
2015-05-01 20:45:32.739 Custom Cell[3729:125567] indexpath = 2
2015-05-01 20:45:32.740 Custom Cell[3729:125567] indexpath = 3
2015-05-01 20:45:32.741 Custom Cell[3729:125567] indexpath = 4
2015-05-01 20:45:32.742 Custom Cell[3729:125567] indexpath = 5
2015-05-01 20:45:32.743 Custom Cell[3729:125567] indexpath = 6
2015-05-01 20:45:32.743 Custom Cell[3729:125567] indexpath = 7
2015-05-01 20:45:32.744 Custom Cell[3729:125567] indexpath = 8
2015-05-01 20:45:32.745 Custom Cell[3729:125567] indexpath = 9
2015-05-01 20:45:32.746 Custom Cell[3729:125567] indexpath = 10
2015-05-01 20:45:32.746 Custom Cell[3729:125567] indexpath = 11
2015-05-01 20:45:32.747 Custom Cell[3729:125567] indexpath = 0
2015-05-01 20:45:32.748 Custom Cell[3729:125567] indexpath = 0
2015-05-01 20:45:32.750 Custom Cell[3729:125567] indexpath = 1
2015-05-01 20:45:32.751 Custom Cell[3729:125567] indexpath = 1
2015-05-01 20:45:32.751 Custom Cell[3729:125567] indexpath = 2
2015-05-01 20:45:32.752 Custom Cell[3729:125567] indexpath = 2

兄弟们如需看代码,我上传到百度云盘上了,公链:http://pan.baidu.com/s/1eQzX0H0
40分
引用 1 楼 Hitourlee 的回复:

兄弟们如需看代码,我上传到百度云盘上了,公链:http://pan.baidu.com/s/1eQzX0H0

你不能在 heightForRowAtIndexPath 调用 cellForRowAtIndexPath 方法的,因为 tableView 加载数据的时候, heightForRowAtIndexPath 会对数据源的每条数据都调用一次,预先把 Cell 的高度获取到,而且你只为了获取 Cell 的高度,就创建出一个完全用不着的Cell,这样不合理, Cell 的创建开销很大的,如果你要做 Cell 动态高度的话,你可以看看我的博客:http://blog.csdn.net/zhangao0086/article/details/7165041

多谢回复,一语中的,一直没注意这个地方,你一提我马上醒悟了。分给你了。
引用 2 楼 zhangao0086 的回复:
Quote: 引用 1 楼 Hitourlee 的回复:

兄弟们如需看代码,我上传到百度云盘上了,公链:http://pan.baidu.com/s/1eQzX0H0

你不能在 heightForRowAtIndexPath 调用 cellForRowAtIndexPath 方法的,因为 tableView 加载数据的时候, heightForRowAtIndexPath 会对数据源的每条数据都调用一次,预先把 Cell 的高度获取到,而且你只为了获取 Cell 的高度,就创建出一个完全用不着的Cell,这样不合理, Cell 的创建开销很大的,如果你要做 Cell 动态高度的话,你可以看看我的博客:http://blog.csdn.net/zhangao0086/article/details/7165041

你好,分数当时定少了,我发个追加帖子,给你加另外的45分,链接http://bbs.csdn.net/topics/391027561,多谢。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明IOS 自定义TableViewCell 加载过多的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!