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)
     }
 }
 

最新文章

  1. implicit和explicit的基本使用
  2. Python爬虫学习笔记——豆瓣登陆(三)
  3. weiapi2.2 HelpPage自动生成接口说明文档和接口测试功能
  4. SqlServer中创建Oracle连接服务器
  5. 寒假训练第九场 Brocard Point of a Triangle
  6. RM-Linux驱动--Watch Dog Timer(看门狗)驱动分析
  7. 欢迎使用skymvc框架,简单易用的php框架
  8. [Java] 可运行 jar 记录
  9. 从客户端(txtNewsContent="<hr />")中检测到有潜在危险的 Request.Form 值。怎么办呀?
  10. 转载: Nova-Router 分析
  11. 开源Math.NET基础数学类库使用(16)C#计算矩阵秩
  12. KeyTweak(笔记本键盘设置工具) V2.20 中文版
  13. [2014-08-18]初尝 AspNet vNext On Mac
  14. mysql数据库第一弹
  15. [Linux]安装node.js
  16. MySQL数据库的安装与基本操作
  17. MySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost'解决
  18. Ubuntu14.04(server amd 64)编译安装 ceres-solver
  19. (2)bytes类型
  20. Laravel路由除了根目录全报404错误

热门文章

  1. python中的深拷贝与浅拷贝
  2. 高薪诚聘.NET MVC开发工程师
  3. NET知识大纲
  4. Hadoop 问题 & 解决
  5. 安装 nodejs图像处理模块 sharp
  6. Spark的发展历程
  7. (转载)javascript函数作用域和提前声明
  8. jQuery在updatepanel中使用造成内存泄露
  9. oracle客户端安装及Plsql devloper连接
  10. Web服务器与Web系统发布