我有一個數組,我從後端獲得其值,並且我有一個UITableView
用3段和不同的行:的UITableView - 在部分不同的行與同一陣列
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 3
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if section == 1 {
return 2
}
else if section == 2 {
return 1
} else {
return 3
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell
// Configure the cell...
if indexPath.section == 1 {
myCell.titleLabel.text = titles[indexPath.row + 3]
}
else if indexPath.section == 2 {
myCell.textLabel?.text = "Section 2"
}
else {
myCell.titleLabel.text = titles[indexPath.row] // ERROR!
}
return myCell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section"
}
我需要有細胞從2個不同的部分獲取他們的數據來自同一個數組,我有一箇中間的部分,它應該從不同的數組中獲取數據。當我運行我得到這個錯誤:
fatal error: Array index out of range
我的數組包含5個值,它是這樣的:
titles = ["1", "2", "3", "4", "5"]
PS:我有定製單元爲我的TableView
編輯:我編輯部分的安排(第一次我知道他們是從下到上索引的),但是我仍然得到和錯誤的最後一節單元格,還有一個非常奇怪的行爲,當我向下滾動然後返回時,第2節將取代第1部分的第3格,它被移動到桌子的底部!
當我運行你的代碼時,我沒有得到任何錯誤,但它沒有像我想要的那樣填充單元格。你從上到下得到的是5,5,3,3,「第2節」。您不能像使用循環一樣使用循環,因爲這隻會爲該節中的每個單元格提供最後一個循環索引(索引2或4)中的值。 – rdelmar