在 Cell 上我們可客製化 Cell,讓它除了 Delete 這功能外,還有其他功能可選。如何做? 我們要把新的功能 Action 加入 UITableViewRowAction 中。這函數 tableView(_:editActionsForRowAtIndexPath:) (New in tvos 9.0) 被定義在 UITableViewDelegate 這協定中: 也就是說我們要實作此協定:override func tableView(_ tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { }
但,在此協定中我們又該如何把新的 Action 加入? 我們依文件說明,來加入 Action:override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: {(action:UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in self.vocabulary.removeAtIndex(indexPath.row) self.tableView.reloadData() } ) let alertAction = UITableViewRowAction(style : UITableViewRowActionStyle.Default, title : "Share", handler : {(action:UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in let alertController = UIAlertController(title:"My First App", message:"Hello World", preferredStyle: .Alert) let alertAction = UIAlertAction(title:"OK", style: UIAlertActionStyle.Default, handler:nil) alertController.addAction(alertAction) self.presentViewController(alertController, animated: true, completion: nil) } ) return [deleteAction, alertAction] }
呼! 寫到此,驗證結果: 1. 選取 Cell,然後向左滑,顯示 Share 與 Delete Button 2. 按下 Share 按鈕 3. 在 Cell 顯示 8 的行,按下 Delete 按鈕 程式碼:// // MyTableViewController.swift // SimpleTableViewControllerDemo // // Created by Elvis Meng on 2015/9/6. // Copyright (c) 2015年 Elvis Meng. All rights reserved. // import UIKit class MyTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate { var vocabulary = ["book","pen","queen","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"] override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Potentially incomplete method implementation. // Return the number of sections. return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the number of rows in the section. return self.vocabulary.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "Cell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell // Configure the cell... cell.textLabel?.text = vocabulary[indexPath.row] return cell } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! if cell.accessoryType == UITableViewCellAccessoryType.None { cell.accessoryType = UITableViewCellAccessoryType.Checkmark } else { cell.accessoryType = UITableViewCellAccessoryType.None } tableView.deselectRowAtIndexPath(indexPath, animated: true) } override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! if cell.accessoryType == UITableViewCellAccessoryType.None { cell.accessoryType = UITableViewCellAccessoryType.Checkmark } else { cell.accessoryType = UITableViewCellAccessoryType.None } tableView.deselectRowAtIndexPath(indexPath, animated: false) } override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { self.vocabulary.removeAtIndex(indexPath.row) //tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) self.tableView.reloadData() } else if editingStyle == .Insert { self.vocabulary.insert("new", atIndex: indexPath.row) self.tableView.reloadData() } } override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: {(action:UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in self.vocabulary.removeAtIndex(indexPath.row) self.tableView.reloadData() } ) let alertAction = UITableViewRowAction(style : UITableViewRowActionStyle.Default,title : "Share", handler : {(action:UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in let alertController = UIAlertController(title:"My First App", message:"Hello World", preferredStyle: .Alert) let alertAction = UIAlertAction(title:"OK", style: UIAlertActionStyle.Default, handler:nil) alertController.addAction(alertAction) self.presentViewController(alertController, animated: true, completion: nil) } ) return [deleteAction, alertAction] } /* // Override to support editing the table view. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { // Delete the row from the data source tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { } */ /* // Override to support conditional rearranging of the table view. override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { // Return NO if you do not want the item to be re-orderable. return true } */ /* // 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. } */ }
參考: 1.UITableViewRowAction, https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UITableViewRowAction_class/
prettyprint
2015年9月10日 星期四
第 6 個程式:Cell 上多重功能的製作: UITableViewRowAction
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言