简体   繁体   中英

Force cellForRowAtIndexPath to be called when numberOfRowsInSection equals to zero

I want cellForRowAtIndexPath to be always called even if numberOfRowsInSection ==0 as i'm hiding and showing a section inside the table by setting numberOfRowsInSection to zero but when i do that the method stated above doesn't executed as numberOfRowsInSection becomes zero which makes a problem as the contents of the cell remains rendered although the section header disappears, any idea on how to hide the contents of the cell after setting the numberOfRowsInSection to zero:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (section==0||section==1)
    return 1;

if (section==2) {
    if (self.isSectionAppears)
        return 1;
    else
        return 0; 

}

}

given that self.isSectionAppears is a bool property setted to false when i want to hide the section

You shouldn't be burying your section showing/hiding logic in your cellForRowAtIndexPath. When you need to tell the tableview to update the section header, you can do a refresh on the section with the code

NSUInteger sectionToRefresh = //index of table section with header you want to refresh.
NSIndexSet indexSetWithSection = [NSIndexSet indexSetWithIndex:sectionToRefresh];
[tableView reloadSections:indexSetWithSection withRowAnimation:UITableViewRowAnimationAutomatic];

If you'd like to refresh visual elements of your section header without refreshing the rest of the section, you could do so by acting on your section header object directly:

//Get section header from table
UITableViewHeaderFooterView* myHeader = [tableView headerViewForSection:sectionToRefresh];

//Update header
[myHeader.textLabel setText:newSectionHeaderText]; //Update it's text for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM