How to delete sections from static cell tableview - objective-c

I'm trying to delete or hide a section inside a tableview with static cells on it. I am trying to hide it in function viewDidLoad. Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView beginUpdates];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES];
[self.tableView endUpdates];
[self.tableView reloadData];
}
Still the sections appear. I am using storyboards in it. Can you please help me out?Thanks!

I found it most convenient to hide sections by overriding numberOfRowsInSection.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ( section == 1 )
// Hide this section
return 0;
else
return [super tableView:self.tableView numberOfRowsInSection:section];
}

Check the answer found here. How to remove a static cell from UITableView using Storyboards Seems to be a bugger of a problem when using static cells. Hope this helps.

Here you go. This one also removes the vertical space.
NSInteger sectionToRemove = 1;
CGFloat distance = 10.0f; // minimum is 2 since 1 is minimum for header/footer
BOOL removeCondition; // set in viewDidLoad
/**
* You cannot remove sections.
* However, you can set the number of rows in a section to 0,
* this is the closest you can get.
*/
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
if (removeCondition && section == sectionToRemove)
return 0;
else
return [super tableView:self.tableView numberOfRowsInSection:section];
}
/**
* In this case the headers and footers sum up to a longer
* vertical distance. We do not want that.
* Use this workaround to prevent this:
*/
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section {
return removeCondition &&
(section == sectionToRemove - 1 || section == sectionToRemove)
? distance / 2
: distance;
}
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section {
return removeCondition &&
(section == sectionToRemove || section == sectionToRemove + 1)
? distance / 2
: distance;
}

It seems that reloadData makes table view to re-read dataSource. You should also remove data from dataSource before calling reloadData. If you are using array, remove object you want with removeObject: before calling reloadData.

Related

Static Table View Cell Labels are getting Squished into Header

EDIT
My constraints on custom cells are all weird. Please disregard. Just using standard Left Detail ones now.
I don't understand why in iOS8 now my static table view cell contents are collapsing into the header of each section.
There's really almost nothing in my SettingsTVC.m. This is a just a test with all static data in the storyboard:
- (void)viewDidLoad {
[super viewDidLoad];
}
Have you implemented TableView delegate methods properly i.e
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == CUSTOM_SECTION)
{
return CUSTOM_VALUE;
}
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return height;
}

How to add a header to the top of a tableview and to every section

Is it possible to add a header to the top of a tableview as well as a header to the top of every section in the tableview?
An example would be an image at the top of the tableview and then a row heading label at the top of every section.
Just create a header for the first section that contains your standard header as well as the header that you want for the top of your tableView. Configure the UITableViewDelegate to return the top header only for the first section, otherwise use the standard header.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section == 0) {
MyTopHeader *header = [[MyTopHeader alloc] init];
return header;
}
else {
MySectionHeader *header = [[MySectionHeader alloc] init];
return header;
}
}
You will probably need to customize the height as well.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0) {
return 150.0f; //top header height
}
else {
return 70.0f; // section header height
}
}

Making a conditional for table view buttonpress

I have a table view and don't know how to access the row number when the user presses one of the cells. The data of the table view is a small array and this is the closest I could come... In the IB, I control dragged the reusable cell to the view controller I push which works fine. I am unsure how to get the row number in a conditional like this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// A bunch of stuff is before this, but not important to the question
//-- > This line if([indexPath row] == 1) This doesn't work... any ideas?
{
if(totalPoints < 10 && number != 0)
{
[self saveNumber:#"1"];
}
else if (totalPoints < 10 && attachment == 0)
{
[self saveData:#"1"];
int newPnts = totalPoints + 1;
[self savePoints:[NSString stringWithFormat: #"%d", newPnts]];
}
Thanks
It should work
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",indexPath.row);
if(indexPath.row==1)
{
NSLog(#"Do Somthing..");
}
else
{
NSLog(#"Do Somthing..");
}
}
the row number is [indexPath row] . in tableview you have a dataSource which save the data array used to reload table
Ok I was doing something wrong elsewhere and the view controller doesn't update it's label from the saved data. I will post a seperate question if you're interested

How to hide a section in a table view controller with static cells?

I have a UITableViewController with static cells, with 3 sections, and a segmented control with 2 buttons.
I would like to achieve the following behavior:
when button 1 is pressed hide section 2
when button 2 is pressed hied section 3
I cannot find a solution to this.
Any tip is useful.
Thanks.
Simple, just make sure you set your UITableViewDelegate, and you can use heightForRowAtIndexPath: (and similar for headers and footers) to show/hide the cells by setting their height to 0.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2) {
if (self.shouldShowSection2) {
return 44.0f;
}else{
return 0.0f;
}
}else if (indexPath.section == 3) {
if (self.shouldShowSection3) {
return 44.0f;
}else{
return 0.0f;
}
}else{
return 44.0f;
}
}
Then just define some logic within your IBAction to change these BOOLs around in between tableview's begin/end updates, and the table will show/hide the sections you want.
- (IBAction)toggleSegment:(UISegmentedControl *)sender
{
[self.tableView beginUpdates];
// change boolean conditions for what to show/hide
[self.tableView endUpdates];
}

Changing in Code Specific Row Heights in Objective-C

I have a UITableView that uses prototype cells to recreate a table. I have the first row with height 30 and the rest default 44. I would like to change only first row's height but have failed to do so. I tried cellForRowAtIndexPath but I learned here that it's heightForRowAtIndexPath: I'm interested in. However, all the IndexPath's that get sent to it are [0,0]. Can anyone help?
Have you tried this?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) { //first row
return 30;
}
else {
return 44;
}
}
Do this for resizing the cells in your table views delegate.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return 30.0;
return 44.0;
}
If this doesn't work: give us your code