gpt4 book ai didi

swift - 来自 Xib 和 Swift 3 的 Cell

转载 作者:IT王子 更新时间:2023-10-29 05:24:40 24 4
gpt4 key购买 nike

我已经阅读并看过一些关于这个主题的视频,但我无法让它发挥作用。我正在尝试从 xib 而不是 Storyboard 中的单元格。

这是我的 ViewController(我有表格 View )。

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 5
}

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

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

cell.test.text = "A";

return cell;

}

}

这是我的 CellTableView(我有我的 Cell 的类):

import UIKit

class CellTableView: UITableViewCell {

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

@IBOutlet var teste: UILabel!

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

我一直在 StoryBoard 上的 TableView 中使用 TableView 单元格,但我现在想尝试 XIB 方法。

我在单元格 XIB 上添加了 单元格 标识符,以使用 dequeueReusableCell 方法调用它。

我可能做错了什么?我尝试 Outlet dataSource 和 Table View 的 delegate,但出现错误(我认为这不是正确的步骤)。

最佳答案

您需要注册您的 nib 对象。

viewDidLoad() 中:

let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)

tableView.register(nib, forCellReuseIdentifier: "yourIdentifier")

您还需要返回部分的数量:

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

关于swift - 来自 Xib 和 Swift 3 的 Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40272624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com