How to remove a Static Cell from a UITableView designed in StoryBoard

前端 未结 9 2481
情话喂你
情话喂你 2020-12-13 09:43

The solution is probably very simple, but I couldn\'t just find it .. !

Working with storyboard (iOS 5), I have a tableViewController, and a designed ST

相关标签:
9条回答
  • 2020-12-13 10:14

    If you want to permanently delete the tableview cell, just call deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone] with the an indexPath corresponding to your row. You will also need to decrement the number of rows returned by numberOfRowsInSection:.

    However, I was looking for a way to temporarily "delete" unwanted static tableview cells from a static tableview, because I wanted a particular row to be there sometimes, and other times not. I also wanted to be able to update this on demand from outside of the tableview controller.

    My case is fairly simple, since it is the first row that was being either shown or hidden. You can generalize to suit your own needs. My tableview controller is my data source delegate.

    First, I created a public method in the tableview controller to update the state variables and trigger the redisplay:

    - (void)updateSettingsDisplay:(BOOL)hideSetting
    {
        if (hideSetting == self.hideSetting) {
            return; // no need to do anything because it didn't change
        }
    
        self.hideSetting = hideSetting;
        self.numRows = (hideSetting)? kNumRowsWithSetting : kNumRowsWithoutSetting;
    
        // redisplay only if we're visible
        if (!self.viewJustLoaded && (self.navController.visibleViewController == self)) {
            [self.tableView reloadData];
        }
        self.viewJustLoaded = NO;
    }
    

    The tableview controller's viewDidLoad looks like:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // check whether or not to display out-of-coverage tableview cell
        self.hideSetting = YES; // initial value; could just as easily be NO
        self.viewJustLoaded = YES;
        [self updateSettingsDisplay];
    }
    

    The tableview's data-source delegate:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return self.numRows;
    }
    

    and finally

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // set row to the index of the stored tableView cell that corresponds to the
        // cell you want (its location in the static tableview from your storyboard)
        NSInteger row = indexPath.row;
        if (self.hideSetting) {
            // skip drawing setting's tableviewcell (since the setting we're hiding is
            // first, we can just increment the row to get the one we want)
            ++row;
            assert(row < kTotalRows); // bounds checking just to convince yourself (remove after testing)
        }
        // get a new index path since the row field is read-only
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];
    
        // grab the cell from super that corresponds to the cell you want
        UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:newIndexPath]; // static table
    
        return cell;
    }
    

    The trick is that the static cells are always available in [super tableView:tableView cellForRowAtIndexPath:newIndexPath] - so use that for permanent storage of your static tableview cells. Then adjust the number of rows as needed, and map the rows correctly (ie, get the row of the stored cell that corresponds to the cell you want displayed) in your tableview delegate's cellForRowAtIndexPath:.

    The updateSettingsDisplay method can be called on your tableview controller by any class that retains it. If the tableview controller is not visible when it is called, it will just update the state and wait until next time it becomes visible to change the display.

    0 讨论(0)
  • 2020-12-13 10:14

    you might want to look at this

    https://github.com/xelvenone/StaticDataTableViewController/

    usage

    self.hideSectionsWithHiddenRows = YES; //YES, NO
    [self cell:self.outletToMyStaticCell1 setHidden:hide];
    [self cell:self.outletToMyStaticCell2 setHidden:hide];
    [self reloadDataAnimated:YES];
    
    0 讨论(0)
  • 2020-12-13 10:16

    You need to put beginUpdates before the delete call and endUpdates after:

    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:r inSection:s]]];
    [tableView endUpdates];
    

    In response to your comment:

    - (NSInteger)tableView:(UITableView)tableView numberOfRowsInSection:(NSInteger)section {
        NSInteger ret;
        switch (section) {
            case 0:
                // sectionZeroRows doesnt have to be a 
                // property (an attribute works just fine), but it
                // helps with explaining this example.
                ret = self.sectionZeroRows;
                break;
            // ...
        }
        return ret;
    }
    
    - (void)someMethod {
        --self.sectionZeroRows;
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]];
        [tableView endUpdates];
    }
    
    0 讨论(0)
提交回复
热议问题