ios - 如何将项目追加到表格或 Collection View 的底部?

标签 ios swift swift2

我有一个快速的 TableView / Collection View 。无论如何,我可以将项目附加到容器的末尾吗?比如颠倒的堆栈之类的?

最佳答案

您需要在 UITableView 和 Collection View 中插入行,因为首先需要在您的数据源中插入行。 对于 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];

对于 Collection View

    [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];

关于ios - 如何将项目追加到表格或 Collection View 的底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863439/

相关文章:

java - Appium 桌面 1.6.1> IOS> java : running desired capabilities gives bad

ios - Swift 如何使用动画淡入新图像然后淡入旧图像

swift - Xcode - 桥接 header 的 Swift 编译器错误

swift - 使用 ObjectMapper 映射 JSON 对象的通用函数

ios - 插入 View Controller 动画时工具栏按钮闪烁(快速淡出)

ios - 我无法让 ScrollView 正确滚动 'round rect buttons' 或标签等对象

ios - 在 Swift 中更改导航栏的颜色

swift - Any 在 Swift 中的转换失败?协议(protocol)

xcode - 升级到 Xcode 7 并得到这个 : Library not loaded: @rpath/libswiftCore. dylib

ios - 左镜像照片的缩略图方向错误