gpt4 book ai didi

ios - 错误 : "unable to dequeue a cell with identifier Cell"

转载 作者:行者123 更新时间:2023-11-29 01:38:43 26 4
gpt4 key购买 nike

我正在使用以下 viewController 设置

enter image description here

我想要做的是从 placesTabBar 访问地点的 array 并在两个 navigationControllers 中显示它们。其中一个 Controller 将包含一个地点列表,而另一个将仅用于您正在参加的地点。在选择一个单元格时,它会在切换时更新另一个 View Controller ,说你要去那个地方。我正在使用以下代码:

AllPlaces ViewController

class AllPlaces: UITableViewController {

var delegate:placesTabBar!

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return delegate.placeDataArray.count
}


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

cell.textLabel?.text = delegate.placeDataArray[indexPath.row].description

return cell
}

}

AttendingPlaces ViewController 类 AttentingPlaces:UITableViewController {

var delegate:placesTabBar!

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

return delegate.placeDataArray.count
}


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

cell.textLabel?.text = delegate.placeDataArray[indexPath.row].description
return cell
}

标签栏 Controller

class placeData: Equatable {
var description : String
var selected : Bool

init (description : String, selected : Bool) {
self.description = description
self.selected = selected
}
}
class placesTabBar: UITabBarController, CustomTabBarDelegate {

var placeDataArray = Array<placeData>()

override func viewDidLoad() {

placeDataArray = [placeData(description: "Afghanistan", selected: false), placeData(description: "Albania", selected: false)]

var table1 = AttendingPlaces()
var table2 = AllPlaces()

table1.delegate = self
table2.delegate = self

var navController1 = UINavigationController(rootViewController: table1)
var navController2 = UINavigationController(rootViewController: table2)

self.viewControllers = [navController1, navController2]

}



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

现在我只是想用数组中的两个位置加载表。但是,我收到“无法使具有标识符 Cell 的单元格出队”错误。重用标识符在两个 tableViewControllers 中都设置正确,所以我不确定发生了什么。这可能是我试图在两个 viewControllers 之间共享数据的方式吗?

最佳答案

您收到该错误是因为您正在创建两个 TableView Controller 的新实例(例如 table1 = attendingPlaces()),它们与 Storyboard无关,因此不会“了解” Storyboard中设置的标识符。

您应该能够获取对与 tabBarController 同时实例化的现有 TableView Controller 的引用:

var nav1 = self.viewControllers[0] as! UINavigationController
var table1 = nav1.topViewController as! AttendingPlaces
var nav2 = self.viewControllers[1] as! UINavigationController
var table2 = nav2.topViewController as! AllPlaces

table1.delegate = self
table2.delegate = self

关于ios - 错误 : "unable to dequeue a cell with identifier Cell",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672630/

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