ios TableView那些事2(Swift 二) 初见TableView Grouped

来源:互联网 发布:ubuntu命令行调整音量 编辑:程序博客网 时间:2024/05/16 16:08

今天让我来创建个简单的Grouped


import UIKit


class ViewController: UITableViewController {

    let dataScoureArray:[String] = ["A","B","C","D","E","F","G","H"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    //cell Number
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        
        return dataScoureArray.count;
    }
    
    //section Number
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 2
    }
    //creat Cell
   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    
    let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"cell");

 
        cell.textLabel.text = dataScoureArray[indexPath.row]
        return cell;
    }
    
    //set Footer Height
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20;
    }
   
    //set Header Height
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20;
    }

    //set Header Title
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Hello"
    }
    
    //set Footer Title
    override func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return "World"
    }
    
    //set Cell Row Height
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50;
    }
    
    //cell  DidSelectAction
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        
    }



(二)在实际中我们要定制自己的tableview head 或footer


     // 在tableview 的headView 上添加个view 其实你可以在这个view 加很多组件 在添加在HeadView

   override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
    
        var view:UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20));
        view.backgroundColor = UIColor.yellowColor();
    
        return  view;
    
    }
   

   //在footerView 添加个button

    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?{
        
        let myButton:UIButton = UIButton(frame: CGRectMake(0, 0, self.view.frame.size.width, 20));
        myButton.setTitle("I am a Button", forState: UIControlState.Normal);
        myButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
        myButton.backgroundColor = UIColor.greenColor();
        return myButton;

    }



0 0
原创粉丝点击