如何在具有 dequeueReusableCell 的同时以编程方式设置 tableview 样式以具有字幕?

Posted

技术标签:

【中文标题】如何在具有 dequeueReusableCell 的同时以编程方式设置 tableview 样式以具有字幕?【英文标题】:How to set tableview style to have subtitle programmatically while having dequeueReusableCell? 【发布时间】:2019-11-18 05:31:02 【问题描述】:

我希望我的 tableView 有字幕并且能够正确出列。我已经提到了这个link,但它不适用于我的代码。我该怎么办?

我的代码目前是这样的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    
        //Calling tableview for a reusable cell here will always return a cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = developerArray[indexPath.row].developerName
        cell.detailTextLabel?.text = developerArray[indexPath.row].developerHP

        return cell
    

【问题讨论】:

您可能需要将您的单元格转换为可选的 UITableViewCell 仔细检查你为 tableView 注册了你的单元格:tableview.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 我认为您正在使用嵌入在 Storyboard 中的 UITableView 中的 UITableViewCell。你能把这个 UITableViewCell 类型改成字幕并确认结果吗? 【参考方案1】:

斯威夫特 5

//声明可变单元格标识符

let reuseCellIdentifier = “cellIdentifier”;

//cellForRowAt的实现

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

    var cell = tableView.dequeueReusableCell(withIdentifier: reuseCellIdentifier)
    if (!(cell != nil)) 

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

    cell?.textLabel?.text = //Title text
    cell?.detailTextLabel?.text = //Subtitle text

    return cell!

【讨论】:

代码编译成功,但没有显示字幕。我做错了什么? textLabel 怎么样。它显示在您的屏幕上。 在代码 UITableViewCell(style: .subtitle, reuseIdentifier:reuseCellIdentifier) 行中用 .value2 替换 .subtitle 做一件事。 您是否在 UITableview 中设置了任何其他属性,因为这些东西不起作用请检查? 让我们continue this discussion in chat.【参考方案2】:

您可以通过添加标签使用 nib 创建自定义单元格

要创建自定义单元格,请参阅此链接: Custom UITableViewCell from nib in Swift

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

    let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell

    cell.title.text = yourTitleArray[indexPath.row]
    cell.detailLbl.text = yourDetailArray[indexPath.row]

    return cell

【讨论】:

【参考方案3】:

首先,您是使用故事板还是代码创建表格的?

无论哪种方式,您都需要确保将数据源和委托设置为 self,前提是它们所在的类符合: UITableViewDataSource 和 UITableViewDelegate

myTable?.delegate = self
myTable?.dataSource = self

还要确保您注册了您的手机

myTable?.register(myCell.self, forCellReuseIdentifier: "myCell")

并且在声明你的单元格时,你需要强制它为类型

let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! myCell

下面是带有创建单元格的 tableview 的工作示例。我希望这会有所帮助。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 

    // Create the tableview object
    var myTable:UITableView?

    override func viewDidLoad() 
        // Set the size and location of the tableview
        myTable = UITableView(frame: CGRect(x: 0, y: 0, width: 300, height: 600))
        // register your cell
        myTable?.register(myCell.self, forCellReuseIdentifier: "myCell")
        // set the background color of the table, note this wont make a difference unless the cell background is changed.
        myTable?.backgroundColor = UIColor.clear
        // set the datasource and delegate to self
        myTable?.delegate = self
        myTable?.dataSource = self
        // This is jsut for style, wether there should be seperators or not, and if the user can select multiple lines
        myTable?.separatorStyle = .none
        myTable?.allowsMultipleSelection = true
        // Add the table to your view
        self.view.addSubview(myTable!)
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        // This is declaring how many rows you want in your table. I have 1 but you can do it according to the size of your array.
        return 1
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        // create the cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! myCell

        // set the title text for this cell
        cell.title.text = "HelloWorld"

        // return the cell
        return cell
    

这是我们上面引用的单元格的类。

class myCell: UITableViewCell 


    var title = UILabel()
    var detail = UILabel()


    override func awakeFromNib() 
        super.awakeFromNib()
    



    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) 
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        title.font = UIFont.boldSystemFont(ofSize: 16)
        title.textAlignment = .center
        self.contentView.addSubview(title)
        self.contentView.addSubview(detail)

    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)
    


    override func layoutSubviews() 
        super.layoutSubviews()
        title.frame = CGRect(x: self.contentView.frame.width / 2 - 200, y: 6, width: 400, height: 20)
        detail.frame = CGRect(x: 150, y: 10, width: 280, height: 20)
    

如果这有帮助,请告诉我。如果你是从故事板上做的,请告诉我,我会调整的。

【讨论】:

以上是关于如何在具有 dequeueReusableCell 的同时以编程方式设置 tableview 样式以具有字幕?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Laravel 中使非数据库模型具有两个属性

如何在laravel中搜索具有数组属性的对象数组

如何在 Laravel 中查询具有精确指定关系的数据集?

如何在 Laravel Eloquent 中构建具有多级关系的查询

如何在 Destroy 函数中删除具有多个参数的数据 - Laravel

如何使用 Postgres jsonb '? Laravel 中具有索引支持的运算符?