How to dynamically add rows to a specific UITableView section?

后端 未结 5 1643
Happy的楠姐
Happy的楠姐 2020-12-06 13:23

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 actio

相关标签:
5条回答
  • 2020-12-06 13:55

    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.

    0 讨论(0)
  • 2020-12-06 14:04

    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()
    
    0 讨论(0)
  • 2020-12-06 14:15

    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.

    0 讨论(0)
  • 2020-12-06 14:16

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

    USE THIS METHOD..

    0 讨论(0)
  • 2020-12-06 14:17

    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];
    
    0 讨论(0)
提交回复
热议问题