UI控件之UITableView初步使用

//  UITableView

//

//  Created by Catherine on 2017/8/31.

//  Copyright © 2017 Catherine. All rights reserved.

//   UITableView   要有数据源 delegate   必不可少的  行数、cellforrowatindexPath


import UIKit


class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

    

    var dataArray:Array<String>?

    

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let tableView:UITableView =UITableView(frame: self.view.frame, style:UITableViewStyle.grouped)

        //还有一种类型是 分组,秩序更改section return行数  即可

        tableView.dataSource =self

        tableView.delegate =self

        dataArray = ["1","2","3","4","5","6","7","8","9","10"]

        //在数据源进行改变后会对数据进行刷新

        tableView.reloadData()

        //设置tableview的回弹效果0000拉到头不会变灰

        tableView.bounces =false

        //设置tableview的状态

        tableView.isEditing =true

        self.view.addSubview(tableView)

    }

    //实现具体的编辑状态

    func tableView(_ tableView:UITableView, editingStyleForRowAt indexPath:IndexPath) -> UITableViewCellEditingStyle {

        

        returnUITableViewCellEditingStyle.delete

        

    }

    //具体的逻辑操作

    func tableView(_ tableView:UITableView, commit editingStyle:UITableViewCellEditingStyle, forRowAt indexPath:IndexPath) {

        if editingStyle ==UITableViewCellEditingStyle.delete{

            dataArray?.remove(at: indexPath.row)}

        

        

        tableView.reloadData()

        

    }

    //设置tableview的移动

    func tableView(_ tableView:UITableView, moveRowAt sourceIndexPath:IndexPath, to destinationIndexPath:IndexPath) {

        

        let str =dataArray?[sourceIndexPath.row]

        dataArray?.remove(at: sourceIndexPath.row)

        dataArray?.insert(str!, at: destinationIndexPath.row)

    }

    //设置分区数

    func numberOfSections(in tableView:UITableView) -> Int {

        return1

    }

    //设置每个分区的行数

    func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {

        return (dataArray?.count)!

    }

    //填充每一行的内容

    func tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell {

        //indexPath .row第几行

        

        //支持cell复用        ---- 一屏幕中有多少行一共就创建多少个cell

        var cell = tableView.dequeueReusableCell(withIdentifier:"cellid")

        if cell ==nil{

            cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier:"cellid")

            //选中cell

            cell?.selectionStyle =UITableViewCellSelectionStyle.gray

            

        }

        cell?.textLabel?.text =dataArray?[indexPath.row]

        /*静态cell  0000 动态cell   ----有多少行创建多少cell

        let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"" )

        cell.textLabel?.text = dataArray?[indexPath.row]*/

        return cell!

    }

    //设置头视图的高度

    func tableView(_ tableView:UITableView, heightForHeaderInSection section:Int) -> CGFloat {

        return100

    }

    //设置尾视图的高度

    func tableView(_ tableView:UITableView, heightForFooterInSection section:Int) -> CGFloat {

        return100

    }

    //设置每一行的高度

    func tableView(_ tableView:UITableView, heightForRowAt indexPath:IndexPath) -> CGFloat {

        //默认44

        return60

    }

    //设置头视图

    func tableView(_ tableView:UITableView, viewForHeaderInSection section:Int) -> UIView? {

        let view:UIView =UIView(frame: CGRect(x:0, y: 0, width:320, height: 100))

        view.backgroundColor =UIColor.black

        return view

    }

    //设置尾视图

    func tableView(_ tableView:UITableView, viewForFooterInSection section:Int) -> UIView? {

        let view:UIView =UIView(frame: CGRect(x:0, y: 0, width:320, height: 100))

        view.backgroundColor =UIColor.purple

        return view

    }

    //选中行数的交互

    func tableView(_ tableView:UITableView, didSelectRowAt indexPath:IndexPath) {

        //didselectrowat 这个是选中的第几行的。

        

    }

    //代理方法

    //取消选中某个cell时触发的方法

    func tableView(_ tableView:UITableView, didDeselectRowAt indexPath:IndexPath) {

    

    }

    //返回标题

    func tableView(_ tableView:UITableView, titleForFooterInSection section:Int) -> String? {

        return"footer"

    }

    func tableView(_ tableView:UITableView, titleForHeaderInSection section:Int) -> String? {

        return"header"

    }

    func tableView(_ tableView:UITableView, willSelectRowAt indexPath:IndexPath) -> IndexPath? {

        return indexPath

        //返回实际上展现给用户的cell行数 不是用户真实点中的 实现一个转换

    }

    

    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


猜你喜欢

转载自blog.csdn.net/catherine981234/article/details/77751063