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

Настроить раздел заголовка UITableView

Я хочу настроить заголовок UITableView для каждого раздела. Пока я реализовал

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

этот метод UITabelViewDelegate. Я хочу получить текущий заголовок для каждого раздела и просто добавить UILabel в качестве подпредставления.

Пока что я не могу этого сделать. Потому что я не смог найти ничего, чтобы получить заголовок раздела по умолчанию. Первый вопрос, есть ли способ получить заголовок раздела по умолчанию?

Если это невозможно, мне нужно создать контейнерное представление, которое является UIView но на этот раз мне нужно установить цвет фона по умолчанию, цвет тени и т.д. Потому что, если вы внимательно посмотрите на заголовок раздела, он уже настроен.

Как я могу получить эти значения по умолчанию для каждого заголовка раздела?

Спасибо вам всем.

4b9b3361

Ответ 1

Вы можете попробовать следующее:

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
     NSString *string =[list objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:string];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}

Ответ 2

Выбранный ответ с использованием tableView :viewForHeaderInSection: правильный.

Просто поделитесь советами здесь.

Если вы используете раскадровку /xib, тогда вы можете создать другую ячейку прототипа и использовать ее для своей "секции ячейки". Код для настройки заголовка аналогичен тому, как вы настраиваете ячейки ячеек.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *HeaderCellIdentifier = @"Header";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeaderCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HeaderCellIdentifier];
    }

    // Configure the cell title etc
    [self configureHeaderCell:cell inSection:section];

    return cell;
}

Ответ 3

Быстрая версия Lochana Tejas ответ:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 18))
    let label = UILabel(frame: CGRectMake(10, 5, tableView.frame.size.width, 18))
    label.font = UIFont.systemFontOfSize(14)
    label.text = list.objectAtIndex(indexPath.row) as! String
    view.addSubview(label)
    view.backgroundColor = UIColor.grayColor() // Set your background color

    return view
}

Ответ 4

Если вы используете заголовок заголовка по умолчанию, вы можете изменить текст на нем с помощью

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

Для Swift:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

Если вы хотите настроить представление, вам нужно создать новый.

Ответ 6

Если headerInSection не отображается, попробуйте это.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

Это возвращает высоту для заголовка данного раздела.

Ответ 7

Попробуйте это......

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{
    // Background view is at index 0, content view at index 1
    if let bgView = view.subviews[0] as? UIView
    {
        // do your stuff
    }

    view.layer.borderColor = UIColor.magentaColor().CGColor
    view.layer.borderWidth = 1
}

Ответ 8

Свифт 3 версии lochana и estemendoza отвечает:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let view = UIView(frame: CGRect(x:0, y:0, width:tableView.frame.size.width, height:18))
    let label = UILabel(frame: CGRect(x:10, y:5, width:tableView.frame.size.width, height:18))
    label.font = UIFont.systemFont(ofSize: 14)
    label.text = "This is a test";
    view.addSubview(label);
    view.backgroundColor = UIColor.gray;
    return view

}

Кроме того, имейте в виду, что вы также должны реализовать:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 100;
}

Ответ 9

Другие ответы хорошо воссоздают представление заголовка по умолчанию, но на самом деле не отвечают на ваш главный вопрос:

Есть ли способ получить заголовок раздела по умолчанию?

Есть способ - просто реализовать tableView:willDisplayHeaderView:forSection: в вашем tableView:willDisplayHeaderView:forSection:. Представление заголовка по умолчанию будет передано во второй параметр, и оттуда вы можете привести его к UITableViewHeaderFooterView а затем добавить/изменить подпредставления по своему желанию.

Obj-C,

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;

    // Do whatever with the header view... e.g.
    // headerView.textLabel.textColor = [UIColor whiteColor]
}

стриж

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
    let headerView = view as! UITableViewHeaderFooterView

    // Do whatever with the header view... e.g.
    // headerView.textLabel?.textColor = UIColor.white
}

Ответ 10

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //put your values, this is part of my code
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)];
    [view setBackgroundColor:[UIColor redColor]];
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 150, 20)];
    [lbl setFont:[UIFont systemFontOfSize:18]];
    [lbl setTextColor:[UIColor blueColor]];
    [view addSubview:lbl];

    [lbl setText:[NSString stringWithFormat:@"Section: %ld",(long)section]];

    return view;
}

Ответ 11

