Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

ios - UITableViewCell Subclass with XIB Swift

I have a UITableViewCell subclass NameInput that connects to an xib with a custom init method.

class NameInput: UITableViewCell {

    class func make(label: String, placeholder: String) -> NameInput {

        let input = NSBundle.mainBundle().loadNibNamed("NameInput", owner: nil, options: nil)[0] as NameInput

        input.label.text = label
        input.valueField.placeholder = placeholder
        input.valueField.autocapitalizationType = .Words

        return input
    }

}

Is there a way I can initialize this cell in the viewDidLoad method and still reuse it? Or do I have to register the class itself with a reuse identifier?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The customary NIB process is:

  1. Register your NIB with the reuse identifier. In Swift 3:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.register(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell")
    }
    

    In Swift 2:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell")
    }
    
  2. Define your custom cell class:

    import UIKit
    
    class NameInput: UITableViewCell {
    
        @IBOutlet weak var firstNameLabel: UILabel!
        @IBOutlet weak var lastNameLabel: UILabel!
    
    }
    
  3. Create a NIB file in Interface Builder (with the same name referenced in step 1):

    • Specify the base class of the tableview cell in the NIB to reference your custom cell class (defined in step 2).

    • Hook up references between the controls in the cell in the NIB to the @IBOutlet references in the custom cell class.

  4. Your cellForRowAtIndexPath would then instantiate the cell and set the labels. In Swift 3:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NameInput
    
        let person = people[indexPath.row]
        cell.firstNameLabel.text = person.firstName
        cell.lastNameLabel.text = person.lastName
    
        return cell
    }
    

    In Swift 2:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NameInput
    
        let person = people[indexPath.row]
        cell.firstNameLabel.text = person.firstName
        cell.lastNameLabel.text = person.lastName
    
        return cell
    }
    

I wasn't entirely sure from your example what controls you placed on your cell, but the above has two UILabel controls. Hook up whatever @IBOutlet references make sense for your app.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...