为了自定义 cell 同时可以复用 cell,建立了一个 UITableViewCell,如下所示
import UIKit class newcell: UITableViewCell { var tit = UILabel() var str = "" }
当给这个 cell 传递参数的时候 cell?.tit.text = title 显示没问题,但 cell?.str = content 就没用(打印 src 依然为空,content 确定是有内容的),请问这里的字串符要怎么传给 cell 啊?
![]() | 1 jamfer OP 自顶一下 |
![]() | 2 paradoxs 2017-07-17 13:09:56 +08:00 当你 cell?.str = content 的时候, 你确定这个 cell 是 var tit = UILabel() 创造出来的吗? po 这个 cell,看一下内存 |
![]() | 3 jamfer OP @paradoxs var tit = UILabel() 只是这个 cell 里的一个元素,下面代码还有很多我没贴。创建 cell 的代码如下: var cell = tableView.dequeueReusableCell(withIdentifier:"cell") as? newscell if cell == nil{ cell = newcell(style:.default, reuseIdentifier:"cell") cell?.selectiOnStyle= .none } cell?.tit.text = title cell?.imgstr = content return cell! |
![]() | 5 kingcos 2017-07-17 14:36:56 +08:00 1. 你是在哪里打印的? 2. 你的代码里一会儿是 as? newscell,一会儿是 newcell,你是不是创建了不同的自定义 Cell 然后搞混了? 3. 代码最好规范一些,否则看起来很凌乱,当然只是建议。 |
![]() | 6 loveuqian 2017-07-17 14:51:25 +08:00 使用 dequeueReusableCellWithIdentifier 复用 cell 的时候 为什么还要判断nil,我一直没看懂好多代码都要判断 nil 干嘛 只要你注册了肯定是有的啊,哪种情况会出 nil ? |
![]() | 7 zhNaMore 2017-07-17 14:59:02 +08:00 ![]() 这个 imgStr 是图片地址吧。你这个图片地址的 set 方法先于 cell 的构造方法实现就会出现图片为空的。 你把 cell 的图片赋值 image = imageStr 放到 didset 或者 set 方法试试 |
![]() | 8 jamfer OP |