我无法弄清楚如何在我的UITableViewController中的headerView中重新显示我更改的文本。我做了一个“removeFromSuperview”,它确实被删除了,但是当我将它重新添加到我的subView后它不会重新显示新的headerView。这是我的代码:
- (UIView *)headerView
{
if (headerView){
[headerView removeFromSuperview];
[headerView release];
}
UIButton *headerLabel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (displayOnly == NO)
[headerLabel setTitle:@"Click here to switch to display only mode." forState:UIControlStateNormal];
else
[headerLabel setTitle:@"Click here to switch to practice session mode." forState:UIControlStateNormal];
float w = [[UIScreen mainScreen] bounds].size.width;
CGRect headerLabelFrame = CGRectMake(8.0, 8.0, w - 16.0, 30.0);
[headerLabel setFrame:headerLabelFrame];
CGRect headerViewFrame = CGRectMake(0, 0, w, 48);
[headerLabel addTarget:self action:@selector(switch) forControlEvents:UIControlEventTouchUpInside];
headerView = [[UIView alloc] initWithFrame:headerViewFrame];
[headerView addSubview:headerLabel];
return headerView;
}
- (void) switch
{
if (displayOnly == YES)
{
displayOnly = NO;
[self headerView];
}
else
{
displayOnly = YES;
[self headerView];
}
}
答案 0 :(得分:0)
您是否尝试过调试应用?你在表中添加headerView的位置?你需要像这样改变代码
- (void) switch
{
if (displayOnly == YES)
{
displayOnly = NO;
self.tableView.tableHeaderView = [self headerView];
}
else
{
displayOnly = YES;
self.tableView.tableHeaderView = [self headerView];
}
}
答案 1 :(得分:0)
您是否在设置新标题后尝试拨打[headerView setNeedsDisplay];
而不是每次都重新创建视图?
也许你根本不需要这样做。如果您存储UIButton
和UIView
,如果我是对的,当您为其添加新标题时,它会自动重绘。像这样:
UIButton* headerLabel;
UIView* headerView;
并在您的.m文件中
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [self headerView];
}
- (UIView *)headerView{
if(headerView){
return headerView;
}
if(!headerLabel){
headerLabel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
if (displayOnly == NO)
[headerLabel setTitle:@"Click here to switch to display only mode." forState:UIControlStateNormal];
else
[headerLabel setTitle:@"Click here to switch to practice session mode." forState:UIControlStateNormal];
float w = [[UIScreen mainScreen] bounds].size.width;
CGRect headerLabelFrame = CGRectMake(8.0, 8.0, w - 16.0, 30.0);
[headerLabel setFrame:headerLabelFrame];
CGRect headerViewFrame = CGRectMake(0, 0, w, 48);
[headerLabel addTarget:self action:@selector(switch) forControlEvents:UIControlEventTouchUpInside];
headerView = [[UIView alloc] initWithFrame:headerViewFrame];
[headerView addSubview:headerLabel];
return headerView;
}
- (void) switch{
if (displayOnly == YES){
displayOnly = NO;
[headerLabel setTitle:@"Click here to switch to display only mode." forState:UIControlStateNormal];
} else{
displayOnly = YES;
[headerLabel setTitle:@"Click here to switch to practice session mode." forState:UIControlStateNormal];
}
//may be there's need to make additional call
[headerView setNeedsDisplay];
}
这样您就可以保持UIView
和UIButton
的同一个实例,只更新它的标题,并可能通过拨打[headerView setNeedsDisplay];
来手动调用重绘。
答案 2 :(得分:0)
在此代码中:
if (headerView){
[headerView removeFromSuperview];
[headerView release];
}
您从超级视图中删除了headerView
,但在创建新的headerView
之后却未将其添加到超级视图中。
答案 3 :(得分:0)
我认为您需要调用表重新加载数据然后才能工作。