佚名通过本文主要向大家介绍了(swift)tableview自定义cell,label莫名的叠了两个,UIImageView没图的也变有图等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: (swift)tableview 自定义cell , label 莫名的叠了两个,UIImageView没图的也变有图
描述:
解决方案1:
描述:
我想应该是初始的问题,但一直没办法除错
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let newsImageView = UIImageView(frame: CGRectMake(0, 0, frameWidth,140 ))
newsImageView.image = UIImage(named: "tnews")
MyFunc.loadImageFromUrl(webImageURL + "news/" + (Data[indexPath.row]["iphoto"] as! String) ,view: newsImageView, Location: "newslist")
let newTitlelabelFont = UIFont(name: "Heiti TC", size: 16)
let newsTitleLabelHight = MyFunc.heightForView(Data[indexPath.row]["Subject"] as! String, font: newTitlelabelFont!, width: 16)
let newsTitlelabel = UILabel(frame: CGRectMake(0,newsImageView.frame.height + 5 ,frameWidth,newsTitleLabelHight ))
newsTitlelabel.text = ""
newsTitlelabel.numberOfLines = 0
newsTitlelabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
newsTitlelabel.font = newTitlelabelFont
newsTitlelabel.text = Data[indexPath.row]["Subject"] as! String
print(newsTitlelabel.text)
newsTitlelabel.textColor = MyFunc.colorHex("#ff69b4")
newsTitlelabel.backgroundColor = UIColor.whiteColor()
newsTitlelabel.sizeToFit()
cell.addSubview(newsImageView)
cell.addSubview(newsTitlelabel)
return cell
}
资料是正确的只有一行
但显示出来,一开始都正常,滑到下面就不正常了
解决方案1:
tableView 是有reuse(重用)这么个机制的
原理
不要在 cellForRowAtIndexPath
方法中对 cell 进行 addSubview