IOS控件-UITableVIew

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21153627/article/details/84133909

UITableVIew的简单实用

实现效果

//
//  ViewController.swift
//  DemoApp
//
//  Created by 郭文亮 on 2018/11/15.
//  Copyright © 2018年 finalliang. All rights reserveds
//
import UIKit
//添加两个代理协议 表格视图的数据源协议 表格视图的代理协议
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
 
    override func viewDidLoad() {
        super.viewDidLoad();
        let tableView = UITableView(frame: self.view.bounds)
        //设置表格视图的代理为根视图控制器
        tableView.delegate = self
         //设置表格视图的资源代理为根视图控制器
        tableView.dataSource = self
        
        //初始化一个索引路径。第1个段落的第16行
        let indexpath = IndexPath(row: 49, section:0)
        //让表格滑动到指定位置
        tableView.scrollToRow(at: indexpath, at: UITableViewScrollPosition.bottom, animated: true)
        
        self.view.addSubview(tableView)
    }
    //代理方法。重写的。设置表格视图拥有单元格的行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    //代理方法。设置高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    //初始化或复用表格中的单元格
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let indetifier = "reusedCell"
        //单元格的标识符。可以看作是一种复用机制。 此方法可以从已开辟内存的单元格里面选择一个具有同样标识的空闲的单元格
        var cell = tableView.dequeueReusableCell(withIdentifier: indetifier)
        //判断在可重用单元格队列中,是否有可以重复使用的单元格
        if (cell==nil) {
            //如果没有可以重复使用的单元格,则创建新的单元格。
            //新的单元格具有i 系统默认的样式,并拥有一个复用标识符
            cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: indetifier)
        }
        //索引路径用来标示单元格在表格中的位置 段落section  行数row
        let rowNum = (indexPath as NSIndexPath).row
        
        //默认的单元格,拥有一个标签对象  对象的文字内容
        cell?.textLabel?.text = "第\(rowNum)个"
        //设置标签的描述内容
        cell?.detailTextLabel?.text = "Detail information here"
        cell?.imageView?.image=UIImage(named: "Tab")
        cell?.imageView?.highlightedImage = UIImage(named: "Tab2")
        //设置背景色的两种方式
        if (rowNum==3) {
            cell?.backgroundColor=UIColor.red
        }else{
            let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            view.backgroundColor=UIColor.blue
            cell?.backgroundView=view
        }
        
        return cell!
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

自定义Tableview的Accessory样式

