Why do my table view cells disappear when reloaded using reloadRowsAtIndexPaths?

后端 未结 12 1770
青春惊慌失措
青春惊慌失措 2021-02-02 12:20

I have a bare-bones sample project here:

http://dl.dropbox.com/u/7834263/ExpandingCells.zip

In this project, a UITableView has a custom UITableViewCell. In each

相关标签:
12条回答
  • 2021-02-02 12:26

    May be I am missing a point, but why dont you just use:

    UITableViewRowAnimationNone

    I mean instead of :

    [tableView reloadRowsAtIndexPaths:indicesToReload withRowAnimation:UITableViewRowAnimationAutomatic];
    

    use

    [tableView reloadRowsAtIndexPaths:indicesToReload withRowAnimation:UITableViewRowAnimationNone];
    
    0 讨论(0)
  • 2021-02-02 12:26

    I had a similar issue where I wanted to expand a cell when a switch is activated to display and extra label and button in the cell that is normally hidden when the cell is at its default height (44). I tried various versions of reloadRowsAtPath to no avail. Finally I decided to keep it simpler by adding a condition at heightForRowAtIndexPath like so:

        override func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if ( indexPath.row == 2){
            resetIndexPath.append(indexPath)
            if resetPassword.on {
                // whatever height you want to set the row to
                return 125
            }
        }
        return 44
    }
    

    And in whatever code you want to trigger the expanding of the cell just insert tableview.reloadData(). In my case it was when a switch was turned on to indicate the desire to reset a password.

            @IBAction func resetPasswordSwitch(sender: AnyObject) {
    
            tableView.reloadData()
            }
    

    There is no lag with this approach, no visible way to see that the table reloaded and the expansion of the sell is done gradually like you'd expect. Hope this helps someone.

    0 讨论(0)
  • 2021-02-02 12:28

    Your problem is in setExpanded: in JVCell.m, you are directly editing the frame of the target cell in that method.

    - (void)setExpanded:(BOOL)expanded
    {
        expanded_ = expanded;
    
        CGFloat newHeight = heightCollapsed_;
        if( expanded_ ) newHeight = heightExpanded_;
    
        CGRect frame = self.frame;
        frame.size.height = newHeight;
        self.frame = frame;
    }
    

    Update it to:

    - (void)setExpanded:(BOOL)expanded
    {
        expanded_ = expanded;
    }
    

    Then remove the call to -reloadRowsAtIndexPaths:withRowAnimation: at line 163 of JVViewController.m and it will animate as expected.

    -reloadRowsAtIndexPaths:withRowAnimation: expects different cells to be returned for the provided indexPaths. Since you are only adjusting sizes -beginUpdates & -endUpdates is sufficient to layout the table view cells again.

    0 讨论(0)
  • 2021-02-02 12:36

    @Javy, i noticed some weird behavior while testing your app.

    While running on iPhone 5.0 simulator the previousIndexpth_ variable is of

    class NSArray (it looks like.) here is the debugger output

     (lldb) po previousIndexPath_
        (NSIndexPath *) $5 = 0x06a67bf0 <__NSArrayI 0x6a67bf0>(
    
        <JVSectionData: 0x6a61230>,
    
        <JVSectionData: 0x6a64920>,
    
        <JVSectionData: 0x6a66260>
        )
    
        (lldb) po [previousIndexPath_ class]
    
        (id) $7 = 0x0145cb64 __NSArrayI
    

    Whereas in iPhone 4.3 simulator it is of type NSIndexPath.

    lldb) po [previousIndexPath_ class]
    
    (id) $5 = 0x009605c8 NSIndexPath
    
    (lldb) po previousIndexPath_
    
    (NSIndexPath *) $6 = 0x04b5a570 <NSIndexPath 0x4b5a570> 2 indexes [0, 0]
    

    Are you aware of this issue? Not sure whether this will help but thought of letting you know.

    0 讨论(0)
  • 2021-02-02 12:37

    This problem is caused by returning cached cells in cellForRowAtIndexPath. The reloadRowsAtIndexPaths is expecting to get fresh new cells from cellForRowAtIndexPath. If you do that you will be ok ... no workarounds required.

    From Apple doc: "Reloading a row causes the table view to ask its data source for a new cell for that row."

    0 讨论(0)
  • 2021-02-02 12:38

    I think you should not retain the previous indexPath when the cell is not expanded, try by modifying you did select method like the below, its working fine..

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    BOOL currentCellSameAsPreviousCell = NO;
    NSMutableArray *indicesToReload = [NSMutableArray array];
    
    if(previousIndexPath_ != nil)
    {
        if( [previousIndexPath_ compare:indexPath] == NSOrderedSame ) currentCellSameAsPreviousCell = YES;
    
        JVCell *previousCell = (JVCell*)[self cellForIndexPath:previousIndexPath_];
    
        BOOL expanded = [previousCell expanded];
        if(expanded) 
        {
         [previousCell setExpanded:NO];
        }
        else if  (currentCellSameAsPreviousCell)
        {
            [previousCell setExpanded:YES];
        }
        [indicesToReload addObject:[previousIndexPath_ copy]];
    
        if (expanded)
            previousIndexPath_ = nil;
        else
            previousIndexPath_ = [indexPath copy];         
    }
    
    if(currentCellSameAsPreviousCell == NO)
    {
        JVCell *currentCell = (JVCell*)[self cellForIndexPath:indexPath];
    
        BOOL expanded = [currentCell expanded];
        if(expanded) 
        {
            [currentCell setExpanded:NO];
            previousIndexPath_ = nil;
        }
    
        else
        {
            [currentCell setExpanded:YES];
            previousIndexPath_ = [indexPath copy];
        }
    
        // moving this line to inside the if statement blocks above instead of outside the loop works, but why?
        [indicesToReload addObject:[indexPath copy]];
    
    
    }
    
    // commenting out this line makes the animations work, but the table view background is visible between the cells
    
    [tableView reloadRowsAtIndexPaths:indicesToReload withRowAnimation:UITableViewRowAnimationAutomatic];
    
    // using reloadData completely ruins the animations
    [tableView beginUpdates];
    
     [tableView endUpdates];
    }
    
    0 讨论(0)
提交回复
热议问题