Moving a Title in UITableView Editing Mode

I have a UITableView with custom cells, as well as a custom header, for example:

-------------------- | Header | -------------------- | cell | | cell | | etc | 

Whenever I put it into edit mode to delete cells, it looks like

 -------------------- | Header | -------------------- - | cell - | cell - | etc 

As you can see, the table cells are moved to the right to make room for minus signs. However, the heading remains in the same position. How to move the title to the right to fit the x-postion cell below?

+4
source share
3 answers

Created a different header for the edit mode header, and then it will only show it when editing:

 -(UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)section { // CustomHeader creates the header view CustomHeader *tableHeader; if (self.tableView.editing) { tableHeader = (CustomHeader *)[CustomHeader headerFromNibNamed:@"CustomHeaderEditMode"]; } else { tableHeader = (CustomHeader *)[CustomHeader headerFromNibNamed:@"CustomHeader"]; } return tableHeader; } 
-1
source

You can add a label to your title in the (UIView *) tableView: (UITableView *) tv viewForHeaderInSection: (NSInteger) section, for example:

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, self.tableView.bounds.size.width - 45,50)]; label.font = [UIFont systemFontOfSize:14]; label.backgroundColor = [UIColor greenColor]; if(self.tableView.editing && section==0) { label.frame = CGRectMake(0,0,self.tableView.bounds.size.width,150); NSString *NewSection = @"My new header message in edit mode d\n\n OK"; label.text = NewSection; } else { label.frame = CGRectMake(0,0,self.tableView.bounds.size.width,40); label.text = [self tableView:tableView titleForHeaderInSection:section]; } 

[sectionHeader addSubview: label]; Then add it to the header view

Hope this helps you.

0
source
 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView* viewObj = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40]; 

UILabel *headerLabel = [[UILabel alloc] init];

 headerLabel.font = [UIFont systemFontOfSize:14]; headerLabel.backgroundColor = [UIColor greenColor]; if(self.tableView.editing && section==0) { headerLabel.frame = CGRectMake(cellpositionX,0,self.tableView.bounds.size.width - cellpositionX,40); NSString *NewSection = @"My new header message in edit mode d\n\n OK"; headerLabel.text = NewSection; } else { headerLabel.frame = CGRectMake(0,0,self.tableView.bounds.size.width,40); headerLabel.text = [self tableView:tableView titleForHeaderInSection:section]; } [viewObj addSubview:headerLabel]; return viewObj;} 

In the above code, you can pass the value of x to cellpositionX.

Also, every time a TableView starts and ends editing. you need to reload TableView

I hope this solves your problem.

0
source

All Articles