2015-06-03 33 views
1

我有一個數組,我從後端獲得其值,並且我有一個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格,它被移動到桌子的底部!

+0

當我運行你的代碼時,我沒有得到任何錯誤,但它沒有像我想要的那樣填充單元格。你從上到下得到的是5,5,3,3,「第2節」。您不能像使用循環一樣使用循環,因爲這隻會爲該節中的每個單元格提供最後一個循環索引(索引2或4)中的值。 – rdelmar

回答

3

您不能像使用循環一樣使用循環,因爲這隻會爲該節中的每個單元格提供最後一個循環索引(索引2或4)中的值。通常,對於分區表視圖,您可以使用數組數組來填充這些部分。如果你想使用一個單一的陣列,並且在每個部分的行留像你展示他們的號碼,那麼下面應該工作,

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    var titles = ["1", "2", "3", "4", "5"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 3 
    } 

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

     if section == 0 { 

      return 3 
     } 
     else if section == 1 { 

      return 1 

     } else { 

      return 2 
     } 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let myCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 

     if indexPath.section == 0 { 
      myCell.textLabel!.text = titles[indexPath.row] 
     } 
     else if indexPath.section == 1 { 
      myCell.textLabel!.text = "Section 2" 
     } 
     else { 
      myCell.textLabel!.text = titles[indexPath.row + 3] 
     } 

     return myCell 
    } 

} 

這給了1,2,3,「第2」,4 ,對於六個單元格(我爲測試用例使用了標準的UITableViewCell)。

+0

@ Abdou023我不知道你的意思是「從底部索引到頂部「 - 他們不是。如果使用else子句中的第一條註釋掉的行,會得到什麼確切的錯誤? – rdelmar

+0

我得到同樣的錯誤,我走的任何線路:「致命錯誤:數組索引超出範圍」 – Abdou023

+0

@ Abdou023那麼你的數組肯定不是你說的那樣。如果你登錄'titles',它會給你什麼? – rdelmar

1

你的數組有5個值,但你的代碼表明你有6個。查看你的tableView(tableView: UITableView, numberOfRowsInSection section: Int)

+0

這是故意的,中間部分的額外單元格應該從不同的來源獲取其數據。 – Abdou023

+0

啊好的。那麼當你嘗試檢索你的values數組的數據時,你是否考慮了缺失的行索引? –

+0

我的猜測是你想從你的值數組中拉出一個值在索引5,但你的值數組沒有索引5. –

相關問題