更改headerView的子视图的标题

时间:2013-01-07 06:27:24

标签: iphone objective-c ios ipad

我有以下代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frameWidth, 24.0)];
    [header setBackgroundColor:[UIColor grayColor]];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, self.view.frameWidth - 100, header.frameHeight)];
    label.backgroundColor= [UIColor grayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont boldSystemFontOfSize:17.0];

    [header addSubview:label];
    if (section == kEditInformationSection){
        label.text = [AHConstants kEditInformation];

        UIButton *editInformationButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frameWidth - 50, 0, 50, header.frameHeight)];
        [editInformationButton setTag:1000];
        [editInformationButton setBackgroundColor:[UIColor grayColor]];
        [editInformationButton setTitle:[AHConstants kEdit] forState:UIControlStateNormal];
        [editInformationButton addTarget:self action:@selector(switchToEditMode:) forControlEvents:UIControlEventTouchUpInside];
        self.editInformationButton_ = editInformationButton;
        [header addSubview:self.editInformationButton_];
        self.tableHeaderView_ = header;
        return self.tableHeaderView_;
    } else if (section == kNotificationIntervalInfo){
         label.text = [AHConstants kNotificationInterval];
        return header;
    }

    return nil;
}

并且在按钮操作上,我想更改按钮标题,因此我执行了以下操作:

[self.editInformationButton_ setTitle:[AHConstants kDone] forState:UIControlStateNormal];

为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

使用reloadSections:withRowAnimation

//provide your index for section here
[[self yourTableView]reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

使用handle {/ 1>}这些方法只需new title

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section