首页 > 解决方案 > 通过 UIImageView 选择表格单元格

问题描述

我有一个视图,其中包含用户收到的一组消息,我正在尝试完成类似于 gmail 工作方式的用户体验。

我希望当用户点击用户的图像时,它会选择他们想要执行的任何操作(删除、标记为已读等)的行,然后如果他们点击其他任何地方,它就会显示消息。下面是一个基本的概念证明。

基本概念

我考虑过在图像视图上设置一个轻击手势,但我觉得有比这样做更干净的方法,并且可能是表格视图中内置的东西,但我正在罢工。

标签: uitableviewxamarinxamarin.ios

解决方案


您要做的是根据位置具有单独的点击功能。您必须在单元格的每个部分添加单独的点击操作。

因此,在 TableView 的源代码中,您必须更新覆盖UITableViewCell GetCell,以便它更新图像按钮并添加允许用户交互和添加单击手势的功能。

public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
    var cell = tableView.DequeueReusableCell (cellIdentifier);
    // where cellIdentifier is you TableView Cell key

    if (cell == null)
        cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
    cell.TextLabel.Text = tableItems [indexPath.Row];

    // Add other generic code above, but here's what you need
    cell.ImageButton.UserInteractionEnabled = true;
    var singleTapGestureRecognizer = new UITapGestureRecognizer(() =>  // Do work
                                                                );
    cell.ImageButton.AddGestureRecognizer(singleTapGestureRecognizer);

    return cell;
}

推荐阅读