標籤:
一:TableViewController
1:刪掉預設的ViewController 拖動一個TableViewController
2:建立一個Cocoa Touch Class,命名為:TableViewController
3:將1邦定至2
4:拖動一個Label,TAG設為1,將CELL的ID設為cell
相關代碼:
1 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 2 3 return 1 4 } 5 6 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 7 8 return 4 9 }10 11 12 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {13 let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell14 15 var label=cell.viewWithTag(1) as UILabel16 17 label.text="你好,哈哈哈"18 19 return cell20 }
二:TableView
1,拖動一個TableView
2,建立一個Cocoa Touch Class,命名為:MyTV
3,將1和2綁定,拖動一個cell至TableView,命名為cell
4,拖一個label,TAG設為1
相關代碼:
1 class MyTV: UITableView,UITableViewDataSource { 2 3 4 let data=["你好,我是一個人","didisswfids","w也是在要工"]
5 6 required init(coder aDecoder:NSCoder) 7 { 8 super.init(coder: aDecoder) 9 self.dataSource=self10 }11 12 func numberOfSectionsInTableView(tableView: UITableView) -> Int {13 return 114 }15 16 17 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {18 return 419 }20 21 22 23 24 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {25 26 27 let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell28 29 var label=cell.viewWithTag(1) as UILabel30 31 label.text=data[indexPath.row]
32 33 return cell34 }35 }
iOS開發學習筆記二:UITableView