Reputation:
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
self.tableView?.registerNib(UINib(nibName: "TextViewCell", bundle: nil), forCellReuseIdentifier: "TextViewCell")
self.tableView?.registerNib(UINib(nibName: "AttachViewCell", bundle: nil), forCellReuseIdentifier: "AttachViewCell")
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
}
Upvotes: 3
Views: 3864
Reputation: 92409
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
}
Upvotes: 10