如何在viewForHeaderInSection中的tableview中动态更改标题标题?

时间:2013-03-20 17:23:07

标签: ios objective-c uitableview

我需要在tableview标题中设置标题名称。我在

中的代码中使用它
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  function
{
   IView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    UILabel *lbl_header = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, tableView.bounds.size.width, 30)];
    lbl_header.tag=1000001;
    lbl_header.backgroundColor = [UIColor clearColor];
    lbl_header.textColor = [UIColor colorWithRed:204.0/255 green:204.0/255 blue:204.0/255 alpha:1.0];
    lbl_header.font = [UIFont fontWithName:@"Helvetica-Regular" size:20];

    ////  get the title name from subclass view controller 

    HOAViewController *obj_hoaview_controll = [[HOAViewController alloc]init];
    [obj_hoaview_controll fun_get_login_email_id];


    lbl_header.text = str_email_id1;//[udf_value objectForKey:@"StrEmailGet"];


    if (section == 0)
    {
         [headerView setBackgroundColor:[UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0]];
        //lbl_header.text = [udf_value objectForKey:@"StrEmailGet"];
    }
    else
    {
         [headerView setBackgroundColor:[UIColor clearColor]];
    }
    [headerView addSubview:lbl_header];
    return headerView;
}

感谢您的回复......

1 个答案:

答案 0 :(得分:6)

使用来自UITableViewDataSource协议的titleForHeaderInSection。确保numberOfSectionsInTableView也得到了很好的实施。

<强>示例:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   if (section == 0)
    return @"Title1";
   else if (section == 1)
    return @"Title2";
}