1

I have a table view containing a variable amount of sections and custom cells. On some occasions, a cell may resize within RowSelected(). Whenever that happens, I'd like to also make sure the cell is completely visible after resizing (enlarging) it.

I have that working in another table that just modifies the underlying data so that the table view source will provide a larger cell. I then reload the cell and scroll it visible like so:

// Modify data
//...

// Reload cell
tableView.ReloadRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.None);
tableView.ScrollRectToVisible(tableView.CellAt(indexPath).Frame, true);

The problem arises in a table view where resizing may not only be triggered by RowSelected(), but also by events on UI elements within the cells.

The events then call a method to reload the cell:

void updateCell() {
  if (cell.Superview != null) {
    UITableView tableView = (UITableView)cell.Superview;
    tableView.ReloadRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.None);

    // Get the new (possibly enlarged) frame
    RectangleF frame = tableView.CellAt(indexPath).Frame;
    Console.WriteLine("This really is the new large frame, height: {0}", frame.Height);

    // Try to scroll it visible
    tableView.ScrollRectToVisible(frame, true);
  }
}

This scrolls fine for all cells but the bottom-most. It only makes the old frame of that cell visible. I double-checked that it really provides the new cell frame to ScrollRectToVisible().

So it seems ScrollRectToVisible() is bound to the old content size of the table - even after reloading rows. I tried to work around that by providing a new content size with the calculated difference in height. That does work but feels really hackish to me.

Is there some cleaner way to do things?

Thanks in advance

riha
  • 2,270
  • 1
  • 23
  • 40
  • possible dublicate http://stackoverflow.com/questions/594181/uitableview-and-keyboard-scrolling-problem – Soner Gönül Apr 13 '11 at 08:42
  • Can you please share your code ?I am looking to show only 3 cells whenever user scrolls the talbe, & if the scrolling ends in between, intstead of showing cutting cells , I would like to show complete cells, checking nearby cells index.Any suggestion for this ? – Ajay Sharma Nov 08 '11 at 04:51

1 Answers1

7

Instead using this:

tableView.ScrollRectToVisible(_: animated: )

Use it to scroll UITableView to bottom row:

tableView.scrollToRow(at: at: animated: )
AivanF.
  • 1,134
  • 2
  • 23
  • 52