2016-05-05 68 views
0

我有4個subviews在我cellimageView照片始終顯示在每個cellimageview使用圖標會一直顯示在顯示細胞不顯示的子視圖迅速

label

的遞減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

回答

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

我明白了,我試過,但行爲還是一樣,我試圖打印willDisplayCell標籤文本,它看起來像標籤存在幷包含正確的文本,但它不顯示 –

+0

嘗試使使用bringSubviewToFront方法標籤到前面 – Meet

+0

是這樣的嗎?其他{ let name = cell.contentView.viewWithTag(1000)as!的UILabel cell.contentView.bringSubviewToFront(名稱) } –

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 
    } 
+0

這會導致致命錯誤:意外地發現零,同時展開一個可選值 –