UITableView : crash when adding a section footer view in empty section

后端 未结 5 578
滥情空心
滥情空心 2020-12-10 13:42

This is the first time I ask a question here, but I have to say this site has been a tremendous help for me over the last couple months (iphone-dev-wise), and I thank you fo

5条回答
  •  囚心锁ツ
    2020-12-10 14:23

    it shouldn't be necessary to call both deleteRowsAtIndexPaths:withAnimation: and reloadData on tableView.

    the problem you are seeing is the problem i see manifesting in the call to deleteRowsAtIndexPaths:withAnimation:, and thus for you, it's never getting to reloadData anyway.

    a simpler solution to having to create new cells, manage them, deal with all of the places where a different cell has to be dealt with, is to use reloadSections:withAnimation: when dealing with a section that is going from 0 to 1 row or 1 to 0 rows.

    i.e. replace the following lines of code:

            if(indexPath.section == 0)
            { [firstSectionArray removeObjectAtIndex:indexPath.row]; }
            else 
            { [secondSectionArray removeObjectAtIndex:indexPath.row]; }
    
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationBottom];
    

    with the following

            BOOL useReloadSection;
            if (indexPath.section == 0)
            {
                [firstSectionArray removeObjectAtIndex:indexPath.row];
                useReloadSection = 0 == firstSectionArray.count;
            }
            else 
            {
                [secondSectionArray removeObjectAtIndex:indexPath.row];
                useReloadSection = 0 == secondSectionArray.count;
            }
    
            if (useReloadSection)
                [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                         withRowAnimation:UITableViewRowAnimationBottom];
            else
                [tableView deleteRowsAtIndexPaths:@[indexPath]
                                 withRowAnimation:UITableViewRowAnimationBottom];
    

提交回复
热议问题