Expanding table view cells disappearing

前端 未结 7 1437
逝去的感伤
逝去的感伤 2021-02-12 23:36

I have cells that expand by changing their height with a setExpanded: method call.

I then call reloadRowsAtIndexPaths: to refresh the cells.

The problem is the c

相关标签:
7条回答
  • 2021-02-13 00:11

    @Javy,

    Thanks for your question. I was developing table with similar behaviour: there are text views (UITextView) in my table view cells (UITableViewCell) that are :

    1. expanding if new line is required to display text without scrolling, or
    2. collapsing if new line was removed

    So I found the same problem. In iOS 5.x when I was starting to type text it suddenly was becoming invisible. But in iOS 4.x everything works fine.

    I have found the solution and it works well for me.

    Solution: Just try to replace your animation type UITableViewRowAnimationAutomatic with UITableViewRowAnimationNone when reloading the particular cell.

    Some additional code: reload both cell in one moment:

        NSMutableArray *indexes = [NSMutableArray arrayWithCapacity:2];
        // collapse previous cell
        if( previousCell != nil && [previousCell expandable] )
        {
            if( [previousCell expanded] ) [previousCell setExpanded:NO];
            [indexes addObject:previousIndexPath_];
        }
    
        // expand new cell
        if( [cell expandable] )
        {
            [cell setExpanded:YES];
            [indexes addObject:indexPath];
        }
    
        [tableView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone];
    

    Hope it will help you.

    0 讨论(0)
  • 2021-02-13 00:12

    Okay...

    before you

    previousIndexPath_ = indexPath;
    

    and after you do all the expanding / contracting of heights etc; you need to do something like

    [cellArray replaceObjectAtIndex:previousIndexPath_ withObject:previousCell];
    [cellArray replaceObjectAtIndex:indexPath withObject:cell];
    

    which means your cellArray needs to be

    NSMutableArray* cellArray;
    
    0 讨论(0)
  • 2021-02-13 00:16

    I had the same issue. I confirm that using no animation when reloading the row works fine.

    But it turns out the issue is caused by returning a cached UITableViewCell in cellForRowAtIndexPath when actually a fresh one is needed.

    The doc for reloadRowsAtIndexPaths:withRowAnimation: says: "Reloading a row causes the table view to ask its data source for a new cell for that row."

    Your cellForRowAtIndexPath method is returning locally cached cells. Returning a brand new cell fixed the issue in my case and no workarounds were needed...

    0 讨论(0)
  • 2021-02-13 00:22

    you could try reloading the table view when the animations have completed? at the moment if your animations are fired, and then you call [tableView reloadData] the animations won't be complete, so the frame etc may get out of sync

    you could change your set expanded method to take a flag as well as an animation completion block, which could contain the reload code

    [cell setExpanded:YES completion:^
        //reload code
    }];
    
    0 讨论(0)
  • 2021-02-13 00:25

    Seems to me like the cell isn't redrawn properly.

    Did you try something like:

    [cell.backgroundView setNeedsDisplay]
    
    0 讨论(0)
  • 2021-02-13 00:30

    I don't see any difference in behavior between iOS 5.0 and 4.3.2 in the simulator, or 5.0 on my phone—the cells disappear in the same way on each. (I'd test on 4.2.1 on my older iPod touch but Xcode 4 can't. Grr..) It looks like there's an easy workaround, though: If you do both reloadRowsAtIndexPaths:withRowAnimation: after expanding/collapsing your cells and then the reloadData call after that, it appears to preserve the animation.

    Here's a small demo project for this. It's hard to say what the actual problem was—it's just some odd side effect of how UIKit is doing the cell animation. If you subclass [UITableViewCell setAlpha:] you can see that the table view is setting the cell's alpha to 0 and leaving it there. Weird.

    Edit: Well that's weird. It wasn't working for a bit (was doing the old behavior, with cells disappearing), but now it's right again. Maybe I was running the wrong version. Anyway, let me know if this works for you.

    0 讨论(0)
提交回复
热议问题