iPhone入门开发系列2备忘录APP学习03--table view

UIViewController和UITableViewController:

数据源协议,table view 需要知道有多少行,每行显示什么,这些都是数据。

在ChecklistViewController里添加如下语句,建立一个Controller 和View的关系

<span style="font-size:14px;">    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem")as! UITableViewCell
            return cell
    }</span>
可以这样理解,tableView问有多少numberOfRowsInSection?  ChecklistViewController 回答有1个 (即return 1);

tableView又问有索引路径的每行单元格是cellForRowAtIndexPath? ChecklistViewController回答是有identifier为“ChecklistItem的UITableViewCell(即return cell)。如下在故事板上右侧的属性检查器里设置identifier为ChecklistItem。

实现的是table view 上有一行identifier为“ChecklistItem”的单元格。


改为return 5,结果如下:


实现在table view上添加单元格cell的方法:先在故事板上添加一个原型的cell,identifier命名为“ChecklistItem”,

在重复使用这个原型cell,调用tableView.dequeueReusableCellWithIdentifier()得到一个新的原型cell的复制copy

Label设置标签tag为1000(比0大的整数多可以的),编辑每行单元格的文本内容:

  override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem")as! UITableViewCell
        let label = cell.viewWithTag(1000) as! UILabel
        if indexPath.row == 0 { label.text = "Walk the dog"
        } else if indexPath.row == 1 { label.text = "Brush my teeth"
        } else if indexPath.row == 2 { label.text = "Learn iOS development"
        } else if indexPath.row == 3 { label.text = "Soccer practice"
        } else if indexPath.row == 4 { label.text = "Eat ice cream"
        }
            return cell
    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值