在iOS 8 Beta 2+中,我使用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?
Monotach
public override UITableViewRowAction[] EditActionsForRow (UITableView tableView, NSIndexPath indexPath)
{
UITableViewRowAction moreButton = UITableViewRowAction.Create (
UITableViewRowActionStyle.Default,
"More",
delegate {
Console.WriteLine ("Hello World!");
});
return new UITableViewRowAction[] { moreButton };
}
成功代码如下所示:
Swift
var button = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Test", handler:{ action, indexpath in println("Hello World!"); });
目标-c
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Test" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog("Hello World!");
}];