我有一个问题:
当我选择tableView的第二个单元格时,我得到一个SIGABRT。但是当我选择第一个时,一切都很好。
它必须与我的TableViewController类有关,因为“SET ROWS IN SECTION”(来自我的print语句)将被打印3次,当我选择第2个单元格时,但是3 * 4 = 12次,当我选择第一个单元格时... ...哦......我收到这个错误:
GONNA SET THE ROWS IN SECTION
2
SET SECTION 2 TO 1
GONNA SET THE ROWS IN SECTION
0
SET SECTION 0 TO 1
GONNA SET THE ROWS IN SECTION
1
SET SECTION 1 TO 3
2016-07-04 12:43:14.407 Sessions[1229:629535] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x1822e6db0 0x18194bf80 0x1821c7098 0x187b7d5ac 0x187933f1c 0x18756d444 0x18752aff8 0x1878e4b88 0x187529fc4 0x187529d5c 0x187529b68 0x187794a20 0x18752fac8 0x18743fac8 0x187465350 0x18743ed40 0x182c18cc0 0x18743ebc4 0x18744c678 0x18763edbc 0x18743fac8 0x18743f7ac 0x18743ed40 0x182c18cc0 0x18743ebc4 0x18744c678 0x1876df8c0 0x1874535b4 0x18763e8d4 0x18775e334 0x18763e44c 0x1875f8818 0x187503e40 0x187503b1c 0x187503a84 0x1874401e4 0x184dd2994 0x184dcd5d0 0x184dcd490 0x184dccac0 0x184dcc820 0x1874365f8 0x18229c728 0x18229a4cc 0x18229a8fc 0x1821c4c50 0x183aac088 0x1874ae088 0x1000f8b54 0x181d628b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
“SET SECTION 2 TO 1”表示第2节有1个单元格...
这是我的班级:
import UIKit
class SessionsTableViewController: UITableViewController {
let header = ["Mediatoren", "Medianden", "Konfliktverlauf"]
var medianden: Array<String> = []
var mediators: Array<String> = []
// ----------------
// MARK : IBACTIONS
// ----------------
(I just deleted these...)
// ---------------------
// MARK : OVERRIDE FUNCS
// ---------------------
override func viewDidLoad() {super.viewDidLoad() }
override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning()}
override func viewWillAppear(animated: Bool) {
print(sessions.workingOnSession)
medianden.append((sessions.workingOnSession["Name1"] as? String)!)
medianden.append((sessions.workingOnSession["Name2"] as? String)!)
medianden.append((sessions.workingOnSession["Name3"] as? String)!)
medianden.append((sessions.workingOnSession["Name4"] as? String)!)
//print(medianden)
for _ in 0...3 {
if medianden.last == "" {
medianden.removeLast()
}
}
print(medianden)
mediators.append((sessions.workingOnSession["MID"] as? String)!)
mediators.append((sessions.workingOnSession["MID2"] as? String)!)
//print(mediators)
for _ in 0...1 {
if mediators.last == "" {
mediators.removeLast()
}
}
print(mediators)
}
// ---------------------
// MARK : FUNCS
// ---------------------
(Deleted these too...)
// -----------------
// MARK : TABLE VIEW
// -----------------
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.header[section]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("SET ROWS IN SECTION")
print(section)
if section == 0 {
print("SET SECTION 0 TO \(mediators.count)")
return mediators.count
} else if section == 1 {
print("SET SECTION 1 TO \(medianden.count)")
return medianden.count
} else if section == 2 {
print("SET SECTION 2 TO 1")
return 1
}
return 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("MAKE MY CELLS...")
let cell = UITableViewCell()
if indexPath.section == 0 {
cell.detailTextLabel?.text = mediators[indexPath.row]
return cell
} else if indexPath.section == 1 {
cell.textLabel!.text = medianden[indexPath.row]
return cell
} else if indexPath.section == 2 {
let textCell = FSTextViewTableViewCell()
return textCell
//textCell.textView.text = sessions.workingOnSession["Description"] as? String
}
return cell
}
}
谢谢大家! :-)抱歉我的英语不好......
费边答案 0 :(得分:0)
错误信息非常清楚。
uncaught exception 'NSRangeException',
reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
所以你有一个包含元素[0..1]的数组,你可以访问元素[2]。您的计划中只有这么多数组:mediators
,medianden
,self.header
。尝试多做一些调试,看看哪一个有问题。
您似乎假设mediators
有2个元素且mediaden
有4个,这是硬编码的。最好不要在整个代码中撒上这些依赖项;相反,设置一次大小,并使代码的其余部分独立于大小(使用for循环等)。