2017-09-04 88 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 
    } 
} 
相关问题