简体   繁体   中英

Loading a Custom UITableViewCell from nib to a UIViewController in Swift

I'm currently working on an exercise swift program, one that requires CoreData results to be displayed on a table.

I've structured my app such that the storyboard itself doesn't contain any UI elements, only views (with an accompanying UIViewController class), which then loads custom nibs / xibs (also accompanied by a UIView Subclass). In this case, the xib contains a UITableView , and a separate xib contains the cell.

TableView Class :

import UIKit

protocol SkillsViewDelegate{
}

class SkillsView: UIView {
    var delegate : SkillsViewDelegate!
    @IBOutlet weak var skillsTable: UITableView!
}

TableView Controller :

import UIKit
import CoreData

class SkillsViewController: UIViewController, SkillsViewDelegate, UITableViewDataSource, UITableViewDelegate {
    var displaySkillsView : SkillsView!
    var displaySkillsCell : SkillViewCell!
    let textCellIdentifier = "skillViewCell"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.displaySkillsView = UIView.loadFromNibNamed("SkillsView") as! SkillsView
        self.displaySkillsView.delegate = self
        self.view = self.displaySkillsView
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK : -Table view delegates

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

        return cell
    }
}

TableCellView Class :

import UIKit

protocol SkillsViewCellDelegate{
}

class SkillViewCell: UITableViewCell {
    var delegate : SkillsViewCellDelegate!

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

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
   }

    @IBOutlet weak var skillName: UILabel!
    @IBOutlet weak var skillRank: UILabel!
}

I can't figure out how to call the cell xib into the controller. I would guess it's something like this problem , but the difference is the guy is using a UITableViewController . I tried adding that as well, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles.

What I Tried :
I tried adding UITableViewController up top, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles.

Any suggestions?

You can just register your SkillsViewCell in your SkillsViewController

Obj C

-(void)viewDidLoad {
   [super viewDidLoad];
   UINib *cellNib = [UINib nibWithNibName:@"NibName" bundle:nil];
   [self.skillsTable registerNib:cellNib forCellReuseIdentifier:textCellIdentifier];
}

Swift 2

override func viewDidLoad() {
   super.viewDidLoad()
   let nibName = UINib(nibName: "NibName", bundle:nil)
   self.skillsTable.registerNib(nibName, forCellReuseIdentifier: textCellIdentifier)
}

Swift 4

override func viewDidLoad() {
   super.viewDidLoad()
   let nibName = UINib(nibName: "NibName", bundle:nil)
   self.skillsTable.register(nibName, forCellReuseIdentifier: textCellIdentifier)
}

Then only inherit from UIViewController not UITableViewController

To call methods specific to your custom cell

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SkillViewCell

    cell.skillName.text = "Some text"
    cell.skillRank.text = "Some text"
    return cell
}

I think you will need to use

func registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)

in viewDidLoad

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/registerNib:forCellReuseIdentifier :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM