2017-09-04 11 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 
    } 
} 
관련 문제