2016-08-20 26 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 

} 

I取得AlamofireとreloadDataと息子のファイルからのデータ:

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

私の問題は何ですか?

ありがとうございます。

答えて

0

iOSセルを使用しているときに古典的なエラーが発生しました。 (私は教師ですので、よくこの種のエラーが表示されます...心配しないでください)

いくつかの注意点: 1)ラベルをセルに絶対に追加しないでください。そう、セルが再使用される非常に時間が、以前のラベルはここにとどまり、そして新しいラベルは

2追加された)ラベルとして透明であり、あなたはすべてのテキストが重なって表示されます。..

3)あなたは、メモリをリークしています誰もラベルを取り外したり解放したりすることはないからです。

ので、方法が使用できます

A) ユースTAGS単純なケースのための簡単な、Appleは細胞の昔に行ったように。.. (コードI

は、いくつかの定義を行います。

let TEXT_TAG = 2000 

存在しない場合は、現在はそれを得ればviのタグラベルを追加:

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

が機能していない、問題はまだ、その後、細胞をクリートするためにそれを使用して、あなたがのUITableViewCellの太陽のクラスを作ることができます – MohammadReza

+0

存在します。 – chouheiwa

関連する問題