Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NickHung1982/2f8363eb76833309d53ff4f1c8b36a0d to your computer and use it in GitHub Desktop.
Save NickHung1982/2f8363eb76833309d53ff4f1c8b36a0d to your computer and use it in GitHub Desktop.
demo_dynamic_tableView_cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! CustomCell
let postitem = postAr[indexPath.row]
cell.lb_date.text = postitem.Msg_date
cell.userName.text = postitem.Msg_username
cell.userThumb.sd_setImage(with: URL(string: postitem.Msg_userThumb))
//Here is how we deal with image
if postitem.Msg_imgPath.count > 0 {
let img1str = postitem.Msg_imgPath[0]
let url1 = URL(string: img1str)
cell.img1.sd_setImage(with: url1, completed: { retData in
//resize UIimageview to fit image's ratio
if let image = retData.0 {
let ratio = image.size.width / image.size.height
if ratio > 1 {
let newHeight = cell.frame.width / ratio
cell.img1.frame.size = CGSize(width: cell.frame.width, height: newHeight)
}else{
// if is height > width then we need to update this row
let newWidth = cell.frame.height * ratio
cell.img1.frame.size = CGSize(width: newWidth, height: cell.frame.height)
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
})
}
return cell
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment