Checklists学习日志之创建UITableViewCell的一般方法

教程很早之前讲到的利用prototype cells创建cell的方法搞的我云里雾里。现在好了,现在在AllListsViewController中创建的方法比较好,纯代码。粘贴如下

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 执行的时候会不停遍历indexPath更新cell的textLabel和accessoryType
        let cell = cellForTableView(tableView)
        
        let checklist = lists[indexPath.row]
        cell.textLabel!.text = checklist.name
        cell.accessoryType = .DetailDisclosureButton
        // This control indicates that tapping the cell allows the user to configure the cell’s contents.
        // Configure the cell...
        return cell
    }
    func cellForTableView(tableView: UITableView) -> UITableViewCell{
        let cellIdentifier = "Cell"
        if let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier){
            // Returns a reusable table-view cell object located by its identifier.
            // 创建cell的代码
            return cell
        } else {
            return UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
            // Return Value: An initialized UITableViewCell object or nil if the object could not be created.
        }
    }


    var lists: [Checklist]
    
    required init?(coder aDecoder: NSCoder){
        lists = [Checklist]()
        super.init(coder:aDecoder)
        var list = Checklist(name: "Birthdays")
        lists.append(list)
        list = Checklist(name: "Groceries")
        lists.append(list)
        list = Checklist(name: "Cool Apps")
        lists.append(list)
        list = Checklist(name: "To Do")
        lists.append(list)
    }

这里主要用到的是tableView.dequeueReusableCellWithIdentifier方法。创建cell的时候,先指定一个cellIdentifier,再用此函数创建cell。 然后重写 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 函数更新cell的内容和样式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值