[英]Swift static tableview with dynamic section
我有 tableviewcontroller,有 2 个部分。
第一个(静态)部分有 2 行,第二个部分应该是动态的。
代码:
class DocumentInventoryChangeTableViewController: UITableViewController {
var results: [String] = ["Dog", "Cat", "Snake", "Test"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "DocumentInventoryCell", bundle: nil), forCellReuseIdentifier: "DocumentInventoryCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 1 {
return results.count
}
return 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell!
if indexPath.section == 0 {
cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
} else {
cell = tableView.dequeueReusableCellWithIdentifier("DocumentInventoryCell", forIndexPath: indexPath)
cell.textLabel?.text = results[indexPath.row]
}
return cell
}
}
但我收到此错误:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:
'*** -[__NSArray0 objectAtIndex:]: 索引 0 超出空 NSArray 的边界'
根据我的经验,在UITableViewController
覆盖这些方法就足够了:
tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int
如果你想在你的表格视图中有自定义表格视图单元格,你需要用 nib 来创建UITableViewCell
子类,并将它注册到你的表格视图中。
我的整个控制器看起来像这样:
var data = ["Ahoj", "Hola", "Hello"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 1 {
return data.count
}
return super.tableView(tableView, numberOfRowsInSection: section)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! CustomCell
cell.titleLabel.text = data[indexPath.row]
return cell
}
return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44
}
override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
return 0
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if indexPath.section == 1 {
print(data[indexPath.row])
}
}
@IBAction func addItem() {
data.append("Item \(data.count)")
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: data.count - 1, inSection: 1)], withRowAnimation: .Left)
tableView.endUpdates()
}
@IBAction func removeItem() {
if data.count > 0 {
data.removeLast()
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: data.count, inSection: 1)], withRowAnimation: .Left)
tableView.endUpdates()
}
}
问题是不存在“第一个(静态)部分有 2 行,第二个部分应该是动态的”的表格视图。 您的表格视图需要完全动态; 也就是说,它应该在故事板中显示一个原型单元,而不是静态内容,并且它的全部内容应该在代码中配置,包括第一部分——即使第一部分永远不会改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.