ios - 使用 Xamarin 在 iOS 的 UITableView 中未调用 RowSelected

标签 ios uitableview selection xamarin mouseclick-event

当用户触摸 UITableView 中的一行时,我正在尝试做某事。我正在为 iOS 编码并使用 Xamarin。

我假设当用户触摸 UITableView 中的一行并且该行被突出显示时,这意味着用户刚刚选择了一行。所以,我认为在我的 UITableViewSource 中覆盖 RowSelected 方法是有意义的。但是,出于某种原因,我似乎无法调用该方法。

这是我完整的 UITableViewSource 实现:

public class DeviceSource : UITableViewSource
{
    public event EventHandler<FoundDevice> DeviceSelected;
    private List<FoundDeviceWithInfo> Devices { get; set; }

    public DeviceSource(List<FoundDeviceWithInfo> devices)
    {
        Devices = new List<FoundDeviceWithInfo>(devices);
    }

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

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        base.RowSelected(tableView, indexPath);

        if (DeviceSelected != null)
        {
            DeviceSelected(tableView, Devices[indexPath.Row].FoundDevice);
        }
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell((NSString)indexPath.Row.ToString());

        if (cell == null)
        {
            cell = new DeviceCell(Devices[indexPath.Row].FoundDevice.IpAddress + ":" + Devices[indexPath.Row].FoundDevice.PortNumber,
                              Devices[indexPath.Row].FoundDevice.DeviceName,
                              Devices[indexPath.Row].DeviceCommonInfo != null ? Devices[indexPath.Row].DeviceCommonInfo.Position.AutoGEOTag.Lat : string.Empty,
                              Devices[indexPath.Row].DeviceCommonInfo != null ? Devices[indexPath.Row].DeviceCommonInfo.Position.AutoGEOTag.Long : string.Empty,
                              (NSString)indexPath.Row.ToString()) {UserInteractionEnabled = true};
        }

        return cell;
    }

    public void AddDevice(FoundDeviceWithInfo device)
    {
        if (!Devices.Contains(device))
        {
            Devices.Add(device);
        }
    }

    public void RemoveDevice(FoundDeviceWithInfo device)
    {
        if (Devices.Contains(device))
        {
            Devices.Remove(device);
        }
    }

    public void ClearAllDevices()
    {
        Devices.Clear();
    }
}

这是我创建表格 View 并将其分配给我的 DeviceSource 的地方:
    _tableView = new UITableView
    {
        ScrollEnabled = true,
        Frame = new RectangleF(10, 74, View.Bounds.Width - 20, View.Bounds.Height - 84),
        AutoresizingMask = UIViewAutoresizing.All,
        Source = new DeviceSource(new List<FoundDeviceWithInfo>()),
        RowHeight = 45
    };

    ((DeviceSource)_tableView.Source).DeviceSelected += DeviceViewController_DeviceSelected;

这是用户选择列表中的项目时应该处理的方法:
void DeviceViewController_DeviceSelected(object sender, ClientCommunication.AutoDiscovery.FoundDevice dev)
{
    UpnpController.GetInfo(dev);
}

当我触摸 UITableView 中的项目时,不会调用上述方法。我在这里想念什么?我是否误解了 iOS 中“选定”的概念?我对 iOS 开发完全陌生,并且正在使用 Xamarin,因此我可以使用 C# 开发 iOS 应用程序。

在此先感谢您的任何指点。我确定我只是错过了一些非常简单的细节,但我无法弄清楚它是什么......

最佳答案

谢谢你杰森!

你搞定了。我正在调用基类,但我不应该:

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
    base.RowSelected(tableView, indexPath);

    if (DeviceSelected != null)
    {
        DeviceSelected(tableView, Devices[indexPath.Row].FoundDevice);
    }
}

只需删除对 base.RowSelected(..) 的调用即可:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
    if (DeviceSelected != null)
    {
        DeviceSelected(tableView, Devices[indexPath.Row].FoundDevice);
    }
}

现在它工作得很好。

谢谢杰森!

关于ios - 使用 Xamarin 在 iOS 的 UITableView 中未调用 RowSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086498/

相关文章:

javascript - 在没有 jQuery 的情况下选择所有具有特定 css 规则的节点

iphone - 从服务器保存和检索核心数据

ios - Xcode 6 : "Simulator in Use" error shows even though the simulator is NOT in use

ios - UIRefreshControl 在 UITableViewController 中的位置错误

Vim/MacVim : when I scroll with mouse, 文本光标也移动了!

r - 使用带有索引的矩阵从数据框中选择多个值

iOS CustomTableViewCell : elements not aligining correctly

ios - 将电话提供商与 Firebase 中的电子邮件提供商关联起来 (Swift)

objective-c - 如何在触摸时更改一个 UITableViewCell

iphone - cellLabel.text的内容似乎在随机单元格中覆盖了自身