所以我的目标是从firebase下载数据并在tableview中填充它。根据从Firebase下载的数据,某些单元可能具有或不具有图像视图。我所做的是,在表格视图单元格中添加一个图像视图,并将其高度设置为0。当我调用表格视图单元格类的init函数时,我传递了所有参数。如果download url参数为nil,则图像视图将保留其属性。但是,如果下载URL具有URL,则将显示图像视图并指定高度,此后将调用一个函数(在表视图单元类中)以下载和设置UIImageView的图像。我认为这不是正确的方法,因为每次我滚动(重新加载单元格)图像都出现在单元格中时,这些图像应该没有任何图像。请帮我解决一下这个。我是iOS应用程序开发的新手,所以请保持友好。这是CellForRow函数。数据下载完成后,我正在重新加载tableview。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 0){
}
guard let cell = broadcastTable.dequeueReusableCell(withIdentifier: "BC") as? BroadCastCell else {
return UITableViewCell()
}
let cast: Broadcasts!
cast = BroadCasts[indexPath.row]
cell.databaseinit(username: cast.username, college: cast.collegename, message: cast.message, downloadURL: cast.downloadURL, date: cast.date)
broadcastTable.rowHeight = UITableView.automaticDimension
broadcastTable.separatorStyle = .none
return cell
}
这是TableCell类
BroadCastCell类:UITableViewCell {
@IBOutlet weak var DateAndTimeLbl: UILabel!
@IBOutlet weak var BackGroundView: UIView!
@IBOutlet weak var messageLbl: UILabel!
@IBOutlet weak var UsernameLbl: UILabel!
@IBOutlet weak var collegeLbl: UILabel!
@IBOutlet weak var MessageImg: UIImageView!
@IBOutlet weak var MessageImgHeight: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
BackGroundView.backgroundColor = UIColor(gradientStyle: .topToBottom, withFrame: BackGroundView.frame, andColors: [#colorLiteral(red: 1, green: 0.4274509804, blue: 0.03137254902, alpha: 1),#colorLiteral(red: 1, green: 0.5960784314, blue: 0.04705882353, alpha: 1)])
}
func databaseinit(username: String, college: String, message: String, downloadURL: String, date: String){
self.UsernameLbl.text = username
self.collegeLbl.text = college
self.messageLbl.text = message
if(downloadURL != "nil"){
downloadImages(imageurl: downloadURL)
}
self.DateAndTimeLbl.text = date
}
func downloadImages(imageurl: String){
MessageImgHeight.constant = 150
self.MessageImg.sd_setImage(with: URL(string: imageurl))
MessageImg.isHidden = false
MessageImg.layer.masksToBounds = true
MessageImg.layer.cornerRadius = 8
MessageImg.layer.borderWidth = 1.5
MessageImg.layer.borderColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
}