单元格不显示子视图swift

时间:2016-05-05 08:52:18

标签: ios swift uitableview cell

我的subviews中有4 cellimageView照片始终显示在每个cell中 始终在显示名称imageview时显示带有图标的label

desc label有时会出现

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = ""
    if cell.contentView.viewWithTag(1000) == nil {
        let name = UILabel(frame: CGRect(x: cell.frame.origin.x + cell.frame.width / 3.3, y: cell.frame.origin.y + cell.frame.height / 4, width: 200, height: cell.frame.height / 5))
        name.textAlignment = NSTextAlignment.Left
        name.text = lines[indexPath.row].name
        name.font = UIFont(name: "OpenSans-Bold", size: cell.frame.height / 6)
        name.textColor = UIColor(red: 247/255.0, green: 215/255.0, blue: 129/255.0, alpha: 1)
        name.tag = 1000
        cell.contentView.addSubview(name)

    }

    let name = cell.contentView.viewWithTag(1000)

    if cell.contentView.viewWithTag(1001) == nil {
        let desc = UILabel(frame: CGRect(x: cell.frame.origin.x + cell.frame.width / 3.3, y: cell.frame.origin.y + cell.frame.height / 4 + name!.frame.height, width: cell.frame.width / 2, height: 0))
        desc.textAlignment = NSTextAlignment.Left
        if let age = lines[indexPath.row].age{
            desc.text = String(age) + NSLocalizedString("radarTable.age", comment: "age")
            print(lines[indexPath.row].isMale)
            if let city = lines[indexPath.row].residence{
                desc.text = desc.text! + DataService.instance.getResidenceMutation(city, sender: self)
            }

        }
        desc.font = UIFont(name: "OpenSans", size: cell.frame.height / 9)
        desc.textColor = UIColor.whiteColor()
        desc.numberOfLines = 2
        desc.lineBreakMode = NSLineBreakMode.ByWordWrapping
        desc.sizeToFit()
        cell.contentView.addSubview(desc)
    }
    if cell.contentView.viewWithTag(1002) == nil{
        let photo = UIImageView(frame: CGRect(x: 0, y: 0, width: cell.frame.height/1.6, height: cell.frame.height/1.6))
        photo.image = lines[indexPath.row].photo

        photo.center = CGPoint(x: (cell.frame.height/2), y: (cell.frame.height / 2))
        photo.contentMode = UIViewContentMode.ScaleAspectFill
        photo.clipsToBounds=true
        photo.layer.cornerRadius = photo.frame.height/2
        photo.tag = 1002
        cell.contentView.addSubview(photo)
    }
    if cell.contentView.viewWithTag(1003) == nil {
        let image = UIImage(named: "profileIcon")
        let backgroundView = UIImageView(frame: CGRect(x: cell.frame.origin.x + cell.frame.width / 3.3 - cell.frame.height / 7,y: cell.frame.origin.y + cell.frame.height / 4 + cell.frame.height / 25, width: cell.frame.height / 10, height: cell.frame.height / 10))
        backgroundView.image = image
        backgroundView.tag = 1003
        backgroundView.center = (name?.center)!
        backgroundView.frame.origin.x = cell.frame.origin.x + cell.frame.width / 3.3 - cell.frame.height / 7
        cell.contentView.addSubview(backgroundView)
    }
    return cell
}

这就是它的样子 tableview

2 个答案:

答案 0 :(得分:0)

你需要改变这个条件:

if cell.contentView.viewWithTag(1001) == nil {
        /* all above code*/
    }

这样的事情:

if cell.contentView.viewWithTag(1001) == nil { Here, label does not exist, so you need to create a label and add subvieew
        let desc = UILabel(frame: CGRect(x: cell.frame.origin.x + cell.frame.width / 3.3, y: cell.frame.origin.y + cell.frame.height / 4 + name!.frame.height, width: cell.frame.width / 2, height: 0))
        desc.textAlignment = NSTextAlignment.Left
        if let age = lines[indexPath.row].age{
            desc.text = String(age) + NSLocalizedString("radarTable.age", comment: "age")
            print(lines[indexPath.row].isMale)
            if let city = lines[indexPath.row].residence{
                desc.text = desc.text! + DataService.instance.getResidenceMutation(city, sender: self)
            }

        }
        desc.font = UIFont(name: "OpenSans", size: cell.frame.height / 9)
        desc.textColor = UIColor.whiteColor()
        desc.numberOfLines = 2
        desc.lineBreakMode = NSLineBreakMode.ByWordWrapping
        desc.sizeToFit()
        cell.contentView.addSubview(desc)
    }
