如何处理UITableView中的触摸事件

时间:2014-01-18 22:49:06

标签: c# ios xamarin.ios xamarin

我正在尝试将事件应用于单元格以指定TouchesBegan方法。

cell.TouchesBegan += OnTouchesBegan;

但是TouchesBegan是一个方法组,而不是一个事件。

如何在monotouch中向单元格TouchesBegan()方法添加自定义行为?

[Register("MenuView")]
public class MenuView : ViewControllerBase
{
    private new MenuViewModel ViewModel { get { return (MenuViewModel)base.ViewModel; } }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        // this ensures the sliding panel doesn't fill the entire view.
        var frame = View.Frame;
        frame.Width = 300;
        View.Frame = frame;

        var chapters = ViewModel.Chapters;

        //var listHeight = (chapters.Count*40);
        var navigationList = new UITableView(new RectangleF(0, 50, 300, 300))
            {
                Source = new NavigationTableSource(chapters)
            };
        Add(navigationList);
    }
}

public class NavigationTableSource : UITableViewSource
{
    private readonly IList<ChapterViewModel> _chapters;
    private const string CellIdentifier = "NavigationCell";

    public NavigationTableSource(IList<ChapterViewModel> chapters)
    {
        _chapters = chapters;
    }

    public override int RowsInSection(UITableView tableview, int section)
    {
        return _chapters.Count;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell(CellIdentifier) ??
                   new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier);
        // if there are no cells to reuse, create a new one
        cell.TextLabel.Text = _chapters[indexPath.Row].Title;
        cell.TouchesBegan += OnTouchesBegan;
        return cell;
    }
}

2 个答案:

答案 0 :(得分:0)

TouchesBegan是一个可以通过继承UITableViewCell来覆盖的方法:

public class CustomCell : UITableViewCell
{
    public CustomCell(UITableViewCellStyle style, NSString reuseIdentifier) : base(style, reuseIdentifier)
    {
    }

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        // do something

        base.TouchesBegan(touches, evt);
    }
}

然后在创建单元格时使用您的子类:

var cell = tableView.DequeueReusableCell(CellIdentifier) ??
               new CustomCell(UITableViewCellStyle.Default, CellIdentifier);

答案 1 :(得分:0)

所以我能够管理这个的方法是SubClass我的UITableViewSource。在那里我覆盖了RowSelected(UITableView tableView, NSIndexPath indexPath)

public class NavigationTableSource : UITableViewSource
{
    private readonly IList<ChapterViewModel> _chapters;
    private const string CellIdentifier = "NavigationCell";

    public NavigationTableSource(IList<ChapterViewModel> chapters)
    {
        _chapters = chapters;
    }

    public override int RowsInSection(UITableView tableview, int section)
    {
        return _chapters.Count;
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        // todo: This is where we're going to load up the appropriate chapter.
        new UIAlertView("Row Selected", _chapters[indexPath.Row].Title, null, "OK", null).Show();
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell(CellIdentifier) ??
                   new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier);
        // if there are no cells to reuse, create a new one
        cell.TextLabel.Text = _chapters[indexPath.Row].Title;
        return cell;
    }
}