2017-10-05 3 views
0

UITableView을 사용하여 영역의 데이터를 표시합니다. UIImageView과 다른 하나는 UIImageView이 아닌 두 가지 유형의 행이 있습니다. 이미지 UIImageView에있는 경우 행의 높이가 207이어야한다 또는 행의 높이가되어야한다 전무 UIImageView은 64테이블 뷰 셀에 동적 높이를 사용하는 방법

내 코딩 :

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return msgs.count 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! collCell 
    let msg = msgs[indexPath.row] 
    var decodeImgURL = "" 
    cell.lbl1.text = msg.content 

    decodeImgURL = msg.imgurl 

    if decodeImgURL == "none" { 
     cell.img.isHidden = true 
    } else { 

     let dataDecoded : Data = Data(base64Encoded:decodeImgURL,options: .ignoreUnknownCharacters)! 
     let decodedImage = UIImage(data: dataDecoded) 
     cell.img.image = decodedImage 

    } 
    return cell 
} 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    let cell = tableView.cellForRow(at: indexPath) as! collCell 
    if cell.img.image != nil { 
     return 207 
    } else { 
     return 64 
    } 

} 

내가 let cell = tableView.cellForRow(at: indexPath) as! collCell

+0

@ Moritz ok i understand –

답변

1

에서 오류를 얻고있다

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    let msg = msgs[indexPath.row] 

    if msg.imgurl == "none" { 
     print("row \(indexPath.row) does NOT have an image") 
     return 64 
    } else { 
     print("row \(indexPath.row) DOES have an image") 
     return 207 
    } 

} 

Alternativel을 : 당신이 cellForRowAt에서 사용할 때 heightForRowAt에서 동일한 데이터를 사용하여 y, 자동 레이아웃 및 제약 조건을 사용하여 행의 높이를 자동으로 조정할 수 있지만 다른 주제입니다.

+0

내 대답이 업데이트되었습니다. 이미지가 있고 행이없는 행에 유효한 print 문이 있는지 확인하십시오. – DonMag

+0

고마워요. –

관련 문제