Это самое простое решение. Следующий код может использоваться непосредственно для создания заголовка настраиваемого раздела.

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SectionHeaderTableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:@"sectionHeader"];

    //For creating a drop menu of rows from the section
    //==THIS IS JUST AN EXAMPLE. YOU CAN REMOVE THIS IF-ELSE.==
    if (![self.sectionCollapsedArray[section] boolValue])
    {
        headerView.imageView.image = [UIImage imageNamed:@"up_icon"];
    }
    else
    {
        headerView.imageView.image = [UIImage imageNamed:@"drop_icon"];
    }

    //For button action inside the custom cell
    headerView.dropButton.tag = section;
    [headerView.dropButton addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchUpInside];

    //For removing long touch gestures.
    for (UIGestureRecognizer *recognizer in headerView.contentView.gestureRecognizers)
    {
        [headerView.contentView removeGestureRecognizer:recognizer];
        [headerView removeGestureRecognizer:recognizer];
    }

    return headerView.contentView;
}

ПРИМЕЧАНИЕ. SectionHeaderTableViewCell - это настраиваемый UITableViewCell, созданный в Storyboard.

Ответ 12

Если бы я был вами, я бы сделал метод, который возвращает UIView, если он содержит NSString. Например

+ (UIView *) sectionViewWithTitle:(NSString *)title;

В реализации этого метода создайте UIView, добавьте UILabel к нему со свойствами, которые вы хотите установить, и, конечно же, установите его заголовок для данного.

Ответ 13

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){

        UITableViewHeaderFooterView *headerView = view;

        [[headerView textLabel] setTextColor:[UIColor colorWithHexString:@"666666"]];
        [[headerView textLabel] setFont:[UIFont fontWithName:@"fontname" size:10]];
    }
}

Если вы хотите изменить шрифт textLabel в заголовке раздела, вы хотите сделать это в файле willDisplayHeaderView. Чтобы установить текст, вы можете сделать это в viewForHeaderInSection или titleForHeaderInSection. Удачи!

Ответ 14

Магически добавьте заголовок таблицы в быстрый

Недавно я попробовал это.

Мне нужен один и только один заголовок во всем UITableView.

Как я хотел UIImageView в верхней части TableView. Поэтому я добавил UIImageView поверх UITableViewCell и автоматически добавил его как tableViewHeader. Теперь я подключаю ImageView к ViewController и добавил изображение.

Я был смущен, потому что я сделал что-то подобное в первый раз. Поэтому, чтобы очистить мою путаницу, откройте xml-формат MainStoryBoard и обнаружил, что Image View был добавлен как заголовок.

Это сработало для меня. Спасибо xCode и быстро.

Ответ 15

@samwize решение в Swift (так что повышайте его!). Блестящий, используя тот же механизм рециркуляции также для разделов заголовка/нижнего колонтитула:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let settingsHeaderSectionCell:SettingsHeaderSectionCell = self.dequeueReusableCell(withIdentifier: "SettingsHeaderSectionCell") as! SettingsHeaderSectionCell

    return settingsHeaderSectionCell
}

Ответ 16

вызов этого метода делегата

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

return @"Some Title";
}

это даст возможность автоматически добавлять заголовок по умолчанию с динамическим заголовком.

Вы можете использовать многоразовый и настраиваемый верхний/нижний колонтитул.

https://github.com/sourov2008/UITableViewCustomHeaderFooterSection

Ответ 17

swif 4.2

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    guard let header = view as? UITableViewHeaderFooterView else { return }

    header.textLabel?.textAlignment = .center // for all sections

    switch section {
    case 1:  //only section No.1
        header.textLabel?.textColor = .black
    case 3:  //only section No.3
        header.textLabel?.textColor = .red
    default: //
        header.textLabel?.textColor = .yellow
    }
}

Ответ 19

Если вы просто хотите добавить заголовок в заголовок tableView, не добавляйте представление. В swift 3.x код выглядит следующим образом:

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    var lblStr = ""
    if section == 0 {
        lblStr = "Some String 1"
    }
    else if section == 1{
        lblStr = "Some String 2"
    }
    else{
        lblStr = "Some String 3"
    }
    return lblStr
}

Вы можете реализовать массив для выбора заголовка для заголовков.

Ответ 20

