Moving the header when in a UITableView's edit mode
I've got a UITableView with custom cells and also a custom header, like so:
--------------------
| Header |
--------------------
| c开发者_运维百科ell |
| cell |
| etc |
Whenever I put it into edit mode, in order to delete cells, it looks like this
--------------------
| Header |
--------------------
- | cell
- | cell
- | etc
As you can see, the table cells are moved right-wards to make room for for the minus icons. However, the header stays in the same position. How would I move the header to the right, to match the cells x-postion below?
You can add a label in your header in -(UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)section like this :
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 your header view
Hope it helps you.
-(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 x value in cellpositionX.
Also each time when TableView starts and ends editing. you need to reload TableView
I hope this will solve your problem
Created a different nib for the edit mode header, and then show it only 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;
}
精彩评论