美文网首页
8.2 UiTabelView

8.2 UiTabelView

作者: jayck | 来源:发表于2016-08-02 20:53 被阅读25次

    8.2 UiTabelView

    整个Tabelview有头部,和尾部。里面可能有数个分组section,分组中又有分组头部和分组尾部,分组中又有很多行Cell

    1.行高,默认44个像素
    2.行数
    3.每一行的内容(数据)

    tableView,Cell ,Row,Section,主线程,加载数据

    1. 关键词:tableView header footer section Cell /Row枚举类型
    2. Cell、Header、Footer宽度一定与TableView相同x/y/width无效(TableView中)3. 同一个Cell对象会重复使用,在队列中获取空闲的Cell
      var cell = tableView.dequeueReusableCellWithIdentifier("cell")
    3. 几个关键函数:numberOfSectionsInTableViewnumberOfRowsInSectioncellForRowAtIndexPathdidSelectRowAtIndexPathviewForHeaderInSectionheightForHeaderInSection
    4. 建立一个类比建立数组或者字典要优越的多,可以在敲代码的时候弹出类中的成员变量比如: var channel_id: String!var channel_name: String!
    5. 在refreshTableView放入主线程中执行方法一:self.tableView.performSelectorOnMainThread(#selector(self.tableView.self.reloadData), withObject: nil, waitUntilDone: false)方法二:self.performSelectorOnMainThread(#selector(self.refreshTableView), withObject: nil, waitUntilDone: true)
    6. tableView.reloadData() //重新加载
    import UIKit
    
    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        var names: [String]?                  //定义一个数组
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
    
            names = ["张三", "李四", "王五", "赵六"]
    
            
    
            //.Plain样式默认没有分隔,  .Grouped有分隔
    
            let tableView = UITableView(frame: self.view.bounds, style: .Grouped)
    
            tableView.dataSource = self
    
            tableView.delegate = self
    
            self.view.addSubview(tableView)
    
            
    
            //Cell、Header、Footer宽度一定与TableView相同
    
            //x/y/width无效,只有高度height有效
    
            let headView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
    
            headView.backgroundColor = UIColor.redColor()
    
            tableView.tableHeaderView = headView         //定义头部为红色,50像素高
    
            
    
            let footView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
    
            footView.backgroundColor = UIColor.greenColor()
    
            tableView.tableFooterView = footView         //定义尾部为绿色,50像素高
    
        }
    
        
    
        func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    
            return 2
    
        }
    
        //询问某个section中有多少条数据
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    
            return names!.count
    
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
    
            
    
            //同一个Cell对象会重复使用
    
            //1. 在队列中获取空闲的Cell
    
            var cell = tableView.dequeueReusableCellWithIdentifier("cell")
    
            if cell == nil {
    
                //2. 创建可以重用的Cell对象
    
                cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
    
            }
    
            
    
            //3. 设置内容
    
            cell?.textLabel?.text = names![indexPath.row]
    
    //        cell?.detailTextLabel?.text = "xxxxx"
    
            return cell!
    
        }
    
        
    
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
    
            print(indexPath.section, indexPath.row)
    
            print(names![indexPath.row])
    
        }
    
    //    
    
    //    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
    //        return "Section \(section) Header"       //显示Section头部名称
    
    //    }
    
    //    
    
    //    func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    
    //        return "Section \(section) Footer"       //显示Section尾部名称
    
    //    }
    
        
    
        func tableView(tableView: UITableView, viewForHeaderInSection section: Int) ->UIView? {
    
            let v = UIView()
    
            v.backgroundColor = UIColor.blueColor()
    
            return v                        //定义Section的头部为蓝色
    
        }
    
        
    
        func tableView(tableView: UITableView, heightForHeaderInSection section: Int) ->CGFloat {
    
            return 44.0                     //定义Section的头部为44个像素高
    
        }
    }
    

    相关文章

      网友评论

          本文标题:8.2 UiTabelView

          本文链接:https://www.haomeiwen.com/subject/kgfqsttx.html