简体   繁体   中英

multiple customTableViewCell using nib

i'm trying create multiple custom tableViewCells using Swift. i've used an objective-c project to convert into swift code, which probably is the reason why its not working. i've created 2 UITableViewCell subclasses with a xib. Then i added the subclasses to the xib class.

But when i register the nibs and try to show them in the tableView the tableView is empty. i've added connected the delegate to the uitableView

viewDidLoad

    self.tableView?.registerNib(UINib(nibName: "TextViewCell", bundle: nil), forCellReuseIdentifier: "TextViewCell")
    self.tableView?.registerNib(UINib(nibName: "AttachViewCell", bundle: nil), forCellReuseIdentifier: "AttachViewCell")

cellForRowAtIndexPath

func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
    var cell: UITableViewCell? = nil


    if indexPath.row == 0 {


        var  textCell:TextViewCell! = tableView.dequeueReusableCellWithIdentifier("TextViewCell") as? TextViewCell

        textCell.nameTextView.text = "Test"

        cell = TextViewCell()
    }

    if indexPath.row == 1 {
        var  attachCell:AttachViewCell! = tableView.dequeueReusableCellWithIdentifier("AttachViewCell") as? AttachViewCell

        attachCell.attachLabel.text = "Attach image"


        cell = AttachViewCell()
    }




    return cell
}

Using Xcode 6 beta 7, your UITableViewController class should look like this:

import UIKit

class ViewController: UITableViewController {

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

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerNib(UINib(nibName: "TextViewCell", bundle: nil), forCellReuseIdentifier: "TextViewCell")
        tableView.registerNib(UINib(nibName: "AttachViewCell", bundle: nil), forCellReuseIdentifier: "AttachViewCell")
    }

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier("TextViewCell", forIndexPath: indexPath) as TextViewCell
            cell.nameTextView!.text = "Test" //@IBOutlet weak var nameTextView: UILabel!
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("AttachViewCell", forIndexPath: indexPath) as AttachViewCell
            cell.attachLabel!.text = "Attach image" //@IBOutlet weak var attachLabel: UILabel!
            return cell
        }
    }

}

Note that in Xcode 6 beta 7, tableView:cellForRowAtIndexPath: does not return a optional UITableViewCell anymore. So you have to use the previous code. If you use a previous version of Xcode 6 beta with tableView:cellForRowAtIndexPath: returning an Implicitly Unwrapped Optional UITableViewCell , you can write the following code:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("TextViewCell", forIndexPath: indexPath) as TextViewCell
        cell.nameTextView!.text = "Test" //@IBOutlet weak var nameTextView: UILabel!
        return cell
    }

    if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCellWithIdentifier("AttachViewCell", forIndexPath: indexPath) as AttachViewCell
        cell.attachLabel!.text = "Attach image" //@IBOutlet weak var attachLabel: UILabel!
        return cell
    }

    return nil
}

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