欢迎使用CSDN-markdown编辑器

1.前言

Swift在这就不多介绍了,想必大家都已皆知。

离Swift面世也过了有一个多月的时间。

在闲暇时间我用Swift实现了UITableView的一些常见操作。

基本都是可以用上的,今天在自己的博客里分享给大家。

2.初始化程序入口

初始化程序入口,先给我们的ViewController封装一个导航控制器

!代表不为nil,?表示可nil (!与?编译器会根据不同的标识来检测 )

1.新建RootViewController类

1.新建RootViewController类


[objc] view plaincopy在CODE上查看代码片派生到我的代码片  
//  
//  RootViewController.swift  
//  UITableViewDemo  
//  
//  Created by 赵超 on 14-6-21.  
//  Copyright (c) 2014年 赵超. All rights reserved.  
//  

import UIKit  

class RootViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {  

    var tableView : UITableView?  
    var items = ["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]  
    var leftBtn:UIButton?  
    var rightButtonItem:UIBarButtonItem?  

    override func viewDidLoad() {  
        super.viewDidLoad()  
        initView()  
        setupRightBarButtonItem()  
        setupLeftBarButtonItem()  
        self.leftBtn!.userInteractionEnabled = true  

        // Do any additional setup after loading the view.  
    }  

    func initView(){  
        // 初始化tableView的数据  
        self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)  
        // 设置tableView的数据源  
        self.tableView!.dataSource=self  
        // 设置tableView的委托  
        self.tableView!.delegate = self  
        //  
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")  
        self.view.addSubview(self.tableView!)  


    }  
    //加左边按钮  
    func setupLeftBarButtonItem()  
    {  
        self.leftBtn = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton  
        self.leftBtn!.frame = CGRectMake(0,0,50,40)  
        self.leftBtn?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)  
        self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)  
        self.leftBtn!.tag = 100  
        self.leftBtn!.userInteractionEnabled = false  
        self.leftBtn?.addTarget(self, action: "leftBarButtonItemClicked", forControlEvents: UIControlEvents.TouchUpInside)  
        var barButtonItem = UIBarButtonItem(customView: self.leftBtn)  
        self.navigationItem!.leftBarButtonItem = barButtonItem  
    }  
    //左边按钮事件  
    func leftBarButtonItemClicked()  
    {  
        println("leftBarButton")  
        if (self.leftBtn!.tag == 100)  
        {  
            self.tableView?.setEditing(true, animated: true)  
            self.leftBtn!.tag = 200  
            self.leftBtn?.setTitle("Done", forState: UIControlState.Normal)  
            //将增加按钮设置不能用  
            self.rightButtonItem!.enabled=false  
        }  
        else  
        {  
            //恢复增加按钮  
             self.rightButtonItem!.enabled=true  
            self.tableView?.setEditing(false, animated: true)  
            self.leftBtn!.tag = 100  
            self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)  
        }  

    }  

    //加右边按钮  
    func setupRightBarButtonItem()  
    {  
         self.rightButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self,action: "rightBarButtonItemClicked")  
        self.navigationItem!.rightBarButtonItem = self.rightButtonItem  

    }  
    //增加事件  
    func rightBarButtonItemClicked()  
    {  

        var row = self.items.count  
        var indexPath = NSIndexPath(forRow:row,inSection:0)  
        self.items.append("杭州")  
        self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)  


    }  


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

    //总行数  
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{  
        return self.items.count  
    }  

    //加载数据  
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{  

        let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell  
        var row=indexPath.row as Int  
        cell.textLabel.text=self.items[row]  
        cell.imageView.image = UIImage(named:"green.png")  
        return cell;  

    }  

    //删除一行  
   func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!){  
        var index=indexPath.row as Int  
        self.items.removeAtIndex(index)  
        self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)  
        NSLog("删除\(indexPath.row)")  
    }  
        //选择一行  
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){  
        let alert = UIAlertView()  
        alert.title = "提示"  
        alert.message = "你选择的是\(self.items[indexPath.row])"  
        alert.addButtonWithTitle("Ok")  
        alert.show()  
    }  



}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值