iOS开发学习笔记二:UITableView(1)

一: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 UITableViewCell
14         
15         var label=cell.viewWithTag(1) as UILabel
16         
17         label.text="你好,哈哈哈"
18        
19         return cell
20     }

二: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=self
10     }
11     
12     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
13         return 1
14     }
15     
16     
17     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
18         return 4
19     }
20 
21     
22     
23     
24     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
25         
26 
27         let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
28         
29         var label=cell.viewWithTag(1) as UILabel
30         
31         label.text=data[indexPath.row]
32         
33         return cell
34     }
35 }

 

posted @ 2015-01-27 16:04  chr1219  阅读(193)  评论(0编辑  收藏  举报