method 1 - commitEditingStyle
這個method一定要複寫才會有功能出現
method 2 - canEditRowAtIndexPath
決定是否要出現左滑功能(要先有method 1)
method 3 - editActionsForRowAtIndexPath
若有自定義的功能,就得複寫這個method,但得連系統預設的功能也要一併複寫
也就是說複寫後連Delete也不見了,要一併再實作出來!
Objective-C
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==self.categoryTableView){
return YES;
}else{
return NO;
}
}
- (NSArray *)tableView:(UITableView *)tableView
editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *moreAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"More" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// show UIActionSheet
}];
moreAction.backgroundColor = [UIColor greenColor];
UITableViewRowAction *flagAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Flag" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// flag the row
}];
flagAction.backgroundColor = [UIColor yellowColor];
return @[moreAction, flagAction];
}
參考︰
http://pablin.org/2014/09/25/uitableviewrowaction-introduction/
自定義的ACTION參考︰
https://gist.github.com/scheinem/e36835db07486e9f7e64
Swift︰
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//把剛剛寫的都刪光光
println("commitEditingStyle")
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var actions = Array<UITableViewRowAction>()
let gatewayJson = gatewayList[indexPath.row]
let isAllowed = gatewayJson["isAllowed"].stringValue
let gatewaySN = gatewayJson["gatewaySerialNo"].stringValue
let newMobileName = defaults.objectForKey("mobileName") as? String
let gatewayUUID = gatewayJson["gatewayUUID"].stringValue
if(isAllowed != "Y"){
var authAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "授權") { (action, indexPath) -> Void in
println("授權")
self.hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
self.hud!.mode = MBProgressHUDMode.Indeterminate
self.hud!.dimBackground = true;
self.hud!.labelText = "申請授權!"
self.model.requestAuthorization(gatewaySN, mobileName:newMobileName!, completionHandler: { (request, response, responseData, error) -> Void in
MBProgressHUD.hideHUDForView(self.view, animated: true)
if(error == nil){
let json = JSON(data: responseData!.dataUsingEncoding(NSUTF8StringEncoding)!)
if let resultCode:String = json["result"]["code"].string {
let message:String = json["result"]["message"].string!
if(resultCode=="200"){
//通知
var alert = UIAlertController(title: "Message", message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
//重新整理
self.tableView.reloadData()
}))
self.presentViewController(alert, animated: true, completion: nil)
}else{
var alert = UIAlertController(title: "Message", message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}else{
println("Error: Respose is nil ! responseData: \(responseData)")
//錯誤通知,不導頁
var alert = UIAlertController(title: "Message", message: "\(responseData)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}else{
println(error)
//錯誤通知,不導頁
var alert = UIAlertController(title: "Message", message: "\(error)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
}
authAction.backgroundColor = UIColor(red: 255/255, green: 166/255, blue: 51/255, alpha: 1)
actions.append(authAction)
}
//delete按鈕自己做
var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "刪除", handler: {
(action:UITableViewRowAction! , indexPath:NSIndexPath!) -> Void in
println("刪除")
self.hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
self.hud!.mode = MBProgressHUDMode.Indeterminate
self.hud!.dimBackground = true;
self.hud!.labelText = "刪除授權!"
self.model.removeAuthorization(gatewayUUID, completionHandler: { (request, response, responseData, error) -> Void in
MBProgressHUD.hideHUDForView(self.view, animated: true)
if(error == nil){
let json = JSON(data: responseData!.dataUsingEncoding(NSUTF8StringEncoding)!)
if let resultCode:String = json["result"]["code"].string {
let message:String = json["result"]["message"].string!
if(resultCode=="200"){
//通知
var alert = UIAlertController(title: "Message", message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
//重新整理
self.tableView.reloadData()
}))
self.presentViewController(alert, animated: true, completion: nil)
}else{
var alert = UIAlertController(title: "Message", message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}else{
println("Error: Respose is nil ! responseData: \(responseData)")
var alert = UIAlertController(title: "Message", message: "\(responseData)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}else{
println(error)
//錯誤通知,不導頁
var alert = UIAlertController(title: "Message", message: "\(error)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
})
actions.append(deleteAction)
return actions
}
留言
張貼留言