Подтвердить что ты не робот

Как изменить цвет текста для заголовков разделов в Grouped TableView в iPhone SDK?

Я создаю приложение для iPhone, где у меня есть сгруппированный TableView с заголовками для разделов.

Проблема в том, что я хочу изменить цвет текста заголовка раздела.

Как изменить цвет текста заголовка раздела?

Что мне делать?

Пожалуйста, помогите и предложите.

Спасибо.

4b9b3361

Ответ 1

Это СРОЧНО будет работать для вас.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor clearColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        [email protected]"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

просто скопируйте и вставьте эту функцию в свой код.

Ответ 2

Добавьте следующий код в класс AppDelegate в - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions Метод:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];

Ответ 3

Если вы не хотите делать это достаточно широко, как в решении Vahan, вот решение, использующее один из методов UITableViewDelegate:

func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
       headerView.textLabel?.textColor = UIColor.redColor() 
    }
}

Ответ 4

Вы можете реализовать этот метод источника данных в виде таблицы:

- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
   //create your custom label here & anything else you may want to add
   return YourCustomView;
}

Ответ 5

Я построил ответ от @Harsh.

Это самое близкое, что я мог получить, неотличимый от того, что я могу сказать.

Он находится в <UITableViewDataSource> очевидно.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    hView.backgroundColor=[UIColor clearColor];

    UILabel *hLabel=[[[UILabel alloc] initWithFrame:CGRectMake(19,17,301,21)] autorelease];

    hLabel.backgroundColor=[UIColor clearColor];
    hLabel.shadowColor = [UIColor whiteColor];
    hLabel.shadowOffset = CGSizeMake(0.5,1);  // closest as far as I could tell
    hLabel.textColor = [UIColor blackColor];  // or whatever you want
    hLabel.font = [UIFont boldSystemFontOfSize:17];
    hLabel.text = @"Your title here";  // probably from array

    [hView addSubview:hLabel];

    return hView;
}

Ответ 6

@Харшавый ответ отлично подействовал на меня, и, изменив координаты UILabel, вы можете переместить его. Кроме того, я лично подумал немного изменить смещение тени, чтобы сделать его более читаемым, но это может быть личным выбором. Здесь моя версия в случае:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, -5, 300, 30)] autorelease];
    //If you add a bit to x and decrease y, it will be more in line with the tableView cell (that is in iPad and landscape)
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor yellowColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.5., 0.5.);
    label.font = [UIFont boldSystemFontOfSize:18];
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]autorelease];
    [view addSubview:label];

    return view;
}

Ответ 7

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UILabel *myLabel = [[UILabel alloc] init];
  myLabel.frame = CGRectMake(20, 8, 220, 20);
  myLabel.font = [UIFont boldSystemFontOfSize:16];
  myLabel.text = [self tableView:tableView 
  titleForHeaderInSection:section];
  myLabel.backgroundColor=[UIColor grayColor];
  UIView *headerView = [[UIView alloc] init];
  [headerView addSubview:myLabel];
  return headerView;
  }