2016-08-25 6 views
-1

私はかなりこのObjective Cの質問に同じ問題があった= Cell Label text overlapping in cellsですが、私はSwiftの回答を見つけられませんでした。また、テーブルビュー/セルには非常に新しく、明確にこれを行う適切な方法を知りたいのですが、間違っています。Swift:テーブルビューのセル内容が各リロードごとに何度も追加されますか?

私はストーリーボードで作成したテーブルビューにカスタムセルを持っています。私はセル(ラベルなど)のコンテンツをプログラムで追加する必要があります。私はここでこれをしました -

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as! EventTableCellTableViewCell 
//  cell.eventTitle.text = names[indexPath.row] 
//  cell.eventDescription.text = descriptions[indexPath.row] 

     cell.contentView.clipsToBounds = false 

     //cell UIX 
     let eventTitleLabel = UILabel() 
     let dateLabel = UILabel() 
     let authorLabel = UILabel() 
     let locationLabel = UILabel() 

     let categoryView = UIImageView() 

     //border 
     let botBorder: CALayer = CALayer() 
     botBorder.frame = CGRectMake(0.0, cell.frame.height-1, cell.frame.width, 1.0) 
     botBorder.backgroundColor = colorWithHexString("#C5C7C9").CGColor 

     //initalize cell items 
     eventTitleLabel.text = names[indexPath.row] 
     eventTitleLabel.frame = CGRectMake(0, 0, cell.frame.width * 0.5, cell.frame.height * 0.3) 
     eventTitleLabel.tag = indexPath.row 
     eventTitleLabel.textAlignment = .Left 
     eventTitleLabel.font = UIFont(name: "Montserrat-Bold", size: screenSize.height * (24/568)) 
     eventTitleLabel.textColor = UIColor.blackColor() 
     eventTitleLabel.center = CGPointMake(cell.contentView.frame.width * 0.35, cell.contentView.frame.height * 0.35) 


     dateLabel.textColor = colorWithHexString("#C5C7C9") 
     let dateString = "\(dates[indexPath.row]) \(times[indexPath.row])" 
     dateLabel.text = dateString 
     dateLabel.frame = CGRectMake(0, 0, cell.frame.width * 0.5, cell.frame.height * 0.3) 
     dateLabel.tag = indexPath.row 
     dateLabel.textAlignment = .Left 
     dateLabel.font = UIFont(name: "Montserrat-Regular", size: screenSize.height * (10/568)) 
     dateLabel.center = CGPointMake(cell.contentView.frame.width * 0.35, cell.contentView.frame.height * 0.6) 

     //for setting bottom label 

     //Code sets label (yourLabel)'s text to "Tap and hold(BOLD) button to start recording." 
     let boldAttribute = [ 
      //You can add as many attributes as you want here. 
      NSFontAttributeName: UIFont(name: "Montserrat-Bold", size: 11.0)!] 

     let regularAttribute = [ 
      NSFontAttributeName: UIFont(name: "Montserrat-Regular", size: 11.0)!] 

     let beginningAttributedString = NSAttributedString(string: authors[indexPath.row], attributes: boldAttribute) 
     //let boldAttributedString = NSAttributedString(string: locationNames[indexPath.row], attributes: boldAttribute) 
     let boldAttributedString = NSAttributedString(string: "Monterey, CA USA", attributes: regularAttribute) 
     let fullString = NSMutableAttributedString() 

     fullString.appendAttributedString(beginningAttributedString) 
     fullString.appendAttributedString(NSAttributedString(string: " ", attributes: regularAttribute)) //space 
     fullString.appendAttributedString(boldAttributedString) 
     //------ 

     authorLabel.attributedText = fullString 

     authorLabel.textColor = colorWithHexString("#C5C7C9") 
     authorLabel.frame = CGRectMake(0, 0, cell.frame.width, cell.frame.height * 0.3) 
     authorLabel.tag = indexPath.row 
     authorLabel.textAlignment = .Left 
     authorLabel.center = CGPointMake(cell.contentView.frame.width * 0.5, cell.contentView.frame.height * 0.8) 

     categoryView.frame = CGRectMake(0, 0, screenSize.width * (50/screenSize.width), screenSize.width * (50/screenSize.width)) 
     categoryView.layer.cornerRadius = categoryView.frame.width * 0.5 
     categoryView.center = CGPointMake(cell.contentView.frame.width * 0.7, cell.contentView.frame.height * 0.35) 
     categoryView.backgroundColor = colorWithHexString("#3dccff") 
     cell.contentView.addSubview(categoryView) 


     cell.contentView.addSubview(eventTitleLabel) 
     cell.contentView.addSubview(dateLabel) 
     cell.contentView.addSubview(locationLabel) 
     cell.contentView.addSubview(authorLabel) 
     cell.contentView.layer.addSublayer(botBorder) 

     print("called cell") 

     return cell 
    } 

これは初めてです。しかし、私はあなたがスクロールするたびに呼び出され、また、私のテーブルビューで新しいセルを占める新しいアイテムを追加した後に、これを呼び出すことを印刷物から学びました。それが起こるとき、私はこの重複を取得する -

enter image description here

私はこれをどのように修正すればよいですか?私はTableViewCell is piled up and appear again and againも見て、cell.contentView.removeFromSuperView()をこの機能の冒頭に置いてみました。そのため、古いコンテンツは消去されますが、何も表示されませんでした。

プログラムでコンテンツを追加する正しい方法は何ですか?

+2

なぜこのコードはすべてあなたの '' cellForRowAtIndexPath''関数にありますか?カスタムセルクラスがあります。このコードのほとんどはそのクラスに属しています。それが所属するコードを入力します。 – rmaddy

+1

'cellForRowAtIndexPath'のセルにサブビューを追加しないでください。各セルに既にラベル/サブビュー(Interface Builderで設計されているか、あるいは 'init()'でプログラム的に)を作成し、毎回ラベルの 'text'などを更新することでそれらを構成します。 –

+0

あなたは私の質問を見ていますかhttp://stackoverflow.com/questions/39148460/swift-cant-configure-custom-table-cell-programmatically-init-not-called私はカスタムクラスで何も設定できないようです – skyguy

答えて

2

テーブルビューのセルはリサイクルされているため、最後に置いたセルが表示されるたびに、入れたラベルで塗りつぶされたセルを適切に処理する必要があります。 新しいセルで一度しか呼び出されないセルの何らかのinitメソッドがあり、セルがリサイクルされるときに無視されます。この種の機能は、内部ではなくセルカスタムクラス自体に組み込む必要がありますcellForRowAtIndexPath

+0

これはinitで呼び出されないために何かをしようとすると問題があります。http://stackoverflow.com/questions/39148460/swift-cant-configure-custom-table-cell-programmatically- init-not-called – skyguy

関連する問題