Multiple customTableViewCell using nib
I am trying to create multiple custom tableViewCells using Swift. I used the objective-c project to convert to swift code, which is probably the reason for it not working. I have created 2 subclasses of UITableViewCell with xib. Then I added subclasses to the xib class.
But when I register the tips and try to show them in the tableView, the tableView is empty. i added a connected delegate to 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 class UITableViewController
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:
no longer returns optional UITableViewCell
. Thus, you should use the previous code. If you are running a previous beta version of Xcode 6 with an tableView:cellForRowAtIndexPath:
optional return 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
}
source to share