我的tableview的应用程序是这样的:Swift |为什么我的UITableView是空的(自定义单元格)
我不知道为什么label1
不工作。
以下是我做到了这一点:
我拖着一个UITableViewController
并将其设置为initialViewController。
我设置的UITableViewCell风格Custom
然后,我设置的UITableViewController的
和定义UITableViewCell的
然后,我增加了一些限制:
这里的TableViewController.swift
:
import UIKit
class TableViewController: UITableViewController {
let arr = ["abc", "def", "ghi", "jkl", "mno"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1 // This should more than one if you want to see the sections and rows in your tableview
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("item", forIndexPath: indexPath) as! TableViewCell
cell.label1.text = arr[indexPath.row]
return cell
}
}
而这里的TableViewCell.swift
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var label1: UILabel!
}
任何人都知道如何解决这个问题?我真的需要使用自定义单元格!
P.S.我使用的Xcode 7测试版5,雨燕2.0
你加约束表视图? – anhtu
我使用'UITableViewController',@anhtu,所以它会自动为我添加。 – David
'override func numberOfSectionsInTableView(tableView:UITableView) - > Int { return 0 }'出错了 – anhtu