我正在尝试使用 Swift 创建多个自定义 tableViewCells。我使用了一个 objective-c 项目来转换成 swift 代码,这可能是它不起作用的原因。我用 xib 创建了 2 个 UITableViewCell 子类。然后我将子类添加到 xib 类。
但是当我注册 Nib 并尝试在 tableView 中显示它们时,tableView 是空的。我添加了将委托(delegate)连接到 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
}
最佳答案
使用 Xcode 6 beta 7,您的 UITableViewController
类应该如下所示:
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
}
}
}
请注意,在 Xcode 6 beta 7 中,tableView:cellForRowAtIndexPath:
不再返回可选的 UITableViewCell
。所以你必须使用以前的代码。如果您使用带有 tableView:cellForRowAtIndexPath:
的早期版本的 Xcode 6 beta 返回隐式展开的可选 UITableViewCell
,您可以编写以下代码:
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
}
关于ios - 使用 Nib 的多个 customTableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25654006/