2014-09-01 91 views
0

我在iOS 8 Beta 2+中使用UITableViewRowAction方法在我的表中的某一行上滑動時添加自定義按鈕。雖然我無法解決如何處理點擊事件,但它目前剛剛崩潰了我的應用程序。我曾嘗試以下:UITableViewRowAction處理單擊

public override UITableViewRowAction[] EditActionsForRow (UITableView tableView, NSIndexPath indexPath) 
    { 
     List<UITableViewRowAction> items = new List<UITableViewRowAction>(); 

     UITableViewRowAction button = new UITableViewRowAction(); 
     button.Title="Test"; 
     button.BackgroundColor = UIColor.LightGray; 
     button += delegate { 
      Console.WriteLine ("Hello World!"); 
     }; 

     items.Add(Button); 

     return items.ToArray(); 
    } 

但是我得到以下將編譯:

Operator '+=' cannot be applied to operands of type 'MonoTouch.UIKit.UITableViewRowAction' and 'anonymous method'. 

如何處理函數分配給點擊UITableViewRowAction?

回答

2

MonoTouch的

public override UITableViewRowAction[] EditActionsForRow (UITableView tableView, NSIndexPath indexPath) 
{ 
    UITableViewRowAction moreButton = UITableViewRowAction.Create (
     UITableViewRowActionStyle.Default, 
     "More", 
     delegate { 
      Console.WriteLine ("Hello World!"); 
     }); 
    return new UITableViewRowAction[] { moreButton }; 
} 
1

成功的代碼如下所示:

斯威夫特

var button = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Test", handler:{ action, indexpath in println("Hello World!"); }); 

Objective-C的

UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Test" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) 
{ 
    NSLog("Hello World!"); 
}];