swift4--UITableView表格的使用


import UIKit

class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource {
    
    var weeks = ["Monday","Tuesday","WednesDay","Thursday","Friday","Saturday","Sunday","Monday","Tuesday","WednesDay","Thursday","Friday","星期六星期天"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //在加载视图后进行任何其他设置,通常是从笔尖。
        
        //创建一个显示区域
        
        let rect = CGRect(x: 0, y: 40, width: 320, height: 420)
        
        //初始化一个表格视图
        
        let tableView = UITableView(frame: rect)
        
        //设置表格视图的代理为当前视图控制器类
        
        tableView.delegate = self
        
        //设置表格视图的数据源为当前视图控制器类
        
        tableView.dataSource = self
        
        //默认状态下开启表格的编辑模式
        
        tableView.setEditing(true, animated: true)
        
        //初始化一个索引路径对象,表示表格中的第一和第七行的位置
        
        let indexPath = IndexPath(row: 11, section: 0)
        
        //调用表格对像滚动到指定位置的方法,页面加载完成时滑动到指定索引位置

        tableView.scrollToRow(at:indexPath , at: UITableViewScrollPosition.top, animated: true)
        
        self.view.addSubview(tableView)
    }

//代理方法,设置单元格行数(该方法必须添加)
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return weeks.count
    }

//代理方法,用来设置表格行的高度为80
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

//代理方法,用来初始化表格单元或者复用表格单元(该方法必须添加)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //创建一个字符串,作为单元格的复用标示符
        
        let identifier = "reusedCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        
        // 如果没有可使用的单元格,则创建新的单元格,并拥有一个复用标示符
        
        if(cell == nil){
            
            cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: identifier)
            
        }
        
        // 索引路径用来表示单元格在表格中的位置,有section和row两个属性,前者表示在第几个段落,后者表示在第几行
        
        let rowNum = indexPath.row
        
        // 默认样式单元格拥有一个标签对象,在此设置标签对象的文字内容
        
        // 根据当前段落行数,设置单元格标题文字
        
        cell?.textLabel?.text = weeks[rowNum]
        
        // 在标签下方有一个描述文字标签
        
        cell?.detailTextLabel?.text = "Detail Information here"
        
        // 读取图片素材
        
        let img1 = UIImage(named: "pic1")
        
        let img2 = UIImage(named: "pic2")
        
        // 设置表格单元的默认图片为img1
        
        cell?.imageView?.image = img1
        
        // 设置表格单元高亮的图片为img2
        
        cell?.imageView?.highlightedImage = img2
        
        // 设置单元格的背景颜色
        
         cell?.backgroundColor = UIColor.blue
        
        // 注:也可以设置一个新的视图为单元格的背景视图
        
        let bgimg = UIImage(named: "img1")
        
        let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 80))
        
        imgView.image = bgimg
        
        cell?.backgroundView = imgView
        
        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, 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 = weeks[fromRow]

        // 删除数组中单元格移动前的对象

        weeks.remove(at: fromRow)

        // 然后在数组中的目标位置插入一份删除的对象,以同步数据源

        weeks.insert(obj, at: toRow)

    }

// 代理方法,用来设置单元格的编辑模式为删除模式

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

// return UITableViewCellEditingStyle.delete

// }

 //代理方法,用来响应单元格的删除事件

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

        // 判断如果编辑模式为删除,则执行之后的代码

         if(editingStyle == UITableViewCellEditingStyle.delete){

        // 获取单元格在段落中行数

         let rowNum = indexPath.row

        // 将该单元格的内容清除,以保证数据的一致性

         weeks.remove(at: rowNum)



        // 创建一个包含待删除单元格位置信息的数组

         let indexPaths = [indexPath]

        // 从表格中清除该单元格

         tableView.deleteRows(at: indexPaths, with: UITableViewRowAnimation.automatic)

         }

    }

// 代理方法,用来设置单元格的编辑模式为插入模式

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

//返回UITableViewCellEditingStyle.insert

//}

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

判断如果编辑模式为插入,则执行之后的代码

// if(editingStyle == UITableViewCellEditingStyle.insert){

获取单元格在段落中行数

//让rowNum = indexPath.row

往数组中同步插入新数据,及时更新数据源

// weeks.insert(“Honey Moon”,at:rowNum)

//

创建一个包含待插入单元格位置信息的数组

//让indexPaths = [indexPath]

往表格视图中指定的位置插入新单元格

// tableView.deleteRows(at:indexPaths,with:UITableViewRowAnimation.right)

//}

//}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

 

你可能感兴趣的:(学习笔记)