iOS- Swift实现UITableView的常见操

1.前言

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

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

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

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

2.初始化程序入口

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

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

1.新建RootViewController类

 

 

[objc] view plaincopy 在CODE上查看代码片 派生到我的代码片
 
  1. //  
  2. //  RootViewController.swift  
  3. //  UITableViewDemo  
  4. //  
  5. //  Created by 赵超 on 14-6-21.  
  6. //  Copyright (c) 2014年 赵超. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. class RootViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {  
  12.       
  13.     var tableView : UITableView?  
  14.     var items = ["武汉","上海","北京","深圳","广州","重庆","香港","台海","天津"]  
  15.     var leftBtn:UIButton?  
  16.     var rightButtonItem:UIBarButtonItem?  
  17.       
  18.     override func viewDidLoad() {  
  19.         super.viewDidLoad()  
  20.         initView()  
  21.         setupRightBarButtonItem()  
  22.         setupLeftBarButtonItem()  
  23.         self.leftBtn!.userInteractionEnabled = true  
  24.   
  25.         // Do any additional setup after loading the view.  
  26.     }  
  27.   
  28.     func initView(){  
  29.         // 初始化tableView的数据  
  30.         self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)  
  31.         // 设置tableView的数据源  
  32.         self.tableView!.dataSource=self  
  33.         // 设置tableView的委托  
  34.         self.tableView!.delegate = self  
  35.         //  
  36.         self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier"cell")  
  37.         self.view.addSubview(self.tableView!)  
  38.          
  39.           
  40.     }  
  41.     //加左边按钮  
  42.     func setupLeftBarButtonItem()  
  43.     {  
  44.         self.leftBtn = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton  
  45.         self.leftBtn!.frame = CGRectMake(0,0,50,40)  
  46.         self.leftBtn?.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)  
  47.         self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)  
  48.         self.leftBtn!.tag = 100  
  49.         self.leftBtn!.userInteractionEnabled = false  
  50.         self.leftBtn?.addTarget(self, action"leftBarButtonItemClicked", forControlEvents: UIControlEvents.TouchUpInside)  
  51.         var barButtonItem = UIBarButtonItem(customView: self.leftBtn)  
  52.         self.navigationItem!.leftBarButtonItem = barButtonItem  
  53.     }  
  54.     //左边按钮事件  
  55.     func leftBarButtonItemClicked()  
  56.     {  
  57.         println("leftBarButton")  
  58.         if (self.leftBtn!.tag == 100)  
  59.         {  
  60.             self.tableView?.setEditing(true, animatedtrue)  
  61.             self.leftBtn!.tag = 200  
  62.             self.leftBtn?.setTitle("Done", forState: UIControlState.Normal)  
  63.             //将增加按钮设置不能用  
  64.             self.rightButtonItem!.enabled=false  
  65.         }  
  66.         else  
  67.         {  
  68.             //恢复增加按钮  
  69.              self.rightButtonItem!.enabled=true  
  70.             self.tableView?.setEditing(false, animatedtrue)  
  71.             self.leftBtn!.tag = 100  
  72.             self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)  
  73.         }  
  74.           
  75.     }  
  76.       
  77.     //加右边按钮  
  78.     func setupRightBarButtonItem()  
  79.     {  
  80.          self.rightButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, targetself,action: "rightBarButtonItemClicked")  
  81.         self.navigationItem!.rightBarButtonItem = self.rightButtonItem  
  82.   
  83.     }  
  84.     //增加事件  
  85.     func rightBarButtonItemClicked()  
  86.     {  
  87.           
  88.         var row = self.items.count  
  89.         var indexPath = NSIndexPath(forRow:row,inSection:0)  
  90.         self.items.append("杭州")  
  91.         self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)  
  92.       
  93.       
  94.     }  
  95.       
  96.       
  97.     override func didReceiveMemoryWarning() {  
  98.         super.didReceiveMemoryWarning()  
  99.         // Dispose of any resources that can be recreated.  
  100.     }  
  101.       
  102.     //总行数  
  103.     func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{  
  104.         return self.items.count  
  105.     }  
  106.       
  107.     //加载数据  
  108.     func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{  
  109.   
  110.         let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell  
  111.         var row=indexPath.row as Int  
  112.         cell.textLabel.text=self.items[row]  
  113.         cell.imageView.image = UIImage(named:"green.png")  
  114.         return cell;  
  115.   
  116.     }  
  117.   
  118.     //删除一行  
  119.    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!){  
  120.         var index=indexPath.row as Int  
  121.         self.items.removeAtIndex(index)  
  122.         self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)  
  123.         NSLog("删除\(indexPath.row)")  
  124.     }  
  125.         //选择一行  
  126.     func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){  
  127.         let alert = UIAlertView()  
  128.         alert.title = "提示"  
  129.         alert.message = "你选择的是\(self.items[indexPath.row])"  
  130.         alert.addButtonWithTitle("Ok")  
  131.         alert.show()  
  132.     }  
  133.   
  134.       
  135.   
  136. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值