2013-03-20 80 views
-1

我需要在tableview標題中設置標題名稱。 IM在如何在viewForHeaderInSection的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; 
} 

由於使用此代碼的答覆....

+0

你的問題還不清楚。您發佈的代碼已經設置了標題的標題('lbl_header.text = str_email_id1;')。你真正問的是什麼問題? – rmaddy 2013-03-20 17:30:37

+0

str_email_id1是字符串對象,並在其中存儲標題名稱。 – Raj 2013-03-20 17:49:10

回答

5

使用titleForHeaderInSection,從UITableViewDataSource協議。確保numberOfSectionsInTableView也得到很好的實施。

樣品:

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

標題名稱從一個類到另一個類在這裏代碼爲HOAViewController * obj_hoaview_controll = [[HOAViewController alloc] init]; [obj_hoaview_controll fun_get_login_email_id] ;. – Raj 2013-03-20 17:46:19

+0

這可以使用你的邏輯來處理,但在上面的功能,它會工作。如果是,請將答案標記爲正確(複選標記) – 2013-03-20 17:51:07

+0

標題名稱從一個類到另一個類,最後在lbl_header.text對象中。我從子類獲得標題名稱,以及如何重新加載viewForHeaderInSection函數或在該類中調用viewForHeaderInSection。 – Raj 2013-03-20 17:52:08