简体   繁体   English

TableView展开和折叠部分带有触摸动画

[英]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; 同样在第0部分中,我有一个不使用触摸识别的升级视图,这是_restaurantModelDetails.Menu.Regular[(int)section - _isPromoView].Products.Count isPromoView的值,我尝试了不同的方法,我在此处公开的内容在第二次单击时在EndUpdates()上存在以下问题,在第一次单击时没有任何反应;

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). NSInternalInconsistencyException原因:无效的更新:第6节中的行数无效。更新(6)之后,现有节中包含的行数必须等于更新(0)之前该节中包含的行数,再加上或减去从该部分插入或删除的行数(插入0,删除0),再加上或减去移入或移出该部分的行数(移入0,移出0)。

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 这是0

The fix was to get the rows from 解决方法是从中获取行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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