以编程方式创建自定义动态原型单元

问题描述 投票:1回答:2

我在尝试以编程方式创建动态原型单元时遇到问题。这是我第一次尝试这种方法。我总是通过情节提要创建它们。

我有包含以下内容的ViewController.swift文件

var noteListTableView: UITableView = UITableView()

noteListTableView.frame.origin.x = 0.0
noteListTableView.frame.origin.y = kNAV_BAR_HEIGHT
noteListTableView.frame.size.width = kDEVICE_WIDTH
noteListTableView.frame.size.height = kDEVICE_HEIGHT - kNAV_BAR_HEIGHT
noteListTableView.dataSource = self
noteListTableView.delegate = self
self.view.addSubview(noteListTableView)

也在文件中,我包括了适当的TableView委托和数据源功能

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return GlobalDataController.notesDB.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var aNote: NoteItem = GlobalDataController.notesDB[indexPath.row] as NoteItem

    if var cell: NoteListTVCell = noteListTableView.dequeueReusableCellWithIdentifier(kReuseCellNoteList) as? NoteListTVCell {

        cell.noteTitle.text = "Title"
        cell.noteContentSummary.text = "Content Summary"
        cell.noteDate.text = "May 20"

            return cell
        }
    else{

        var cell: NoteListTVCell = NoteListTVCell(style: UITableViewCellStyle.Default, reuseIdentifier: kReuseCellNoteList)

        cell.noteTitle.text = "Title"
        cell.noteContentSummary.text = "Content Summary"
        cell.noteDate.text = "May 20"

        return cell
    }
}

((1)IF-STATEMENT似乎总是失败,这就是为什么我根据在网上为Obj-C找到的一些示例添加了else语句。

((2)触发ELSE时,程序总是在尝试分配单元格*值时崩溃,表示它遇到了nil值。

编辑:NoteListTVCell

class NoteListTVCell: UITableViewCell {

    var noteTitle: UILabel!
    var noteContentSummary: UILabel!
    var noteDate: UILabel!

    var cellMargin: CGFloat = kPadding * 3

    override func awakeFromNib() {
        super.awakeFromNib()

        noteTitle = UILabel(frame: CGRect(x: cellMargin, y: kPadding, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 30.0))
        noteTitle.backgroundColor = kColorTransparent
        noteTitle.font = UIFont(name: kFont02, size: 20.0)
        noteTitle.textColor = Scripts.hexColor(0x000000, alpha: 0.8)
        noteTitle.text = "Title"
        self.addSubview(noteTitle)

        /* Note Content Summary
        -----------------------------------------------------------------------------------------*/
        var noteContentSummaryY: CGFloat = noteTitle.frame.origin.y + noteTitle.frame.size.height

        noteContentSummary = UILabel(frame: CGRect(x: cellMargin, y: noteContentSummaryY, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 20.0))
        noteContentSummary.backgroundColor = kColorTransparent
        noteContentSummary.font = UIFont(name: kFont02, size: 14.0)
        noteContentSummary.textColor = Scripts.hexColor(0x000000, alpha: 0.4)
        noteContentSummary.text = "This is a summary..."
        self.addSubview(noteContentSummary)

        /* Note Date
        -----------------------------------------------------------------------------------------*/
        noteDate = UILabel(frame: CGRect(x: kDEVICE_WIDTH - 100.0 - kPaddingx2, y: kPadding, width: 100.0, height: 20.0))
        noteDate.backgroundColor = kColorTransparent
        noteDate.font = UIFont(name: kFont02, size: 10.0)
        noteDate.textColor = Scripts.hexColor(kColorThemeBlue, alpha: 1.0)
        noteDate.textAlignment = NSTextAlignment.Right
        noteDate.text = "May 8, 2:14 AM"
        self.addSubview(noteDate)
    }
}
ios swift uitableview cell
2个回答
1
投票

您尚未在自定义单元格中覆盖init方法,但是如果您未提供,则无论如何它将调用super init方法。将您的单元格初始化代码放入init方法而不是awakeFromNib方法。

请在下面尝试。

    required init(coder aDecoder: NSCoder) {

            super.init(coder: aDecoder)
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {

         super.init(style: style, reuseIdentifier: reuseIdentifier)
         //Do your cell set up
        noteTitle = UILabel(frame: CGRect(x: cellMargin, y: kPadding, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 30.0))
        noteTitle.backgroundColor = kColorTransparent
        noteTitle.font = UIFont(name: kFont02, size: 20.0)
        noteTitle.textColor = Scripts.hexColor(0x000000, alpha: 0.8)
        noteTitle.text = "Title"
        self.addSubview(noteTitle)

        /* Note Content Summary
        -----------------------------------------------------------------------------------------*/
        var noteContentSummaryY: CGFloat = noteTitle.frame.origin.y + noteTitle.frame.size.height

        noteContentSummary = UILabel(frame: CGRect(x: cellMargin, y: noteContentSummaryY, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 20.0))
        noteContentSummary.backgroundColor = kColorTransparent
        noteContentSummary.font = UIFont(name: kFont02, size: 14.0)
        noteContentSummary.textColor = Scripts.hexColor(0x000000, alpha: 0.4)
        noteContentSummary.text = "This is a summary..."
        self.addSubview(noteContentSummary)

        /* Note Date
        -----------------------------------------------------------------------------------------*/
        noteDate = UILabel(frame: CGRect(x: kDEVICE_WIDTH - 100.0 - kPaddingx2, y: kPadding, width: 100.0, height: 20.0))
        noteDate.backgroundColor = kColorTransparent
        noteDate.font = UIFont(name: kFont02, size: 10.0)
        noteDate.textColor = Scripts.hexColor(kColorThemeBlue, alpha: 1.0)
        noteDate.textAlignment = NSTextAlignment.Right
        noteDate.text = "May 8, 2:14 AM"
        self.addSubview(noteDate)
    }

在viewDidLoad中执行此操作

override func viewDidLoad() {
        super.viewDidLoad()
        noteListTableView = UITableView(frame: self.view.frame, style: UITableViewStyle.Plain)
        noteListTableView.dataSource = self
        noteListTableView.delegate = self
        self.view.addSubview(noteListTableView)
        noteListTableView.registerClass(NoteListTVCellTableViewCell.self, forCellReuseIdentifier: kReuseCellNoteList)
        // Do any additional setup after loading the view, typically from a nib.
    }

1
投票

您需要在viewDidLoad中注册您的自定义tableViewCell类:

notelistTableView.registerClass(NoteListTVCell.self, forCellReuseIdentifier: kReuseCellNoteList)

Documentation

在InterfaceBuilder中设计原型时,这是自动完成的。在代码中设计单元格时,它是必需的。

© www.soinside.com 2019 - 2024. All rights reserved.