How to dynamically add rows to a specific UITableView section?

旧时模样 提交于 2020-01-09 07:46:49

问题


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

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

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

Could you help me please?

Best Regards you all


回答1:


You can use insertRowsAtIndexPaths: method of 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];



回答2:


You can use the same method insertRowAtIndexPath like

 // 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.




回答3:


Swift 3 version

// 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()



回答4:


you can do this by using scrolltoinfinite method but before that u have to import 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.




回答5:


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

USE THIS METHOD..



来源:https://stackoverflow.com/questions/23821088/how-to-dynamically-add-rows-to-a-specific-uitableview-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!