import UIKit
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
 
    override func viewDidLoad() {
        super.viewDidLoad();
        let tableView = UITableView(frame: self.view.bounds)
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let indetifier = "reusedCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: indetifier)
        if (cell==nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: indetifier)
        }
        let rowNum = (indexPath as NSIndexPath).row
        cell?.textLabel?.text = "第\(rowNum)个"
        return cell!
    }
    //添加一个代理方法 处理单元格的点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //获取被点击的单元格
        let cell=tableView.cellForRow(at: indexPath)
        //如果被点击的单元格 没有显示附加图标 则显示复选标记图标  表示当前单元格被选中
        if (cell?.accessoryType == UITableViewCellAccessoryType.none) {
            cell?.accessoryType=UITableViewCellAccessoryType.checkmark
        }else{
            //如果被点击的已显示复选图标。则隐藏
            cell?.accessoryType = UITableViewCellAccessoryType.none
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

单元格的删除

import UIKit
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
    var months = ["1","2","3","4","5","6","7"]
    
    override func viewDidLoad() {
        super.viewDidLoad();
        let tableView = UITableView(frame: self.view.bounds)
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return months.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let indetifier = "reusedCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: indetifier)
        if (cell==nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: indetifier)
        }
        let rowNum = (indexPath as NSIndexPath).row
        cell?.textLabel?.text = months[rowNum]
        return cell!
    }
    //添加一个代理方法 处理单元格的点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //获取被点击的单元格
        let cell=tableView.cellForRow(at: indexPath)
        //如果被点击的单元格 没有显示附加图标 则显示复选标记图标  表示当前单元格被选中
        if (cell?.accessoryType == UITableViewCellAccessoryType.none) {
            cell?.accessoryType=UITableViewCellAccessoryType.checkmark
        }else{
            //如果被点击的已显示复选图标。则隐藏
            cell?.accessoryType = UITableViewCellAccessoryType.none
        }
    }
    //添加一个代理方法 改变编辑模式
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        //设置为删除模式
        return UITableViewCellEditingStyle.delete
    }
    
    //响应单元格的删除事件
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        //判断如果编辑模式为删除 则执行删除代码
        if editingStyle == UITableViewCellEditingStyle.delete {
            let rowNum = (indexPath as NSIndexPath).row
            months.remove(at: rowNum)
            //创建一个待删除单元格的位置信息数组
            let indexPaths = [indexPath]
            tableView.deleteRows(at: indexPaths, with: UITableViewRowAnimation.automatic)
            
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

插入单元格

import UIKit
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
    var months = ["1","2","3","4","5","6","7"]
    
    override func viewDidLoad() {
        super.viewDidLoad();
        let tableView = UITableView(frame: self.view.bounds)
        tableView.delegate = self
        tableView.dataSource = self
        //在默认状态下开启表格的默认编辑模式
        tableView.setEditing(true, animated: true)
        self.view.addSubview(tableView)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return months.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let indetifier = "reusedCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: indetifier)
        if (cell==nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: indetifier)
        }
        let rowNum = (indexPath as NSIndexPath).row
        cell?.textLabel?.text = months[rowNum]
        return cell!
    }
    //添加一个代理方法 处理单元格的点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //获取被点击的单元格
        let cell=tableView.cellForRow(at: indexPath)
        //如果被点击的单元格 没有显示附加图标 则显示复选标记图标  表示当前单元格被选中
        if (cell?.accessoryType == UITableViewCellAccessoryType.none) {
            cell?.accessoryType=UITableViewCellAccessoryType.checkmark
        }else{
            //如果被点击的已显示复选图标。则隐藏
            cell?.accessoryType = UITableViewCellAccessoryType.none
        }
    }
    //添加一个代理方法 改变编辑模式
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        //设置为插入模式
        return UITableViewCellEditingStyle.insert
    }
    
    //响应单元格的插入事件
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        //判断如果编辑模式为插入 则执行插入代码
        if editingStyle == UITableViewCellEditingStyle.insert {
            let rowNum = (indexPath as NSIndexPath).row
            months.insert("honey moon", at: rowNum)
            let indexPaths = [indexPath]
            tableView.insertRows(at: indexPaths, with: UITableViewRowAnimation.right)
            
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

调整单元格顺序

import UIKit
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource{
    var months = ["1","2","3","4","5","6","7"]
    
    override func viewDidLoad() {
        super.viewDidLoad();
        let tableView = UITableView(frame: self.view.bounds)
        tableView.delegate = self
        tableView.dataSource = self
        //在默认状态下开启表格的默认编辑模式
        tableView.setEditing(true, animated: false)
        self.view.addSubview(tableView)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return months.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let indetifier = "reusedCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: indetifier)
        if (cell==nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: indetifier)
        }
        let rowNum = (indexPath as NSIndexPath).row
        cell?.textLabel?.text = months[rowNum]
        return cell!
    }
    //添加一个代理方法 处理单元格的点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //获取被点击的单元格
        let cell=tableView.cellForRow(at: indexPath)
        //如果被点击的单元格 没有显示附加图标 则显示复选标记图标  表示当前单元格被选中
        if (cell?.accessoryType == UITableViewCellAccessoryType.none) {
            cell?.accessoryType=UITableViewCellAccessoryType.checkmark
        }else{
            //如果被点击的已显示复选图标。则隐藏
            cell?.accessoryType = UITableViewCellAccessoryType.none
        }
    }
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.none
    }
    //添加一个代理 用来设置单元格是否允许拖动换行
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    //添加一个代理 响应单元格的移动事件
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let fromRow = sourceIndexPath.row//移动前的位置
        let toRow = destinationIndexPath.row//移动后的位置
        
        let obj = months[fromRow]//移动钱的对象
        
        months.remove(at: fromRow)
        months.insert(obj, at: toRow)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

猜你喜欢

转载自blog.csdn.net/qq_21153627/article/details/84133909