2016-08-20 109 views
0

我有一個tableView單元格的奇怪問題。iOS Swift TableviewCell問題

當我滾動tableView和單元格消失,並再次回到單元格我明白,tableView添加類似單元格準確地在單元格上。

例如看圖片。 3個確切的文字相互添加。

enter image description here

的cellForRowAtIndexPath 功能

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

    if indexPath.row == 0 { 

     let reviewNumber = UILabel() 

     if (self.book.review_count == 0) { 
      reviewNumber.text = "\(self.lang.book["no_review"]!)" 
     } 
     if (self.book.review_count > 0) { 
      reviewNumber.text = "\(self.book.review_count!) \(self.lang.general["review"]!)" 
     } 

     reviewNumber.textAlignment = .Right 
     reviewNumber.font = UIFont(name: "Vazir", size: 14) 
     reviewNumber.numberOfLines = 0 
     reviewNumber.translatesAutoresizingMaskIntoConstraints = false 
     reviewNumber.textColor = UIColor.grayColor() 

     cell.contentView.addSubview(reviewNumber) 




     let voteIcon = UIImageView() 
     voteIcon.image = UIImage(named: "vote-icn") 
     voteIcon.translatesAutoresizingMaskIntoConstraints = false 
     cell.contentView.addSubview(voteIcon) 
     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[v0(24)]-|",options: [],metrics: nil,views: ["v0" : voteIcon])) 


     let reviewIcon = UIImageView() 
     reviewIcon.image = UIImage(named: "review-icn") 
     reviewIcon.translatesAutoresizingMaskIntoConstraints = false 
     cell.contentView.addSubview(reviewIcon) 
     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[v0(24)]-|",options: [],metrics: nil,views: ["v0" : reviewIcon])) 


     let voteNumber = UILabel() 
     voteNumber.text = " ۴.۵ از ۱۶۵۴رأی" 
     voteNumber.textAlignment = .Left 
     voteNumber.font = UIFont(name: "Vazir", size: 14) 
     voteNumber.numberOfLines = 0 
     voteNumber.translatesAutoresizingMaskIntoConstraints = false 
     voteNumber.textColor = UIColor.grayColor() 

     cell.contentView.addSubview(voteNumber) 


     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-3-[v0]-3-|",options: [],metrics: nil,views: ["v0" : reviewNumber])) 
     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-3-[v0]-3-|",options: [],metrics: nil,views: ["v0" : voteNumber])) 
     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-35-[v0]-8-[v1(25)]",options: [],metrics: nil,views: ["v0" : voteNumber, "v1" : voteIcon, "v2" : reviewNumber, "v3" : reviewIcon])) 

     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[v2]-8-[v3(25)]-35-|",options: [],metrics: nil,views: ["v0" : voteNumber, "v1" : voteIcon, "v2" : reviewNumber, "v3" : reviewIcon])) 



    } 

    if indexPath.row == 1 { 

     let userBookStatusButtn = UIButton(type: .Custom) 
     userBookStatusButtn.setTitle("خواهم خواند", forState: .Normal) 
     userBookStatusButtn.alpha = 0.2 
     userBookStatusButtn.titleLabel?.font = UIFont(name: "Vazir", size: 14) 
     userBookStatusButtn.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
     //   userBookStatusButtn.setImage(UIImage(named: "vote-icn"), forState: .Normal) 
     userBookStatusButtn.translatesAutoresizingMaskIntoConstraints = false 

     userBookStatusButtn.backgroundColor = UIColor(red:0.0/256.0 ,green:150.0/256.0, blue:136.0/256.0 ,alpha:1) 

     cell.contentView.addSubview(userBookStatusButtn) 

     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0(48)]-20-|",options: [],metrics: nil,views: ["v0" : userBookStatusButtn])) 
     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-35-[v0]-35-|",options: [],metrics: nil,views: ["v0" : userBookStatusButtn])) 


    } 

    if indexPath.row == 2 { 

     let topBorder = UIView() 
     topBorder.translatesAutoresizingMaskIntoConstraints = false 
     topBorder.backgroundColor = UIColor(white: 0.5, alpha: 0.5) 
     cell.contentView.addSubview(topBorder) 

     let bottomBorder = UIView() 
     bottomBorder.translatesAutoresizingMaskIntoConstraints = false 
     bottomBorder.backgroundColor = UIColor(white: 0.5, alpha: 0.5) 
     cell.contentView.addSubview(bottomBorder) 


     let paragraphStyle = NSMutableParagraphStyle() 
     paragraphStyle.lineSpacing = 5 
     paragraphStyle.baseWritingDirection = .RightToLeft 

     guard let descriptionStr = self.book.description else {return cell} 
     let attrString = NSMutableAttributedString(string: descriptionStr) 
     attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length)) 


     let description = UILabel() 
     description.attributedText = attrString 
     description.textAlignment = .Justified 
     description.font = UIFont(name: "Vazir", size: 14) 
     description.numberOfLines = 0 
     description.translatesAutoresizingMaskIntoConstraints = false 

     description.lineBreakMode = NSLineBreakMode.ByWordWrapping 

     cell.contentView.addSubview(description) 



     let title = UILabel() 
     title.text = "\(self.lang.book["summery"]!)" 
     title.alpha = 0.2 
     title.textAlignment = .Center 
     title.font = UIFont(name: "Vazir-Bold", size: 16) 
     title.translatesAutoresizingMaskIntoConstraints = false 
     cell.contentView.addSubview(title) 



     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[v0]-|",options: [.AlignAllCenterX],metrics: nil,views: ["v0" : title])) 
     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[v0]-20-|",options: [],metrics: nil,views: ["v0" : description])) 

     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[v1]-44-[v0]",options: [],metrics: nil,views: ["v0" : description,"v1" : title ])) 
     cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[v0]-8-|",options: [],metrics: nil,views: ["v0" : description,"v1" : title ])) 



    } 
    cell.textLabel?.text = nil 



    return cell 

} 

