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

今天让我来创建个简单的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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值