开发者

Initally visible cells gets invisible after calling reloadSections:withRowAnimation: method

开发者 https://www.devze.com 2023-01-05 10:15 出处:网络
I want to display a table which has multiple sections and initally only 3 elements of sections are shown. When user taps the section footer, section footer gets lost (becomes nil) and all elements of

I want to display a table which has multiple sections and initally only 3 elements of sections are shown. When user taps the section footer, section footer gets lost (becomes nil) and all elements of that section will be shown to user.

For this reason, when the user taps the section footer, I call below code:

-(void)loadMoreRowsInSection:(NSInteger)section {
    [groupStates replaceObjectAtIndex:section withObject:[NSNumber numberWithBool:YES]];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:NO];
}

I have the following code to show section footer or not:

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    int count = [[(NSDictionary*)[filters objectAtIndex:section] valueForKeyPath:@"values.value"] count];
    if (count>3&&![[groupStates objectAtIndex:section] boolValue]) {
        LoadMoreFooter* loadMoreFooter = [[LoadMoreFooter alloc] initWithParent:self Section:section];
        return [loadMoreFooter view];
    }   
    else return nil;
}

When user taps the section footer and loadMoreRowsInSection function is called, this section is reloaded but the current rows of that section disappear. When I scroll up down, making the rows go out of the screen and in again, rows appear again.

If I call reloadData instead of reloadSections:withRowAnimation:, there is no problem but it does not seem to be a good idea开发者_开发百科 to reload all the table. Plus there is no animation in reloadTable.

Has anyone encountered this problem?


Is this a "static" cell in the sense that you're keeping a reference to it yourself? In that case, the problem is probably this:

When you do an animated reload, the table view will fade out existing cells while at the same time fading in the new cells. The problem is that when the "new" cell is the exact same cell as the old one, the same cell will both fade in and fade out at the same time! And in your case, the fade out animation takes precedence and the cell is hidden.

Here are some possible solutions:

  1. Instead of static cells, have a reusable "wrapper cell" subclass - a cell whose only purpose is to contain a wrapped view. Then, keep static references to views instead, and add them to a dequeued wrapper cell in cellForRowAtIndexPath:

  2. Avoid reloading index paths of static cells - instead do [UIView transitionWithView:self.myStaticCell duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil]


I had this problem also. I used UITableViewRowAnimationNone instead, which still animates the section for some reason (which reloadData does not do) but it also displays it correctly after the animation.


Adding a call to reloadData after the call to reloadSections:withRowAnimation: appears to fix the disappearing cell problem without affecting the animation.


I had a similar problem where the section headers would get stuck painted wherever they were. Although my issue persists even when scrolled on/off the screen:

Initally visible cells gets invisible after calling reloadSections:withRowAnimation: method

Only way I have found to fix it is to just use reloadData instead of reloadSections:withRowAnimation:.


My bug was that: I had a TableView with multiple sections. When I selected a section for expand (and show cells), other sections above that were hided.

I was treating header as cells.

My solution without reloadData():

inside override func viewDidLoad() method I was using this code:

tableTeams.registerNib(UINib(nibName: "header", bundle: nil ), 
forCellReuseIdentifier: "HeaderCell")

But I should have used this:

tableTeams.registerNib(UINib(nibName: "header", bundle: nil),
forHeaderFooterViewReuseIdentifier: "HeaderCell")

and inside func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?, I would use this code:

func tableView(tableView: UITableView, viewForHeaderInSection section:
Int) -> UIView? {
      let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("HeaderCell")
as! CustomHeaderUiView

I hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消