Add rows to existing UITableView section

前端 未结 2 893
梦如初夏
梦如初夏 2021-01-12 09:03

I am trying to get some sample code on how I would add rows to an existing UITableView. I am trying to use the insertRowsAtIndexPaths: function.

2条回答
  •  无人共我
    2021-01-12 09:54

    You need to handle these functions if you are gonna insert row and section in a table view.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return noOfSections;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [[noOfRows objectForIndex:section] intValue];
    }
    

    also if you are updating your table view it is recommended that you use beginUpdates and endUpdates method.

    this is how you insert new row and section.

    noOfSections++;
    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationTop];
    
    
    // update your array before calling insertRowsAtIndexPaths: method
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:2]] 
                          withRowAnimation:UITableViewRowAnimationTop];
    

    check the sample code provided by the apple.

    http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html

提交回复
热议问题