【发布时间】:2015-12-16 02:30:11
【问题描述】:
我编写应用程序只是为了好玩和改进。
我的问题如下:
在我的应用程序的 ViewController 中,我有 3 个 tableViews 共享 dataSource 和委托(它自己的 viewController)。我每个都做了 3 个 Outlets,这样我可以通过对象引用比较 (tableView === outletTableView) 来识别 numberOfRowsInSection 和 cellForRowAtIndexPath 中返回的值。
每次都返回正确的值(通过一些打印调用检查),但设备似乎将一个 tableView 中的 numberOfRows 用于 3 个 tableViews !
我的第一个有 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 ?
【问题讨论】:
-
如果共享一个公共数据源,行数怎么会不同?
-
你能给出详细的实现吗?我认为问题是您的每种表格的代码都错误吗?
-
你能贴出你的代码吗?