简体   繁体   中英

TableView expanding and collapsing section with animation on touch

I have a list already populated with data, on touch i want the section to expand and delete the rows from the previous expanded section. Also in section 0 i have a Promotion View that doesn t use touch recognition, this is the cause for the _restaurantModelDetails.Menu.Regular[(int)section - _isPromoView].Products.Count isPromoView has a value of 1 I tried different methods, the one i expose here has the following problems on EndUpdates() at the second click, on the first one nothing happens;

NSInternalInconsistencyException Reason: Invalid update: invalid number of rows in section 6. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

This is the touch handler

void SectionHeaderViewOpened( UITableView tableView, int section)
{
    List indexPathsToInsert = new List( );
    List indexPathsToDelete = new List();

    for (int i = 0; i < tableView.NumberOfRowsInSection(section); i++)
    {
        indexPathsToInsert.Add(NSIndexPath.FromRowSection(i, section));
    }

    if(_sectionOpen != -1)
        for (int i = 0; i < tableView.NumberOfRowsInSection(_sectionOpen); i++)
        {
            indexPathsToDelete.Add(NSIndexPath.FromRowSection(i, _sectionOpen));
        }

    UITableViewRowAnimation insertAnimation;
    UITableViewRowAnimation deleteAnimation;

    if ( _sectionOpen == -1 || section < _sectionOpen )
    {
        insertAnimation = UITableViewRowAnimation.Top;
        deleteAnimation = UITableViewRowAnimation.Bottom;
    }
    else
    {
        insertAnimation = UITableViewRowAnimation.Bottom;
        deleteAnimation = UITableViewRowAnimation.Top;
    }

    //tableView.reloadData();

    tableView.BeginUpdates();
    tableView.DeleteRows(indexPathsToDelete.ToArray(), deleteAnimation);
    tableView.InsertRows(indexPathsToInsert.ToArray(), insertAnimation);
    tableView.EndUpdates(  );

    _sectionOpen = section;
}

public override UITableViewCell GetCell( UITableView tableView, NSIndexPath indexPath )
{
    RestaurantRow cell;

    var product = _restaurantModelDetails.Menu.Regular[indexPath.Section - _isPromoView].Products[indexPath.Row];
    cell = (RestaurantRow)tableView.DequeueReusableCell(CellIdentifier) ?? new RestaurantRow(product.Name);

    return cell;
}

public override nint RowsInSection( UITableView tableView, nint section )
{
    //Resturn number of products
    if ( section == _sectionOpen )
    {
        return _restaurantModelDetails.Menu.Regular[(int)section - _isPromoView].Products.Count;
    }

    return 0;
}

I found the problem that i was having.

I was getting my number of rows from

tableView.NumberOfRowsInSection(section)

which came as 0

The fix was to get the rows from

_restaurantModelDetails.Menu.Regular[section - _isPromoView].Products.Count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM