UItableView高级功能

 class UITableViewControllerAF: UIViewController, UITableViewDataSource, UITableViewDelegate  {

     var titleString:String!

     @IBOutlet var titleLabel:UILabel!
     @IBOutlet var listTableView : UITableView!
     @IBOutlet var editDoneButton : UIButton!

     //定义数组
     var items:[String] = ["北京",
         "上海",
         "天津",
         "山东",
         "河北",
         "湖北"]

     //返回按钮事件
     @IBAction func backButtonClick()
     {
         self.navigationController?.popViewControllerAnimated(true)
     }

     //编辑按钮事件
     @IBAction func editButtonClick()
     {
         if editDoneButton.titleForState(UIControlState.Normal) == "编辑"
         {
             //如果按钮标题是编辑,则将表视图设置成可编辑状态,并修改button标题为“完成”
             listTableView.setEditing(true, animated: true)
             editDoneButton.setTitle("完成", forState: UIControlState.Normal)
         }else
         {
             //如果按钮标题是完成,则设置成不可编辑,并修改button标题为“编辑”
             listTableView.setEditing(false, animated: true)
             editDoneButton.setTitle("编辑", forState: UIControlState.Normal)
         }
     }

     //自定义添加按钮事件
     @IBAction func addButtonClick()
     {
         //数组添加新数据
         items.insert()

         //初始化一个NSIndexPath对象,指定要添加的单元格位置
         let indexPath = NSIndexPath(forRow: , inSection: )

         //在指定位置上添加一个新的单元格
         self.listTableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
     }

     override func viewDidLoad() {
         super.viewDidLoad()

         titleLabel.text = titleString

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

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

     /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
     }
     */

     //MARK: - UITableViewDelegate

     //tableView数据源:返回几节(组)
     func numberOfSectionsInTableView(tableView: UITableView) -> Int
     {

     }
     //tableView数据源:返回每一节行数
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
     {
         return items.count  //返回数组数量
     }

     //行缩进
     func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int
     {
         return indexPath.row
     }

     //tableView 数据源:每一行高度
     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
     {

     }

     //tableView数据源:每一行内容
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
     {
         //Cell标示符,代表一系列
         // OC:使用static,  swift:使用let
         let cellIdentifier: String = "cellIdentifier"

         //通过cellIdentifier标示符取没有使用的Cell
         //有可能不存在,所以使用:optional
         var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell

         //如果cell取到是空
         if cell == nil { // no value

             //创建新的cell
             //cell样式:UITableViewCellStyle.Default
             //cell标示符:cellIdentifier
             cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)

             //设置字体
 //            cell!.textLabel.font = UIFont.systemFontOfSize(14)
             //2015年4月10号修改
             cell!.textLabel?.font = UIFont.systemFontOfSize()

             //设置选中cell样式
             cell!.selectionStyle = .Gray;

             //设置cell后面箭头样式
             cell!.accessoryType = .DisclosureIndicator;
         }

         //去当前行
         var row=indexPath.row as Int

         //从数组取对应值给cell赋值
 //        cell!.textLabel.text = self.items[row]
         //2015年4月10号修改
         cell!.textLabel?.text = self.items[row]

         //设置cell图片
 //        cell!.imageView.image = UIImage(named:"cellImage.png")
         //2015年4月10号修改
         cell!.imageView?.image = UIImage(named:"cellImage.png")

         return cell!;
     }

     //确定每一行 是否可以编辑
     func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
     {
         return true
     }

     //返回每一行 操作类型
     func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle
     {
         //最后一行允许插入
         {
             return .Insert
         }

         return .Delete   //允许删除操作
     }

     //在编辑状态,可以拖动设置cell位置
     func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
     {
         return true
     }

     //编辑cell事件
     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
     {
         //如果是删除操作
         if editingStyle == UITableViewCellEditingStyle.Delete
         {
             items.removeAtIndex(indexPath.row)//将数据源数组删除对应行数数据

             //table表删除该行
             tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
         }
         //如果是插入一行操作
         else if editingStyle == UITableViewCellEditingStyle.Insert
         {
             //数组添加一条新数据
             items.append("新城市 \(items.count)")

             //表视图插入一条单元格
             tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Middle)
         }
     }

     //移动cell事件
     func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
         if fromIndexPath != toIndexPath{

             //获取移动行对应的值
             var itemValue:String = items[fromIndexPath.row]

             //删除移动的行的值
             items.removeAtIndex(fromIndexPath.row)

             //如果移动区域大于现有行数,直接在最后添加移动的值
             if toIndexPath.row > items.count{
                 items.append(itemValue)
             }else{ //没有超出最大行数,则在目标位置添加刚才删除的值
                 items.insert(itemValue, atIndex: toIndexPath.row)
             }
         }
     }

     //tableView代理:点击一行
     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
     {
         //释放选中效果
         tableView.deselectRowAtIndexPath(indexPath, animated: true)
     }
 }
 

iOS开发——UI篇Swift篇&玩转UItableView(二)高级功能的更多相关文章

  1. iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用

    swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...

  2. iOS开发——新特性Swift篇&Swift 2.0 异常处理

    Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...

  3. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  4. iOS开发——网络编程Swift篇&Alamofire详解

    Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...

  5. ios开发——实用技术总结Swift篇&swift常用开发技术总结

    swift常用开发技术总结 懒加载:属性,数组(字典),控件... 数组(懒加载): lazy var shops:Array<Dictionary<String, String>& ...

  6. iOS开发——网络编程Swift篇&(八)SwiftyJSON详解

    SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...

  7. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  8. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

  9. iOS开发零基础--Swift篇 元组

    元组的介绍 元组是Swift中特有的,OC中并没有相关类型 它是什么呢? 它是一种数据结构,在数学中应用广泛 类似于数组或者字典 可以用于定义一组数据 组成元组类型的数据可以称为“元素” 元组的定义 ...

  10. iOS开发零基础--Swift篇 循环

    循环的介绍 在开发中经常会需要循环 常见的循环有:for/while/do while. 这里我们只介绍for/while,因为for/while最常见 for循环的写法 最常规写法 // 传统写法 ...

随机推荐

  1. 传统企业,"哀兵必胜"的想法要不得

    [文/ 任英杰]同事在内网上发了一篇文章『哀兵必胜』,思量数日,作文应对.文中表达的积极精神让人敬佩,但背后似乎隐含着一股莫名的“情绪”.对行业大格局的基本看法会影响公司转型的成败,觉得还是有必要讨论 ...

  2. 动手动脑之查看String.equals()方法的实现代码及解释

    动手动脑 请查看String.equals()方法的实现代码,注意学习其实现方法. 第一个是false,后三个是true. package stringtest; public class Strin ...

  3. [LeetCode] Missing Number (A New Questions Added Today)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  4. (转)MFC消息机制

    转自:http://blog.csdn.net/kongfuxionghao/article/details/35882533

  5. js数组依据下标删除元素

    最近在项目中遇到了一些问题,基础性的东西记得不牢固,就总结一下放在这里备再次查找,对操作js数组的一些问题一些常用的记录! 1.创建数组 var array = new Array(); var ar ...

  6. 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)

    利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引) ##应用情景 项目中有大量的SQL,尤其是涉及到统计报表时,表关联比较多,当初开发建表时也没搞好索引关联的,上线后 ...

  7. 将dataset写入数据库

    最近在将excel中的文件导入到数据库中,用程序进行编写,由于数据量较大所以速度很慢,后来采用了SqlBulkCopy类,解决了速度的问题,我就insert语句,sqldataadapter.upda ...

  8. mysql如果搜索长度过宽 导致显示不全的情况解决

    今天我在搜索数据库里面优惠码字段 直接使用 select * from table 的命令的时候 由于 第一个 字段过长导致后面的都无法显示全..我还是宽屏! 所以 搜索了一下 可以让 它单行显示 使 ...

  9. 第二百三十一天 how can I 坚持

    哎,蛋疼的一天,一点破问题搞了一下午,还没搞利索. 他们要组织出去玩,我没有参加啊,随便找了个借口. 博客园的字体怎么变小了呢,看着好难受啊,昨天传照片传的? 睡觉.外边下着雨呢,喜欢下雨的夏天还有下 ...

  10. MySQL 查看表结构简单命令。

    一.简单描述表结构,字段类型 desc tabl_name; 显示表结构,字段类型,主键,是否为空等属性,但不显示外键. 二.查询表中列的注释信息 select * from information_ ...