2016-10-19 5 views
0

셀 당 높이도 변경 되었기 때문에 UITableView 사용자 지정 셀을 만들고 있습니다.Swift3와 ios10에서 UITableView 사용자 지정 셀 겹치기

이것은 셀을 초기화하는 코드이며, 그 후에 UITextView를 Subview로 추가하려고합니다.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
    let dictAd = self.arrAllQuestions[indexPath.row] as! NSDictionary 

    let fontNew = UIFont(name: "AvenirNext-Regular", size: 19.0) 
    let strA = "\(indexPath.row+1). \(dictAd.object(forKey: "Title")!)" 

    let heightTitle = self.heightForView(text: strA, font: fontNew!, width: tableView.frame.size.width-16) 

    return heightTitle+5; 
} 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let dictAd = self.arrAllQuestions[indexPath.row] as! NSDictionary 

    let cell = tableView.dequeueReusableCell(withIdentifier: "quesionCell", for: indexPath) as! QuestionCell 

    let fontNew = UIFont(name: "AvenirNext-Regular", size: 19.0) 

    let strA = "\(indexPath.row+1). \(dictAd.object(forKey: "Title")!)" 

    cell.lblName.font = fontNew 
    cell.lblName.text = strA 
    cell.lblName.numberOfLines = 0 
    let heightTitle = self.heightForView(text: strA, font: fontNew!, width: tableView.frame.size.width-16) 

    var frame = cell.lblName.frame as CGRect 
    frame.size.height = heightTitle; //you need to adjust this value 
    cell.lblName.frame = frame; 


    let txtView = AnimatableTextView(frame: CGRect(x: 8, y: cell.lblName.frame.origin.y+cell.lblName.frame.size.height+5, width: tableView.frame.size.width-16, height: 25)) 
    txtView.borderWidth = 1.0 
    txtView.borderColor = UIColor.lightGray 
    txtView.cornerRadius = 4.0 
    cell.contentView.addSubview(txtView) 

    return cell 
} 

아래 출력을 볼 수 있습니다.

enter image description here

+0

에게 도움이 될 것입니다. 내 생각 엔 위임 메서드에서 각 셀의 높이를 올바르게 설정하지 않았다는 것입니다. –

+0

'heightForRowAt'를 구현하고 있습니까? – Adeel

+0

스토리 보드에서 라벨과 텍스트 뷰를 추가하고 스토리 보드를 통해 제약 조건을 적용하십시오. – Neha

답변

3

당신의 heightForRowAtIndexPath의 높이 계산이 적절하지 것 같다. UITableViewAutomaticDimension 및 Autolayout을 사용하여 자체 크기 조정 셀을 사용하여 문제를 해결하는 것이 좋습니다. Here은 시작하는 데 도움이되는 훌륭한 자습서입니다. 한 셀에서 UITextView 또는 하위 클래스를 사용 중이고 내용의 높이가 scrollable 속성을 false로 설정하도록하려는 경우 한 가지 더 제안하십시오. viewDidLoad에 희망의

+0

자동 레이아웃을 사용하는 경우 UITableViewAutomaticDimension이 가장 좋습니다. – negaipro

+0

자동 레이아웃을 사용하지 않고 있습니다 ... –

+0

그런 다음 heightForRowAtIndexPath에서 높이 계산을 확인했습니다. –

1

선언이이 올바른 결과이고 quesionCell``의 코드가 어떤 기능에 대한 질문을 개선하기 위해 더 나은 당신

tableView.estimatedRowHeight = 44