Возвращаясь к исходному вопросу (4 года спустя), вместо того, чтобы перестраивать собственный заголовок раздела, iOS может просто позвонить вам (с помощью willDisplayHeaderView: forSection:) сразу после его создания по умолчанию. Например, я хотел добавить кнопку графика на правый край заголовка раздела:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView * header = (UITableViewHeaderFooterView *) view;
    if (header.contentView.subviews.count >  0) return; //in case of reuse
    CGFloat rightEdge = CGRectGetMaxX(header.contentView.bounds);
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(rightEdge - 44, 0, 44, CGRectGetMaxY(header.contentView.bounds))];
    [button setBackgroundImage:[UIImage imageNamed:@"graphIcon"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(graphButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
}

Ответ 21

Используйте tableView: willDisplayHeaderView:, чтобы настроить представление, когда оно будет отображаться.

Это дает вам преимущество в том, что вы можете принять представление, которое уже было создано для представления заголовка, и расширить его, вместо того, чтобы самостоятельно воссоздать все представление заголовка.

Вот пример, который окрашивает секцию заголовка на основе BOOL и добавляет текстовый элемент детали в заголовок.

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
//    view.tintColor = [UIColor colorWithWhite:0.825 alpha:1.0]; // gray
//    view.tintColor = [UIColor colorWithRed:0.825 green:0.725 blue:0.725 alpha:1.0]; // reddish
//    view.tintColor = [UIColor colorWithRed:0.925 green:0.725 blue:0.725 alpha:1.0]; // pink

    // Conditionally tint the header view
    BOOL isMyThingOnOrOff = [self isMyThingOnOrOff];

    if (isMyThingOnOrOff) {
        view.tintColor = [UIColor colorWithRed:0.725 green:0.925 blue:0.725 alpha:1.0];
    } else {
        view.tintColor = [UIColor colorWithRed:0.925 green:0.725 blue:0.725 alpha:1.0];
    }

    /* Add a detail text label (which has its own view to the section header… */
    CGFloat xOrigin = 100; // arbitrary
    CGFloat hInset = 20;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin + hInset, 5, tableView.frame.size.width - xOrigin - (hInset * 2), 22)];

    label.textAlignment = NSTextAlignmentRight;

    [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]
    label.text = @"Hi.  I'm the detail text";

    [view addSubview:label];
}

Ответ 22

Полный пример 2019 года для копирования и вставки

Сначала установите "Сгруппированный" на раскадровке: это должно произойти во время int, вы не можете установить его позже, поэтому легче запомнить это на раскадровке:

enter image description here

Далее

Необходимо реализовать heightForHeaderInSection из-за ошибки Apple.

func tableView(_ tableView: UITableView,
                   heightForHeaderInSection section: Int) -> CGFloat {
    return CGFloat(70.0)
}

В течение десяти лет все еще существует ошибка Apple, когда она просто не отображает первый заголовок (т.е. индекс 0), если у вас нет вызова heightForHeaderInSection.

Итак, tableView.sectionHeaderHeight = 70 просто не работает, он сломан.

Установка рамки ничего не дает:

В viewForHeaderInSection просто создайте UIView().

Это бессмысленно/ничего не дает, если вы UIView (frame...), поскольку iOS просто устанавливает размер представления, как определено таблицей.

Таким образом, первая строка viewForHeaderInSection будет просто let view = UIView(), и это тот вид, который вы возвращаете.

func tableView(_ tableView: UITableView,
                       viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()

    let l = UILabel()
    view.addSubview(l)
    l.bindEdgesToSuperview()
    l.backgroundColor = .systemOrange
    l.font = UIFont.systemFont(ofSize: 15)
    l.textColor = .yourClientsFavoriteColor

    switch section {
    case 0:
        l.text =  "First section on screen"
    case 1:
        l.text =  "Here the second section"
    default:
        l.text =  ""
    }

    return view
}

Что это - все остальное - пустая трата времени.

Еще одна "суетливая" проблема Apple.


Расширение удобства, использованное выше:

extension UIView {

    // incredibly useful:

    func bindEdgesToSuperview() {

        guard let s = superview else {
            preconditionFailure("'superview' nil in bindEdgesToSuperview")
        }

        translatesAutoresizingMaskIntoConstraints = false
        leadingAnchor.constraint(equalTo: s.leadingAnchor).isActive = true
        trailingAnchor.constraint(equalTo: s.trailingAnchor).isActive = true
        topAnchor.constraint(equalTo: s.topAnchor).isActive = true
        bottomAnchor.constraint(equalTo: s.bottomAnchor).isActive = true
    }
}