Swift之UITableView

UITableView的相关操作

1.UITableView的基本设置

  <span style="font-size:14px;"> myTbView = UITableView(frame: CGRectMake(0, 20, width, height - 20), style: UITableViewStyle.Plain)
        //设置代理
        myTbView!.delegate = self
        myTbView!.dataSource = self
        self.view.addSubview(myTbView!)
        
        //设置分割线
        myTbView!.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
        myTbView!.separatorInset = UIEdgeInsetsZero
        
        //设置行高
        myTbView!.rowHeight = 40
        
        //设置尾部视图 省去多余的行
        myTbView!.tableFooterView = UIView()</span>

2.UITableView DataSource

1>设置显示的行数
<span style="font-size:14px;">func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int</span>
2>设置显示的分区
<span style="font-size:14px;">func numberOfSectionsInTableView(tableView: UITableView) -> Int</span>

3.UITableView Delegate

//显示cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //检索是否有可复用的cell
        var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("swift") as? UITableViewCell
        
        if cell == nil {
            //创建cell
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "swift")
        }
        //为cell上文本label赋值
        cell!.textLabel!.text = sourceArray![indexPath.row]
        
        return cell!
    }

    //选中单元格触发的事件
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //先取消选中效果
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        //获取当前显示标题
        var title:String = sourceArray![indexPath.row]
        
        //弹出一个警告框Controller
        var alert:UIAlertController = UIAlertController(title: "警告框", message: "您点击的是\(title)", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler:nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }

 //设置是否可以编辑
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    //给tableViewCell添加行滑动操作
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
         var showAction:UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "展示") { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
             println("打印---->\(self.sourceArray![indexPath.row])")
        }
        
        showAction.backgroundColor = UIColor.greenColor()
        
        var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "删除") { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            println("删除")
        }
        deleteAction.backgroundColor = UIColor.redColor()
        
        return [deleteAction,showAction]
    }
    //设置编辑模式
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.Delete
    }
    //处理编辑模式
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
    }
    
    //设置单元格是否可以移动
    func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    //移动单元格操作
    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        //表格改变
        tableView.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)
        
        //数据源改变
        var dest = sourceArray![destinationIndexPath.row]
        sourceArray![destinationIndexPath.row] = sourceArray![sourceIndexPath.row]
        sourceArray![sourceIndexPath.row] = dest
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值