UITableiView


使用UITableView有两种方式:

1、使用TabViewController

2、继承UITableView


TableViewController

1,拖动一个TableViewController。选中,然后点击Editor -》 Embed in -》Navegation Controller。然后界面如下所示






2,创建一个Cocoa Touch类继承UITableViewController


class TableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    // 这里表示tableview有个部分,这个值不能是0,如果是0将没有东西显示
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    // 一部分有几行
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 3
    }

    // 这里对cell初始化
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel.text = "item 1"
        // Configure the cell...

        return cell
    }

}

3,在storyboard设置这个类





4,设置TableViewCell的style





可以选择tableViewCell的样式



5,设置TableViewCell的identifier




设置好后左侧面板的Table View cell应该变成了你设置的那个名字



6,在TableViewController 类的tableView方法中对cell赋值

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 第一个参数就是之前设置的identifier属性
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = "item "

        return cell
    }

7,选择一个平台,然后点击三角形运行



8,效果图:





自定义Table View Cell

1,选择style为custom


2,给Table View Cell添加一个Label

为了方便演示我只拖了一个label


3,创建一个类继承UITableViewCell



4,修改Table View Cell对应的类为创建的那个类



5,修改TableViewController中方法



6,效果图



UITableView

1,拖拽一个TableView到View Controller



2,创建一个类继承UITableView

class MyTableView: UITableView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}

3,添加一个初始化方法

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.dataSource = self
        
    }

4,实现UITableDataSource

<pre name="code" class="javascript">class MyTableView: UITableView , UITableViewDataSource {

 
 

5,添加两个方法

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 3
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        // 这里记得要到tableViewCell设置identifier
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        
        cell.textLabel?.text = "This is Title"
        cell.detailTextLabel?.text = "This is detail"
        
        return cell
    }

6,设置到TableView



7,运行



完整代码

class MyTableView: UITableView , UITableViewDataSource {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.dataSource = self
        
    }
    
   
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 3
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        
        cell.textLabel?.text = "This is Title"
        cell.detailTextLabel?.text = "This is the detail"
        
        return cell
    }

}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值