我得到Alamofire和reloadData從子文件中的數據:

func tableRefresh() 
{ 
    dispatch_async(dispatch_get_main_queue(), { 
     self.bookDetailTableView.reloadData() 
    }) 
} 

什麼是我的問題嗎?

謝謝你們。

回答

0

您在使用iOS單元格時犯了一個經典錯誤。 (我是老師,所以我經常看到這種錯誤...不用擔心..)

一些注意事項: 1)不要給單元格添加標籤。這樣做,很花時間的小區被重複使用,以前的標籤留在了這裏,和新的標籤添加

2)標籤是透明的,你會看到所有文字重疊..

3)您正在泄漏內存,因爲沒有人會分離/釋放標籤。

這樣的方式可以是:

A)簡單簡單的情況下 使用標籤,因爲蘋果對細胞的昔日相比.. (代碼我

做一些定義:

let TEXT_TAG = 2000 

附加標籤,如果不存在,如果存在的話得到它的VI TAG:

let cell = tableView.dequeueReusableCell(withIdentifier: REUSE, for: indexPath) 

let text = "my text..." 
    if let label = cell.viewWithTag(TEXT_TAG) as? UILabel{ 
     label.text = text 
    }else{ 
     let frame = CGRect(x: 100, y: 20, width: 50, height: 50) 
     let label = UILabel(frame: frame) 
     label.text = text 
     cell.addSubview(label) 
    }   

B)進行自定義單元格...

class CustomViewCell: UITableViewCell { 

    var label: UILabel? 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
     let frame = CGRect(x: 100, y: 20, width: 50, height: 50) 
     let label = UILabel(frame: frame) 
     self.addSubview(label) 
    } 

在控制器簡單:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier: REUSE, 
     for: indexPath) as! CustomViewCell 

     cell.label = "my text" 
+0

感謝親愛的@ingconti,非常有用的提示:) – MohammadReza

0

我得到你的問題,你從tableView可重用池獲得單元格。第一個問題,如果你想改變UITableViewCell視圖,你需要創建它的兒子類。我有一個函數來解決它。 我第一次使用Stackoverflow,我不知道我在哪裏可以放我的代碼,所以我把它寫在這句話下。

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
for let item in cell.contentView.subviews { 
     item.removeFromSuperView(); 
    } 

然後使用你的代碼,它應該幫助你。

+0

不工作,問題依然存在 – MohammadReza

+0

你可以做一個陽光類的UITableViewCell的,然後用它來創造細胞。 – chouheiwa