Ios 具有UITableViewCellStyle的Swift自定义Tableview单元格
我正在尝试创建一个自定义表视图单元格 如果我这样做:Ios 具有UITableViewCellStyle的Swift自定义Tableview单元格,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试创建一个自定义表视图单元格 如果我这样做: class TableViewCell: UITableViewCell { @IBOutlet weak var cellBackgroundImage : UIImageView! } 以及: 我获得了一个白色的圆形单元格背景。容易的。我可以使用原始的cell.textlab.text 太好了 但是,如果我想做更复杂的事情: func tableView(_ tableView: UITableView, cellForRowA
class TableViewCell: UITableViewCell {
@IBOutlet weak var cellBackgroundImage : UIImageView!
}
以及:
我获得了一个白色的圆形单元格背景。容易的。我可以使用原始的cell.textlab.text
太好了
但是,如果我想做更复杂的事情:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? TableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell") as? TableViewCell
cell?.cellBackgroundImage.backgroundColor = UIColor.white
cell?.cellBackgroundImage.layer.masksToBounds = false
cell?.cellBackgroundImage.layer.cornerRadius = 5
self.configureCell(cell!, withObject: object)
}
同时,要使用原始表视图属性,如UITableViewCellStyle.subtitle
和cell.accessoryView
,应用程序崩溃或显示错误的输出
这意味着我必须使用具有更多插座的完整自定义单元格来替换原始元素,如UITableViewCellStyle.subtitle和单元格.accessoryView
我将用另一种方式来表达:
我可以将自定义tableview单元格仅用于一个目的(如圆形背景)并使用原始元素(如字幕样式和辅助视图)吗
在这种情况下,如何将?FYI-
dequeueReusableCell(带有标识符:“Cell”,for:indexath)作为?TableViewCell
将永远不会返回nil
(假设它已正确注册)。您得到的崩溃是什么?请说明“使用原始表视图属性”(即代码)是什么意思。第二种方法是不返回任何内容self.configureCell(cell!,with object:object)
导致崩溃,因为如上所述,var cell=tableView.dequeueReusableCell(with identifier:“cell”,for:indexath)as?TableViewCell
不能为零,这意味着将根本不执行cell=UITableViewCell(样式:UITableViewCellStyle.subtitle
)。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? TableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell") as? TableViewCell
cell?.cellBackgroundImage.backgroundColor = UIColor.white
cell?.cellBackgroundImage.layer.masksToBounds = false
cell?.cellBackgroundImage.layer.cornerRadius = 5
self.configureCell(cell!, withObject: object)
}