当前位置:首页 > 操作系统 > Ios

iOS开发学习笔记二:UITableView

一: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 3return1 4    }
 5 6override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 7 8return4 9    }
101112override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
13         let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
1415         var label=cell.viewWithTag(1) as UILabel
1617         label.text="你好,哈哈哈"1819return 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    }
1112     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
13return114    }
151617     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
18return419    }
2021222324     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
252627         let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
2829         var label=cell.viewWithTag(1) as UILabel
3031         label.text=data[indexPath.row]
            32
            33
            return
             cell

            34
                }

            35 }

 

原文:http://www.cnblogs.com/chr1219/p/4253244.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!