如何将项目附加到表格或集合视图的底部?

时间:2015-09-30 10:10:12

标签: ios swift swift2

我有一个快速的表视图/集合视图。无论如何我可以将物品附加到容器的末端吗?就像一个颠倒的堆栈或什么?

1 个答案:

答案 0 :(得分:1)

您需要在UITableView和集合视图中插入行,以便首先插入数据源。 对于TableView

//Update data source with the object that you need to add
[tableDataSource addObject:newObject];

NSInteger row = [tableDataSource count]-1 ;//specify a row where you need to add new row in your case last
NSInteger section = //specify the section where the new row to be added, 
//section = 0 here since you need to add row at first section

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];

对于CollectionView

    [self.myCollectionView performBatchUpdates:^{
[collectionDataSource addObject:newObject];
NSInteger row = [collectionDataSource count]-1 ;//specify a row where you need to add new row in your case last
             to add new row in your case last
    NSInteger section = //specify the section where the new row to be added, 
    //section = 0 here since you need to add row at first section

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
            [self.myCollectionView insertItemsAtIndexPath: indexPath];
        } completion:nil];