Ali
Ali

Reputation: 2768

How to handle UITableViewCell IDs

I have a model which I am passing to my UITableViewCell from my UITableView class as following

this.Source = new MyTableSource(ListOfItems);

My ITEM Model have only two values

public string Id { get; set; }
public string Title { get; set; }

Where my UITableViewSource is the Following

public class AllSource : UITableViewSource
{
    List<ITEM> ITEMS = new List<ITEM>();        
    public AllSource(List<ITEM> _items)
    {
        ITEMS = _items;           
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell =  tableView.DequeueReusableCell("AllCell") as AllCell;

        if (cell == null)
        {
            cell = new AllCell();
            var views = NSBundle.MainBundle.LoadNib(AllCell.Key, cell, null);
            cell = Runtime.GetNSObject(views.ValueAt(0)) as AllCell;
        }
        var LINK = ITEMS[indexPath.Row];           
        cell.BindCell(LINK);            
        return cell;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return HomeDisplay.Count;
    }
}

and my cell.BindCell(LINK) is the following

public void BindCell(ITEM link)
{
 TitleText.Text = Link.Title;
}

I am also using 'RowSelected' as following

 public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{                              
 tableView.DeselectRow(indexPath, true);
}

This works OK but

I would like to attach a unique ID which is in string format to the Cell so when my RowSelected is triggered I get that string ID to uniquly identify the ID of selected item (Not the indexPath)?

Initially, I thought just put a HIDDEN LABEL in but that does not seem to be a right choice.

Is this possible?

Upvotes: 0

Views: 30

Answers (1)

MilanG
MilanG

Reputation: 2604

If I consider public string Id { get; set; } from your Model to be unique Id, then you can get this like following:

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

    //Here you can get your Id of selected row
    string selectedId = ITEMS[indexPath.Row].Id;
}

Upvotes: 1

Related Questions