2017-09-04 85 views
0

我在我的表格視圖中有兩個自定義的可重用表格視圖單元格。第一個細胞,我希望它始終在場。第二個單元以及之後,正在返回從mysql數據庫傳遞的計數。在tableview中顯示兩個可重用的單元格Swift 3

// return the amount of cell numbers 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return posts.count 
    } 


// cell config 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.row < 1 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! InfoCell 
     //set the data here 
     return cell 

    } else { 

    let Postcell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostCell 

     let post = posts[indexPath.row] 
     let image = images[indexPath.row] 
     let username = post["user_username"] as? String 
     let text = post["post_text"] as? String 


     // assigning shortcuts to ui obj 
     Postcell.usernameLbl.text = username 
     Postcell.textLbl.text = text 
     Postcell.pictureImg.image = image 

     return Postcell 

    } 

} // end of function 

我的第一個細胞是存在的,所以是post.count,但由於某些原因,posts.count缺少一個職位,我相信這是因爲第一個單元格中。任何人都可以幫助我嗎?提前致謝。

回答

1

您需要調整從numberOfRowsInSection返回的值以解釋額外的行。而且您需要調整用於訪問posts數組中值的索引來處理額外的行。

但是更好的解決方案是使用兩個部分。第一部分應該是您的額外行,第二部分應該是您的帖子。

func numberOfSections(in tableView: UITableView) -> Int { 
    return 2 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0 { 
     return 1 
    } else { 
     return posts.count 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! InfoCell 
     //set the data here 

     return cell 
    } else { 
     let Postcell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostCell 

     let post = posts[indexPath.row] 
     let image = images[indexPath.row] 
     let username = post["user_username"] as? String 
     let text = post["post_text"] as? String 


     // assigning shortcuts to ui obj 
     Postcell.usernameLbl.text = username 
     Postcell.textLbl.text = text 
     Postcell.pictureImg.image = image 

     return Postcell 
    } 
} 
相關問題