我使用UITableView
來顯示來自領域的數據。我有兩種類型的第一排UIImageView
和其他沒有UIImageView
。如果形象UIImageView
行的高度應爲207或UIImageView
是零行的高度應爲64如何使用tableviewcell的動態高度
我的編碼:
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
@Moritz確定我理解 –