Forum Xamarin.iOS

How do I add new items to a UITableView?

TrevorYokumTrevorYokum USMember ✭✭

This question has been asked before, and I'm very close to the answer, I just have no idea how to implement it correctly. I would really appreciate some help.

My application has a button that, when pressed, should add a new row to the end of the list (really a UITableView) and display it immediately after pressing the button. Eventually, it will pull down the user's location and add the lat/lon as a string to the UITableView. Here is my UITableViewSource code:

class LocationTableSource : UITableViewSource
    public List<string> _list;
    public LocationTableSource(List<string> list)
        _list = list;
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        const string CellIdentifier = @"Cell";
        UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
        string item = _list[indexPath.Row];

        if( cell == null )
            cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier);
        cell.TextLabel.Text = item;
        return cell;

    public override nint RowsInSection(UITableView tableview, nint section)
        return _list.Count;

In this discussion, @adamkemp almost completes my understanding, but I'm missing two things:

  1. Where do I implement the addition of new rows? Inside of my UITableViewSource subclass? Inside of my button.TouchUpInside event? I can access my LocationTableSource._list property inside of my View Controller, which is why I ask
  2. How do I work with the NSIndexPath[] atIndexPaths parameter of UITableView.InsertRows(NSIndexPath[] atIndexPaths, UITableViewRowAnimation withRowAnimation)? I would presume that I would need to somehow create/get the NSIndexPath of the UITableView at _list.Count, which would be where the new row would be added.

Again, any help would be greatly appreciated -- I really do not understand the NSIndexPath[] parameter.


Best Answer


  • TrevorYokumTrevorYokum USMember ✭✭

    @adamkemp said:
    1. Wherever you want. The only thing that matters is that the data source is updated before telling the table view about the new rows.
    2. The index paths that you give it will be used by the table view to ask your data source for the new cells. Assuming you only have one section and you're adding a row at the end then the index path for the new row will have a row of _list.Count before you do anything, but it should probably be _list.Count - 1 by the time you call InsertRows. That's because when you call InsertRows the table view is going to turn around and ask your data source "how many rows do you have?" and "give me the row at this (new) index path", and your data source better have the right answers for those questions. Again, update the data source first and then call InsertRows.

    Wonderful -- perfectly explained. I had no idea that NSIndexPath contained a FromIndex(nuint index) method. I do want to say, though, that I tried _list.Add("test item"); tableView.ReloadData(); and it worked perfectly for my intended use. What implications, if any, exist for using this methodology? If none - great! If not, I'll totally switch to using the InsertRows(...) method of UITableView.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    There are two advantages to using InsertRows (or similar methods) over ReloadData:

    1. Performance. ReloadData will always reload everything, which often means doing unnecessary work.
    2. Better UI. When you use InsertRows you can make it animate the insertion, which makes it clear to the user what changed.
Sign In or Register to comment.