[IOS]tableView的操作。
来源:互联网 发布:qt ros 显示界面编程 编辑:程序博客网 时间:2024/05/22 00:10
//// ViewController.swift// try//// Created by Stary on 3/28/16.// Copyright © 2016 Stary. All rights reserved.//import UIKitclass NewTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { var array = [String](count: 50, repeatedValue: "yan") var arrayIsMarked = [Bool](count: 51, repeatedValue: false) // 必备的两个函数。 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } // 对每一个cell的操作。 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // 每个自定义的cell都有一个identify let identistring = "trys" let cell = tableView.dequeueReusableCellWithIdentifier(identistring, forIndexPath: indexPath) as! NewTableViewCell cell.ClassLabel.text = "Variable" cell.DescriptionLabel.text = "Let's try" cell.NewimageView.image = UIImage(named: "p1.jpg") if arrayIsMarked[indexPath.row] { cell.accessoryType = .Checkmark } else { cell.accessoryType = .None } return cell } // 按住某个cell的反应。 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let controller = UIAlertController(title: "Thanks", message: "\(array[0])", preferredStyle: .Alert) let okAction = UIAlertAction(title: "Ok", style: .Default, handler: { (action : UIAlertAction) -> Void in let cell = tableView.cellForRowAtIndexPath(indexPath) cell?.accessoryType = UITableViewCellAccessoryType.Checkmark self.arrayIsMarked[indexPath.row] = true }) tableView.deselectRowAtIndexPath(indexPath, animated: true) let alert = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action : UIAlertAction) -> Void in let cell = tableView.cellForRowAtIndexPath(indexPath) cell?.accessoryType = UITableViewCellAccessoryType.None self.arrayIsMarked[indexPath.row] = false }) controller.addAction(alert) controller.addAction(okAction) self.presentViewController(controller, animated: true, completion: nil) } // 把一个cell左拉后会出现的按键。 func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: "Delete", handler: { (action: UITableViewRowAction, NSIndexPath) -> Void in self.array.removeAtIndex(indexPath.row) self.arrayIsMarked.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left) }) let ChangeAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Change") { (UITableViewRowAction, NSIndexPath) -> Void in let Controller = UIAlertController(title: "Do you wanna change?", message: nil, preferredStyle: UIAlertControllerStyle.Alert) let YesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { action in let controller = UIAlertController(title: "Thank you for your changing", message: nil, preferredStyle: UIAlertControllerStyle.Alert) let cancelAction = UIAlertAction(title: "Bye", style: UIAlertActionStyle.Cancel, handler: nil) controller.addAction(cancelAction) self.presentViewController(controller, animated: true, completion: nil) }) let NoAction = UIAlertAction(title: "No", style: .Cancel, handler: nil) Controller.addAction(YesAction) Controller.addAction(NoAction) self.presentViewController(Controller, animated: true, completion: nil) } return [deleteAction, ChangeAction] } // 返回每个cell的高度。 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 80 }}
tableView还要注意设定delegate & datasource的委托。
0 0
- [IOS]tableView的操作。
- ios TableView 一般操作
- ios TableView那些事3(Swift 三) TableView 的删除操作
- OC的tableView操作
- ios tableView 的使用
- IOS:tableview的重用
- iOS---tableView的代理
- ios:TableView的用法
- IOS的tableView的知识
- ios的tableview的cell
- ios自制tableview的cell
- ios tableview的几个坑
- IOS 整理tableview的代码
- iOS中TableView的自定义
- IOS tableView的重用机制
- ios自制tableview的cell
- iOS TableView的性能优化
- ios关于tableView的重用
- mFC + ffmpeg
- CF 620 C. Pearls in a Row 贪心
- n x n矩阵的转置矩阵,并计算对角线元素之和
- 4.6日学习记录
- SDAU 搜索专题 15 Knight Moves
- [IOS]tableView的操作。
- 项目:信息交流平台系统
- Android后台定时提醒功能实现
- angularjs 前端页面之用户修改密码
- JAVA语言规范(官方文档)一
- LeetCode 110 Balanced Binary Tree
- Nginx
- no such file or directory
- 如何筛选素数