码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView使用

时间:2016-04-16 12:34:09      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

1.新建RootViewController类

 

 

[objc] view plaincopy技术分享技术分享
 
  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, animated: true)  
  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, animated: true)  
  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, target: self,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. }  

 

2.APPDelegate.swift调用

[objc] view plaincopy技术分享技术分享
 
  1. //  
  2. //  AppDelegate.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. @UIApplicationMain  
  12. class AppDelegate: UIResponder, UIApplicationDelegate {  
  13.                               
  14.     var window: UIWindow?  
  15.   
  16.   
  17.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {  
  18.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  19.         // Override point for customization after application launch.  
  20.         var rootView=RootViewController()  
  21.         var nav=UINavigationController(rootViewController:rootView)  
  22.         self.window!.rootViewController = nav;  
  23.   
  24.         self.window!.backgroundColor = UIColor.whiteColor()  
  25.         self.window!.makeKeyAndVisible()  
  26.           
  27.         return true  
  28.     }  
  29.   
  30.     func applicationWillResignActive(application: UIApplication) {  
  31.         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.  
  32.         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.  
  33.     }  
  34.   
  35.     func applicationDidEnterBackground(application: UIApplication) {  
  36.         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.  
  37.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  38.     }  
  39.   
  40.     func applicationWillEnterForeground(application: UIApplication) {  
  41.         // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.  
  42.     }  
  43.   
  44.     func applicationDidBecomeActive(application: UIApplication) {  
  45.         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.  
  46.     }  
  47.   
  48.     func applicationWillTerminate(application: UIApplication) {  
  49.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  50.     }  
  51.   
  52.   
  53. }  

3.效果

技术分享
技术分享
技术分享
 
 多谢分享

UITableView使用

标签:

原文地址:http://www.cnblogs.com/isItOk/p/5397942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!