gpt4 book ai didi

uitableview - 为什么 tableview 委托(delegate)不起作用?

转载 作者:行者123 更新时间:2023-11-30 10:17:50 26 4
gpt4 key购买 nike

我在单 View 应用程序中有以下类(class)。在IB中,我已将数据源和数据委托(delegate)连接到 TableView 。我做错了什么导致没有一个 tableview 委托(delegate)被触发吗?

小区标识符在IB中设置。

import UIKit

class ViewController: UITableViewController {

let myarray = ["item 1", "item 2", "item 3", "item 4"]
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.
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "test")
cell.textLabel?.text = myarray[indexPath.item]
return cell;
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}

}

IB 中已经有一个原型(prototype)单元。

另一件事,viewDidLoad() 甚至没有触发。

最佳答案

您应该将 cellForRowAtIndexPath 中的单元格出队。在 Storyboard 中,如果表中还没有 UITableViewCell,则将其添加到表中,您应该会看到一个原型(prototype)单元格。确保原型(prototype)的标识符是“test”,就像在代码中一样。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("test") as UITableViewCell

cell.textLabel?.text = self.myarray[indexPath.row]

return cell
}

关于uitableview - 为什么 tableview 委托(delegate)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29198445/

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