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

查看:125
本文介绍了swift - 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

这篇关于swift - tableview 自定义cell , label 莫名的叠了两个,UIImageView没图的也变有图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