else //Since contentView contains the label this time, just set the label value here
{
let desc:UILabel = cell.contentView.viewWithTag(1001) as! UILabel
if let age = lines[indexPath.row].age{
            desc.text = String(age) + NSLocalizedString("radarTable.age", comment: "age")
            print(lines[indexPath.row].isMale)
            if let city = lines[indexPath.row].residence{
                desc.text = desc.text! + DataService.instance.getResidenceMutation(city, sender: self)
            }

        }
}

请原谅格式化。 但我相信你有了这个主意。

答案 1 :(得分:0)

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = ""
        if cell.contentView.viewWithTag(1000+indexPath.row) == nil {
            let name = UILabel(frame: CGRect(x: cell.frame.origin.x + cell.frame.width / 3.3, y: cell.frame.origin.y + cell.frame.height / 4, width: 200, height: cell.frame.height / 5))
            name.textAlignment = NSTextAlignment.Left
            name.text = lines[indexPath.row].name
            name.font = UIFont(name: "OpenSans-Bold", size: cell.frame.height / 6)
            name.textColor = UIColor(red: 247/255.0, green: 215/255.0, blue: 129/255.0, alpha: 1)
            name.tag = 1000+indexPath.row
            cell.contentView.addSubview(name)

        }

        let name = cell.contentView.viewWithTag(1000+indexPath.row)

        if cell.contentView.viewWithTag(2000+indexPath.row) == nil {
            let desc = UILabel(frame: CGRect(x: cell.frame.origin.x + cell.frame.width / 3.3, y: cell.frame.origin.y + cell.frame.height / 4 + name!.frame.height, width: cell.frame.width / 2, height: 0))
            desc.textAlignment = NSTextAlignment.Left
            if let age = lines[indexPath.row].age{
                desc.text = String(age) + NSLocalizedString("radarTable.age", comment: "age")
                print(lines[indexPath.row].isMale)
                if let city = lines[indexPath.row].residence{
                    desc.text = desc.text! + DataService.instance.getResidenceMutation(city, sender: self)
                }

            }
            desc.font = UIFont(name: "OpenSans", size: cell.frame.height / 9)
            desc.textColor = UIColor.whiteColor()
            desc.numberOfLines = 2
            desc.lineBreakMode = NSLineBreakMode.ByWordWrapping
            desc.sizeToFit()
            cell.contentView.addSubview(desc)
        }
        if cell.contentView.viewWithTag(3000+indexPath.row) == nil{
            let photo = UIImageView(frame: CGRect(x: 0, y: 0, width: cell.frame.height/1.6, height: cell.frame.height/1.6))
            photo.image = lines[indexPath.row].photo

            photo.center = CGPoint(x: (cell.frame.height/2), y: (cell.frame.height / 2))
            photo.contentMode = UIViewContentMode.ScaleAspectFill
            photo.clipsToBounds=true
            photo.layer.cornerRadius = photo.frame.height/2
            photo.tag = 3000+indexPath.row
            cell.contentView.addSubview(photo)
        }
        if cell.contentView.viewWithTag(4000+indexPath.row) == nil {
            let image = UIImage(named: "profileIcon")
            let backgroundView = UIImageView(frame: CGRect(x: cell.frame.origin.x + cell.frame.width / 3.3 - cell.frame.height / 7,y: cell.frame.origin.y + cell.frame.height / 4 + cell.frame.height / 25, width: cell.frame.height / 10, height: cell.frame.height / 10))
            backgroundView.image = image
            backgroundView.tag = 4000+indexPath.row
            backgroundView.center = (name?.center)!
            backgroundView.frame.origin.x = cell.frame.origin.x + cell.frame.width / 3.3 - cell.frame.height / 7
            cell.contentView.addSubview(backgroundView)
        }
        return cell
    }
相关问题