您需要创建一个继承自 UITableViewCell 的自定义类,并在那里配置 outlet。
class MyCustomTableViewCell: UITableViewCell {
@IBOutlet weak var menuListLabel: UILabel!
@IBOutlet weak var menuListImage: UIImageView!
}
接下来,您需要在情节提要中配置单元格。选择您的单元格。打开身份检查器并将自定义类设置为“MyCustomTableViewCell”。
然后,在单元格仍处于选中状态的情况下,转到属性检查器,并将重用标识符设置为“MyCustomTableViewCell”。 (这个标识符可以是任何你想要的,你只需要在调用'dequeueReusableCellWithIdentifier'时使用这个确切的值。我喜欢使用我的单元格的类名作为标识符,这样很容易记住。)
在您的表格视图控制器中,实施必要的方法来使用您的自定义单元格构建表格。
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1 // however many sections you need
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // however many rows you need
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// get an instance of your cell
let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as MyCustomTableViewCell
// populate the data in your cell as desired
cell.menuListLabel.text = "some text"
cell.menuListImage.image = UIImage(named: "some image")
return cell
}