【问题标题】:Connect Prototype Cells to View Controller [Swift 4]将原型单元连接到视图控制器 [Swift 4]
【发布时间】:2018-12-06 07:32:08
【问题描述】:

我是编程新手,目前正在开发类似应用程序的新闻源。我有一个正常的表格视图并运行良好,但现在想尝试使用服装单元格类型。所以我创建了一个,并认为以通常的方式连接标签会非常好,但我错了。所以我想知道如何让我的文本标签连接到我的视图控制器,这样我就可以使用我的自定义单元格。

class ViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var newsfeedTableView: UITableView!

var ref: DatabaseReference!

var posts = [String]()


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (posts.count)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")

//here is where I need the custom label to get the posts 
    cell.textLabel?.text = posts[indexPath.row]
    cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)

    return cell
 }
}

【问题讨论】:

标签: swift uitableview


【解决方案1】:

创建UITableViewCell 的子类并将IBOutlets 连接到该类

class YourCell: UITableViewCell {
    @IBOutlet weak var customLabel: UILabel!
    ...
}

不要忘记在情节提要中设置原型单元的类:

然后在cellForRowAt 数据源方法中将您的出列单元格向下转换为YourCell

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

那么你就可以访问YourCell outlets

cell.customLabel.text = "SomeText"
...

【讨论】:

    【解决方案2】:

    我假设您正在使用 Storyboard。

    首先,您应该明白,当您使用自己的自定义表格单元格时,差别不大。在这种情况下,在“cellForRowAtIndexPath”方法中,在将单元格出列后,您只需将表格单元格类型转换为 'as!你的CustomTableCellClass'。在此行之后,您可以访问该类的每个属性。

    首先,在 Storyboard 上随意设计表格单元格。

    现在,创建 UITableViewCell 的子类并将该类分配给您在 Storyboard 上设计的原型自定义单元格。另外,不要忘记在 Storyboard 表格单元格中设置“重用标识符”。

    然后将您的插座与 Storyboard 中的自定义单元格类连接起来。

    现在您可以使用如下代码:

     class YourTableCellClass: UITableViewCell {
    
    // I'm using these outlets as a sample, but you have to connect them from Storyboard.
    var leftTextLabel: UILabel!
    var rightTextLabel: UILabel!
    }
    
    class YourTableController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    // MARK: - TableView Delegate & DataSource
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1 // return your number of rows here...
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100 // return the height for the row here.....or you can set height from Storyboard if you have fix height of all rows.
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! YourTableCellClass
        cell.leftTextLabel.text = "Text" // Set text here....
        cell.rightTextLabel.text = "Text" // Set text here....
        return cell
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      相关资源
      最近更新 更多