美文网首页
如何改变 cell 的 UITableViewCellStyle

如何改变 cell 的 UITableViewCellStyle

作者: Desmond_ | 来源:发表于2018-09-27 16:55 被阅读64次

一般地,我们对 cell 进行复用会采用如下代码:

// 在 viewDidLoad 方法中注册
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: identify, for: indexPath)
  return cell
}

但这样做的结果是 cell 的 UITableViewCellStyle 属性是默认的 default。而 UITableViewCellStyle 只能在如下初始化方法中设置:

cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")

那该如何改变 UITableViewCellStyle 且同时达到复用的目的呢?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: UITableViewCell? = nil
        cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        }
        return cell!
    }

Ref:
https://blog.csdn.net/weixin_42349140/article/details/80947266
http://www.cocoachina.com/bbs/read.php?tid-1730647.html
http://www.cocoachina.com/bbs/read.php?tid-1706470.html

相关文章

网友评论

      本文标题:如何改变 cell 的 UITableViewCellStyle

      本文链接:https://www.haomeiwen.com/subject/wooeoftx.html