swift里UITableView简单的用法

/*
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellID)
需要和写在cellForRow里面的方法
tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
配对使用
*/

import UIKit

class TestViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    var tableView:UITableView!
    var dataArray = NSMutableArray()
    let cellID = "testCellID"
    var isEdit = false  // 判断tableview是否在编辑状态
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "UITableView"
        
        // 数据源数组
        self.dataArray = ["UIScrollView",
            "Xib自适应cell高度",
            "UICollectionView",
            "UIDatePicker",
            "UIAnimationViewController",
            "代码自定义cell",
            "XGBlockValueTest",
            "自定义View测试"
        ]
        // self.dataArray.addObject("heiehi")
        
        // 添加nav右侧按钮
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "编辑", style: UIBarButtonItemStyle.Done, target: self, action:Selector("rightBarButtonItemClicked"))
        
        // 初始化UITableView
        self.tableView = UITableView(frame: CGRectMake(0.0, 0.0, KLScreenWidth, KLScreenHeight), style: .Plain)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.rowHeight = 60.0
        self.tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellID)// cell的第二种写法 注册cell
        self.view.addSubview(self.tableView)
        
        // 去掉没有数据的cell的分割线
        self.tableView.tableFooterView = UIView()
       
    }
    
    // nav添加右按钮
    
    func rightBarButtonItemClicked(){
        
        
        if isEdit {
            self.tableView.setEditing(false, animated: true)
            isEdit = false
        }else{
            self.tableView.setEditing(true, animated: true)
            isEdit = true
        }
        
    }
    
    
    //  cell内容的显示
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        /*
        //cell的写法1:
        var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as? UITableViewCell
        if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellID)
        }
        cell!.textLabel!.text = self.dataArray[indexPath.row] as? String
        return cell!
        */
        
        // cell的写法2:
        //
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel!.text = self.dataArray[indexPath.row] as? String
        return cell
        
    }
    
    //  返回的cell的行数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
    }
    
    //  cell选中时的状态
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
    }
    
    // 设置返回的cell的高度
    //    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    //        return 60.0
    //    }
    
    // 可以被编辑
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    // 确定编辑模式(默认是滑动删除模式)
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        // 设置编辑模式为删除
        return  UITableViewCellEditingStyle.Delete
    }
    
    // 具体编辑操作(默认删除操作)
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
        // 先移除数据源数据
        self.dataArray.removeObjectAtIndex(indexPath.row)
        
        // 再动态刷新UITableView
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
    }
    
    // 允许移动某行(排序)
    func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    // 实现排序
    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        
        // 从数组中读取需要移动行的数据
        let object:AnyObject = self.dataArray[sourceIndexPath.row]
        
        // 在数组中先移除需要移动行的数据
        self.dataArray.removeObjectAtIndex(sourceIndexPath.row)
        
        // 把需要移动的cell数据插到到想要移动的数据前面
        self.dataArray.insertObject(object, atIndex: destinationIndexPath.row)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值