gpt4 book ai didi

ios - UILabel 未使用 UITableViewCell 自定义类 Swift 进行更新

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:58 24 4
gpt4 key购买 nike

我是 Swift 编程的新手,所以不要认为我很强。我花了很多时间试图了解问题出在哪里,但我无法理解。

我在 UIViewController 中有一个 UITableView

UIViewController:

@IBOutlet weak var myTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
myTableView.delegate = self
myTableView.dataSource = self
}



func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 10
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell

cell.createNewCell()
return cell
}

在我的自定义 UITableViewCell 类中,我有 myLabel - 我在 Storyboard 中附加的标签。

@IBOutlet weak var myLabel: UILabel!
func createNewCell(){
myLabel.text = "1234567"
}

但是,如果我在单元格更新时将以下行 cell.textLabel!.text = "12312" 添加到 cellForRowAtIndexPath 中,则单元格不会在我尝试更新时更新通过自定义 UITableViewCell 类更新它们。

最佳答案

我认为你应该这样做:

1) 创建您的自定义单元格

class customCell: UITableViewCell
{
@IBOutlet weak var myLabel: UILabel!
}

2) 在 cellForRowAtIndexPath 中设置可重用单元格的标识符

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! customCell
cell.myLabel.text = "1234"
return cell

关于ios - UILabel 未使用 UITableViewCell 自定义类 Swift 进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37841132/

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