2015-10-19 45 views
1

我有一個用2個原型單元格創建的UITableView,每個原型單元格都有獨立的標識符和子類。具有多個原型單元格的Swift TableView不顯示所有行

我的問題是當我顯示細胞的第一個原型單元格的第二個原型的第一行被吸收。

例如,我有第一個原型單元格只顯示1項。第二個原型單元應顯示4個項目。但是,第二個原型單元格中的第一項不顯示,相反,只有三個可見的四個項目。

這裏是我的實現代碼:

override func viewDidLoad() { 
super.viewDidLoad() 


    self.staticObjects.addObject("Please...") 
    self.objects.addObject("Help") 
    self.objects.addObject("Me") 
    self.objects.addObject("Thank") 
    self.objects.addObject("You") 

    self.tableView.reloadData() 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

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

    return self.objects.count 
} 

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

    if(indexPath.row==0){ 

     let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell 

     staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String 

     return staticCell 

    } 

    else{ 

     let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell 

     cell.titleLabel.text = self.objects.objectAtIndex(indexPath.row) as? String 


     return cell 

    } 

感謝所有幫助。

回答

1

你有邏輯問題與你如何兩個tableView:numberOfRowsInSectiontableView:cellForRowAtIndexPath計數在錶行數。

您的代碼產生顯示輸出如下所示,其中:

  • 藍色細胞代表你的staticCell原型表格單元視圖;這些是來自staticsObjects陣列的值。
  • 黃色單元格表示您的cell原型表格單元格視圖;這些是來自objects陣列的值。

Bad Output of Table View


1.看的tableView:的cellForRowAtIndexPath:

cellForRowAtIndexPath方法,你只返回objects陣列的數量。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {   
    return self.objects.count 
} 

這意味着,行數,你會在你的桌子會,而不是5.相反,你想回到你正在使用你的表格中兩個數組的總和:objects.count + staticObjects.count。例如:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return objects.count + staticObjects.count 
} 

2.看的tableView:的cellForRowAtIndexPath:

這裏是你原來的代碼與我的意見..

// You are assuming that `staticObjects` will always have 
// exactly one row. It's better practice to make this 
// calculation more dynamic in case the array size changes. 
if (indexPath.row==0){ 

    let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell 
    staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String 
    return staticCell    
} else {    
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell 
    // Here's your problem! You need to calculate the row 
    // because you want to put the objects from your other 
    // array first. As a result, you need to account for them. 
    cell.titleLabel.text = self.objects.objectAtIndex(indexPath.row) as? String 
    return cell  
} 

現在,這裏有一個方法來解決上述討論指出你的錯誤:

// If the indexPath.row is within the size range of staticObjects 
// then display the cell as a "staticCell". 
// Notice the use of "staticObjects.count" in the calculation. 
if indexPath.row < staticObjects.count { 
    let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell 
    staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String 
    return staticCell 
} else { 
    // If you get here then you know that your indexPath.row is 
    // greater than the size of staticObjects. Now you want to 
    // display your objects values. 
    // You must calculate your row value. You CANNOT use the 
    // indexPath.row value because it does not directly translate 
    // to the objects array since you put the staticObjects ahead 
    // of them. As a result, subtract the size of the staticObjects 
    // from the indexPath.row.   
    let row = indexPath.row - staticObjects.count 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell 
    cell.titleLabel.text = self.objects.objectAtIndex(row) as? String     
    return cell 
} 

現在你應該看到這一點:

Correct Display of Table View

+0

感謝這個幫助。它是完全有道理的,我感謝你的努力。我在離開一段時間後發現問題。再次感謝。 – Rico

+0

@Rico不客氣。很高興聽到你明白了;逐步遠離問題總是有幫助的。 :) – whyceewhite

+0

它是怎麼回事我實現了上面的代碼,並通過打印出迄今爲止的5(這是正確的)的計數做了一些檢查。一切工作,直到我實現計數邏輯,它將兩個細胞計數加在一起。我收到一個NSException和一個SIGABRT信號。不知道爲什麼會這樣。對我來說,似乎計數是超載或溢出。你有什麼想法嗎?感謝 – Rico

相關問題