ios 如何将行动态添加到特定的 UITableView 部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23821088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:11:28  来源:igfitidea点击:

How to dynamically add rows to a specific UITableView section?

iosobjective-cuitableview

提问by vmfesta

I am a new IOS Programmer, and i am having a issue.

我是一个新的 IOS 程序员,我遇到了一个问题。

I have a UITableView with 2 sections, one then is static, and another one is dynamical.

我有一个包含 2 个部分的 UITableView,一个是静态的,另一个是动态的。

In specific actions i need to add new rows for the second section in runtime..

在特定操作中,我需要在运行时为第二部分添加新行。

I know how to manage a UITableView , but not a specific section

我知道如何管理 UITableView ,但不知道如何管理特定部分

Could you help me please?

请问你能帮帮我吗?

Best Regards you all

向大家致以最诚挚的问候

回答by Akhilrajtr

You can use insertRowsAtIndexPaths:method of UITableView

你可以使用insertRowsAtIndexPaths:方法UITableView

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

NSInteger row = //specify a row where you need to add new row
NSInteger section = //specify the section where the new row to be added, 
//section = 1 here since you need to add row at second section

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

回答by Anil Varghese

You can use the same method insertRowAtIndexPathlike

你可以用同样的方法insertRowAtIndexPath一样

 // Add the items into your datasource object first. Other wise you will end up with error
 // Manage the number of items in section. Then do

 NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:1];
 NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:1 inSection:1];
[self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];

This will insert two rows to the section1. Remember before doing this you have manage your datasource object.

这将在 section1 中插入两行。请记住,在执行此操作之前,您已经管理了数据源对象。

回答by Anand

Swift 3version

斯威夫特 3版本

// Adding new item to your data source
dataSource.append(item)
// Appending new item to table view
yourTableView.beginUpdates()
// Creating indexpath for the new item
let indexPath = IndexPath(row: dataSource.count - 1, section: yourSection)
// Inserting new row, automatic will let iOS to use appropriate animation 
yourTableView.insertRows(at: [indexPath], with: .automatic)
yourTableView.endUpdates()

回答by Arun Yadav

you can do this by using scrolltoinfinite method but before that u have to import 3rd party svpulltorefresh

你可以通过使用 scrolltoinfinite 方法来做到这一点,但在此之前你必须导入 3rd party svpulltorefresh

 [self.tableViewMixedFeed addInfiniteScrollingWithActionHandler:^{

        CGFloat height = self.tableViewMixedFeed.frame.size.height;

        CGFloat contentYoffset = self.tableViewMixedFeed.contentOffset.y;

        CGFloat distanceFromBottom = self.tableViewMixedFeed.contentSize.height - contentYoffset;
if(distanceFromBottom<contentYoffSet)
{
 [weak insertRowAtBottom];
}
 }];

-(void)insertRowAtBottom
{
 [self.tableViewMixedFeed beginUpdates];
    [self.tableViewMixedFeed insertRowsAtIndexPaths:indexPaths1 withRowAnimation:UITableViewRowAnimationTop];

     [self.tableViewMixedFeed endUpdates];
    [self.tableViewMixedFeed.infiniteScrollingView stopAnimating];
}

here indexpaths1 is the array of indexpaths of next cells which u want to insert in a table . try using loop to get the next set of arrays and store them into indexpaths1.

这里 indexpaths1 是您要插入表中的下一个单元格的索引路径数组。尝试使用循环获取下一组数组并将它们存储到 indexpaths1 中。

回答by asdafd

[self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];

[self.tableView insertRowsAtIndexPaths:@[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];

USE THIS METHOD..

使用这个方法..