2017-06-11 2 views
0

Ich bin verloren. Ich habe gesucht und gesucht und kann den Grund nicht finden, warum meine benutzerdefinierte Zelle nicht angezeigt wird.Benutzerdefinierte UITableViewCell erstellt von. Xib zeigt nicht

// ProfileCell.swift: 

import UIKit 
import QuartzCore 

class ProfileCell: UITableViewCell { 

    @IBOutlet weak var profileNameLabel: UILabel! 
    @IBOutlet weak var profilePictureView: UIImageView! 
} 

Die zweite Standard-TableViewCell wird normal angezeigt. Ich habe keine Einschränkungen in Interface Builder oder irgendwelche Fehler. ProfileCell wird auf der Registerkarte ProfileCell.xib als benutzerdefinierte Klasse ausgewählt.

// MoreTableViewControllerIB.swift 

import UIKit 

class MoreTableViewControllerIB: UITableViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  
    return 2 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     // this cell is missing 
     if indexPath.row == 0 { 

      tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell") 
      let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as! ProfileCell    
      cell.profileCommentLabel.text = "Test Comment Label" 
      cell.profilePictureView.image = UIImage(named:"profile_picture_test")    
      return cell 

     // this cell is displayed perfectly 
     }else if indexPath.row == 1 { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "statisticsCell") ?? UITableViewCell(style: .default, reuseIdentifier: "statisticsCell") 
      cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator 
      cell.textLabel?.text = "Statistics" 
      cell.imageView.image = UIImage(named:"statistics") 
      return cell 

     // has to return a cell in every scenario 
     }else{ 
      let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell 
      return cell 
     } 
    } 
} 

Here is a screenshot of what I get.

Antwort

3
tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell") 

diese Zeile in viewDidLoad hinzufügen oder viewWillApppear

+0

Danke für Ihre Antwort, aber das war nicht der Fehler. Das Registrieren der .xib im 'cellForRowAt indexPath' funktioniert ebenfalls. Sehen Sie meine eigene Antwort für das, was falsch war. –

0

So fand ich heraus, was mein Fehler war. Ziemlich doof und es kostete mich einen halben Tag:

Die Zelle wurde bereits angezeigt, aber die Standardhöhe war nicht groß genug, um es zu sehen. Ich dachte, die eingestellte Höhe in der .xib würde verwendet werden. Es ist anscheinend nicht. Also habe ich folgendes hinzugefügt:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath.row == 0 { 
     return 192 // the height for custom cell 0 
    } 
} 
Verwandte Themen