Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

ios - Animate the insertion of a new section in UITableVIew

I have this button on my view, and when I press it I insert a new section in my table view ( I have a logical condition in my

-(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
    if (slide==TRUE) return 2;
    return 1;
}

And also in my -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. My section is added as it should, but I have read somewhere that this can be animated, because when I press my button the section is added but with no animation. I think I should use this -(void)insertSections:(NSIndexSet*)sections withRowanimation(UITableViewRowAnimation) animation but I haven't found a proper example on the web.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Manipulating rows and sections into UITableView using animation is made pretty easy using the following API's (emphasis on the last two):

Updating the Data Model

You should update your data model anywhere between the beginUpdates and endUpdates methods. It doesn't matter if you update your data model before or after insertion or deletion methods just so long as you do it between the beginUpdates and endUpdates methods.

Do not Insert Rows for New Sections You are Inserting

When adding a new section using the insertSections:withRowAnimation: method, you do not need to call insertRowsAtIndexPaths:withRowAnimation: to add rows into it. The insertRowsAtIndexPaths:withRowAnimation: method is just for animating an existing section changing. Similarly, you do not need to call deleteRowsAtIndexPaths:withRowAnimation: when you remove a section using deleteSections:withRowAnimation:.

Regarding Deleting and Inserting:

I generally do these in separate beginUpdates: endUpdates calls, however you can insert and delete at the same time. Just be aware that the UITableView will first try to delete rows / sections and then try to insert them regardless of the order you do it in your code.

Discussion part of deleteRowsAtIndexPaths:withRowAnimation:

Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods. UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls. This is unlike inserting or removing an item in a mutable array, where the operation can affect the array index used for the successive insertion or removal operation. For more on this subject, see “Batch Insertion and Deletion of Rows and Sections” in Table View Programming Guide for iOS.

Example of inserting into a section:

int indexOfNewSection = 4; // TODO: change to meaningful value
[self.tableview beginUpdates];
[self.tableview insertSections:[NSIndexSet indexSetWithIndex:indexOfNewSection]
              withRowAnimation:UITableViewRowAnimationAutomatic];

// Update data model
[self.sections insertObject:sectionObj atIndex:indexOfNewSection];

[self.tableview endUpdates];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...