首页 > tableview 自定义cell , label 莫名的叠了两个,UIImageView没图的也变有图

tableview 自定义cell , label 莫名的叠了两个,UIImageView没图的也变有图

我想应该是初始的问题,但一直没办法除错

 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
}

资料是正确的只有一行
但显示出来,一开始都正常,滑到下面就不正常了


不要在 cellForRowAtIndexPath 方法中对 cell 进行 addSubview


tableView 是有reuse(重用)这么个机制的
原理

【热门文章】
【热门文章】