Xamarin.ios CollectionView删除和插入项目

时间:2016-10-24 06:52:48

标签: visual-studio xamarin.ios uicollectionview

我目前正在使用Visual Studio中的xamarin开发iphone应用程序。我是初学者..

我使用CollectionView。它有一个列表,包含8个在CollectionViewSource.cs中具有不同ID的项目:

ProductCategoryList.Add (new ProductCategorys () {Categoryid = 1, ProductCategoryName = "testname", ProductCategoryImage = UIImage.FromFile ( "productcat / maincat / testpic.jpg")});

现在我想,如果按下某个项目,请删除所有项目并重新填充。 在ProductCategoryCollectionDelegate.cs中我有:

Public override Boolean ShouldSelectItem (UICollectionView collectionView, NSIndexPath indexPath)

对于删除和插入,我找到了这两种方法

Public void DeleteItems (NSIndexPath [] indexPaths)
Public void InsertItems (NSIndexPath [] indexPaths)

(xamarin文档非常缺乏)

如何使用这2个元码完全清空列表并添加新项?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我已采用TableView代替CollectionView。 使用此功能,可以直接删除Tablesource类中的项目:

public override void CommitEditingStyle(UITableView tableView,
                          UITableViewCellEditingStyle editingStyle,
                          Foundation.NSIndexPath indexPath)
{
        switch (editingStyle)
        {
            case UITableViewCellEditingStyle.Delete:
                AppDelegate.DAUtil.setBasketItemStatusByBasketID(
                   basketitems[indexPath.Row].ZBASKETID, "2");
                // remove the item from the underlying data source
                basketitems.RemoveAt(indexPath.Row);
                // delete the row from the table
                tableView.DeleteRows(new NSIndexPath[] { indexPath },
                   UITableViewRowAnimation.Fade);
                break;
            case UITableViewCellEditingStyle.None:
                Console.WriteLine("CommitEditingStyle:None called");
                break;
        }
}

public override bool CanEditRow(UITableView tableView,
                            NSIndexPath indexPath)
{
        return true;
     // return false if you wish to disable editing
     // for a specific indexPath or for all rows
}

表的来源是一个列表。或者,您可以使用以下内容重新加载TableSource

tablename.ReloadData();