如何在Swift中设置UITableViewCellStyleSubtitle和dequeueReusableCell?

 命硬D小童鞋 发布于 2023-01-11 14:43

我想要一个UITableView使用的带subtitle式单元格dequeueReusableCellWithIdentifier.

我原来的Objective-C代码是:

static NSString*    reuseIdentifier = @"Cell";
    UITableViewCell*    cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }

UITableViewSO上搜索了几个问题之后,我想在Swift中写这样的话:

    tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

但这并没有让我说我想要一种subtitle风格.所以我尝试了这个:

var cell :UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")

这给了我一个subtitle细胞,但它不让我dequeueReusableCellWithIdentifier.

我已经研究了一些并看了这个视频教程,但他创建了一个单独subclassUITableViewCell,我认为是不必要的,因为我之前在Obj-C中完成了相同的效果.

有任何想法吗?谢谢.

6 个回答
  • 请记住,UITableView在函数中定义为可选,这意味着您的初始单元格声明需要检查属性中的可选项.此外,返回的排队单元格也是可选的,因此请确保对其进行可选的强制转换UITableViewCell.之后,我们可以强行打开包装,因为我们知道我们有一个单元格.

    var cell:UITableViewCell? = 
    tableView?.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell
    if (cell == nil)
    {
       cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, 
                    reuseIdentifier: reuseIdentifier)
    }
    // At this point, we definitely have a cell -- either dequeued or newly created,
    // so let's force unwrap the optional into a UITableViewCell
    cell!.detailTextLabel.text = "some text"
    
    return cell
    

    2023-01-11 14:44 回答
  • 基本上与其他答案相同,但我通过使用计算变量处理讨厌的选项(你不能nil-tableView:cellForRow:atIndexPath:Swift中返回):

    斯威夫特3

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell: UITableViewCell = {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell") else {
                // Never fails:
                return UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "UITableViewCell")
            }
            return cell
        }()
    
        // (cell is non-optional; no need to use ?. or !)
    
        // Configure your cell:
        cell.textLabel?.text       = "Key"
        cell.detailTextLabel?.text = "Value"
    
        return cell
    }
    

    编辑:

    其实,这将是更好的使用出列的单元格:tableView.dequeueReusableCell(withIdentifier:for:)不是.

    如果没有人可以重用(这正​​是我的代码在上面明确指出的那样),函数的后来变量会自动实例化一个新单元格,因此永远不会返回nil.

    2023-01-11 14:44 回答
  • 如果你宁愿避免选择性,你可以创建一个UITableViewCell的子类,如下所示:

    class SubtitleTableViewCell: UITableViewCell {
    
        override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
            super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    然后注册它:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(SubtitleTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
    }
    

    这允许您的单元格自定义代码非常好:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
    
        cell.textLabel?.text = "foo"
        cell.detailTextLabel?.text = "bar"
    
        return cell
    }
    

    2023-01-11 14:45 回答
  • 通过清理Swift 2风格来构建memmons的答案......

    let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) ?? UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
    
    cell.detailTextLabel?.text = "some text"
    
    return cell
    

    斯威夫特3:

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) ?? UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
    
    cell.detailTextLabel?.text = ""
    
    return cell
    

    2023-01-11 14:45 回答
  • 由于tableView.dequeueReusableCell(withIdentifier:, for:)返回非零单元格,因此if cell == nil检查始终为false。但是我找到了一个解决方案,使默认样式单元格成为您想要的样式(值1,值2或字幕),因为默认样式单元格detailTextLabel为nil,因此请检查其detailTextLabel是否为nil,然后创建新样式单元格,并将其交给出队格, 喜欢:

    斯威夫特3:

    var cell = tableView.dequeueReusableCell(withIdentifier: yourCellReuseIdentifier, for: indexPath)
    
    if cell.detailTextLabel == nil {
        cell = UITableViewCell(style: .value1, reuseIdentifier: repeatCellReuseIdentifier)
    }
    
    cell.textLabel?.text = "Title"
    cell.detailTextLabel?.text = "Detail"
    
    return cell
    

    那对我有用。

    希望对您有所帮助。

    2023-01-11 14:45 回答
  • func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let reuseIdentifier = "cell"
        var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as UITableViewCell?
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
        }
        cell!.textLabel?.text = self.items[indexPath.row]
        cell!.detailTextLabel?.text = self.items[indexPath.row]
        return cell!
    }
    

    2023-01-11 14:47 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有