3 具有通用数据源和委托的 TableViews - 行数错误
3 TableViews with common dataSource and delegate - Wrong number of rows
我编写应用程序只是为了好玩和改进。
我的问题如下:
在我的应用程序的 ViewController 中,我有 3 个 tableViews 共享数据源和委托(viewController 它自己)。我为每个 Outlet 做了 3 个 Outlet,这样我就可以通过对象引用比较 (tableView === outletTableView
) 来识别 numberOfRowsInSection
和 cellForRowAtIndexPath
中的值 return。
正确的值每次都是 returned(通过一些打印调用进行检查)但是设备似乎使用一个 tableView 中的 numberOfRows 作为 3 个 tableView!
我的第一个有 13 行,第二个有 21 行,第三个只有 1 行。当加载第二个 tableView 的第 14 行时,应用程序崩溃并出现此错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0xc000000001a00016> {length = 2, path = 0 - 13})'
我检查了很多主题,但我还没有找到任何解决方案...
谢谢
这是我的代码:
@IBOutlet weak var mainSkillTableHeight: NSLayoutConstraint!
@IBOutlet weak var secondarySkillTableHeight: NSLayoutConstraint!
@IBOutlet weak var exoticSkillTableHeight: NSLayoutConstraint!
[...]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (tableView === mainSkillsTable) {
print("CM \(character.main_skills.count + 1)")
return character.main_skills.count + 1
}
else if (tableView === secondarySkillsTable) {
print("CS \(character.secondary_skills.count + 1)")
return character.secondary_skills.count + 1
}
else if (tableView === exoticSkillsTable) {
print("CE \(character.exotic_skills.count + 1)")
return character.exotic_skills.count + 1
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell1: CreationMainSkillCell?
var cell2: CreationSecondarySkillCell?
var cell3: CreationExoticSkillCell?
if (tableView === mainSkillsTable) {
if indexPath.row == 0 {
return mainSkillsTable.dequeueReusableCellWithIdentifier("mainHeader", forIndexPath: indexPath) as! HeaderMainSkillCell
}
cell1 = mainSkillsTable.dequeueReusableCellWithIdentifier("mainDataC", forIndexPath: indexPath) as? CreationMainSkillCell
cell1!.character = character
cell1!.row = indexPath.row
return cell1!
}
else if (tableView === secondarySkillsTable) {
if indexPath.row == 0 {
return mainSkillsTable.dequeueReusableCellWithIdentifier("secondHeader", forIndexPath: indexPath) as! HeaderSecondarySkillCell
}
cell2 = mainSkillsTable.dequeueReusableCellWithIdentifier("secondDataC", forIndexPath: indexPath) as? CreationSecondarySkillCell
cell2!.character = character
cell2!.row = indexPath.row
return cell2!
}
else if (tableView === exoticSkillsTable) {
if indexPath.row == 0 {
return mainSkillsTable.dequeueReusableCellWithIdentifier("exoticHeader", forIndexPath: indexPath) as! HeaderExoticSkillCell
}
cell3 = mainSkillsTable.dequeueReusableCellWithIdentifier("exoticDataC", forIndexPath: indexPath) as? CreationExoticSkillCell
cell3!.character = character
cell3!.row = indexPath.row
return cell3!
}
return UITableViewCell()
}
numberOfRows 没有链接到特定的 tableView?
嗯,您的代码似乎有问题。当数据源相同时,3 table 不可能有不同的 numberOfRows。您显然正在更改传递给 tableView 的数组。
检查最初加载视图时返回的 numberOfRows。每次你对任何一个 table 进行更改时,你都需要反映 "datasource" 数组而不是本地数组。您似乎正在使用中间数组,因为您在显示 numberOfRows 时不一致。
我编写应用程序只是为了好玩和改进。
我的问题如下:
在我的应用程序的 ViewController 中,我有 3 个 tableViews 共享数据源和委托(viewController 它自己)。我为每个 Outlet 做了 3 个 Outlet,这样我就可以通过对象引用比较 (tableView === outletTableView
) 来识别 numberOfRowsInSection
和 cellForRowAtIndexPath
中的值 return。
正确的值每次都是 returned(通过一些打印调用进行检查)但是设备似乎使用一个 tableView 中的 numberOfRows 作为 3 个 tableView!
我的第一个有 13 行,第二个有 21 行,第三个只有 1 行。当加载第二个 tableView 的第 14 行时,应用程序崩溃并出现此错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0xc000000001a00016> {length = 2, path = 0 - 13})'
我检查了很多主题,但我还没有找到任何解决方案...
谢谢
这是我的代码:
@IBOutlet weak var mainSkillTableHeight: NSLayoutConstraint!
@IBOutlet weak var secondarySkillTableHeight: NSLayoutConstraint!
@IBOutlet weak var exoticSkillTableHeight: NSLayoutConstraint!
[...]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (tableView === mainSkillsTable) {
print("CM \(character.main_skills.count + 1)")
return character.main_skills.count + 1
}
else if (tableView === secondarySkillsTable) {
print("CS \(character.secondary_skills.count + 1)")
return character.secondary_skills.count + 1
}
else if (tableView === exoticSkillsTable) {
print("CE \(character.exotic_skills.count + 1)")
return character.exotic_skills.count + 1
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell1: CreationMainSkillCell?
var cell2: CreationSecondarySkillCell?
var cell3: CreationExoticSkillCell?
if (tableView === mainSkillsTable) {
if indexPath.row == 0 {
return mainSkillsTable.dequeueReusableCellWithIdentifier("mainHeader", forIndexPath: indexPath) as! HeaderMainSkillCell
}
cell1 = mainSkillsTable.dequeueReusableCellWithIdentifier("mainDataC", forIndexPath: indexPath) as? CreationMainSkillCell
cell1!.character = character
cell1!.row = indexPath.row
return cell1!
}
else if (tableView === secondarySkillsTable) {
if indexPath.row == 0 {
return mainSkillsTable.dequeueReusableCellWithIdentifier("secondHeader", forIndexPath: indexPath) as! HeaderSecondarySkillCell
}
cell2 = mainSkillsTable.dequeueReusableCellWithIdentifier("secondDataC", forIndexPath: indexPath) as? CreationSecondarySkillCell
cell2!.character = character
cell2!.row = indexPath.row
return cell2!
}
else if (tableView === exoticSkillsTable) {
if indexPath.row == 0 {
return mainSkillsTable.dequeueReusableCellWithIdentifier("exoticHeader", forIndexPath: indexPath) as! HeaderExoticSkillCell
}
cell3 = mainSkillsTable.dequeueReusableCellWithIdentifier("exoticDataC", forIndexPath: indexPath) as? CreationExoticSkillCell
cell3!.character = character
cell3!.row = indexPath.row
return cell3!
}
return UITableViewCell()
}
numberOfRows 没有链接到特定的 tableView?
嗯,您的代码似乎有问题。当数据源相同时,3 table 不可能有不同的 numberOfRows。您显然正在更改传递给 tableView 的数组。
检查最初加载视图时返回的 numberOfRows。每次你对任何一个 table 进行更改时,你都需要反映 "datasource" 数组而不是本地数组。您似乎正在使用中间数组,因为您在显示 numberOfRows 时不一致。