I am trying to do something when a user touches a row in a UITableView. I am coding for iOS and using Xamarin.
I am assuming that when the user touches a row in the UITableView, and that row is highlighted, that it would mean that the user has just selected a row. So, I figured it made sense to override the RowSelected method in my UITableViewSource. For some reason, though, I can't seem to get that method to ever be called.
Here is my complete UITableViewSource implementation:
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();
}
}
Here is where I create my table view and assign it to my 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;
Here is the method that is supposed to handle when the user selects an item in the list:
void DeviceViewController_DeviceSelected(object sender, ClientCommunication.AutoDiscovery.FoundDevice dev)
{
UpnpController.GetInfo(dev);
}
The above method is NOT being called when I touch an item in my UITableView. What am I missing here? Am I misunderstanding the concept of 'Selected' in iOS? I am totally new to iOS development and am using Xamarin so I can develop iOS apps using C#.
Thanks in advance for any pointers. I'm sure that I'm just missing some really simple detail, but I can't figure out what it is..
THANK YOU JASON !!!
You nailed it. I was calling the base class and I shouldn't:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
base.RowSelected(tableView, indexPath);
if (DeviceSelected != null)
{
DeviceSelected(tableView, Devices[indexPath.Row].FoundDevice);
}
}
Just needed to remove call to base.RowSelected(..)
:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
if (DeviceSelected != null)
{
DeviceSelected(tableView, Devices[indexPath.Row].FoundDevice);
}
}
Now it works just fine.
Thanks JASON !!!