Как изменить высоту сгруппированы UITableView заголовка?



Я знаю, как изменить высоту заголовков разделов в виде таблицы. Но я не могу найти никакого решения, чтобы изменить интервал по умолчанию перед первым разделом.



сейчас у меня есть этот код:



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0){
return 0;
}
return 10;
}


enter image description here

582   10  

10 ответов:

возвращение CGFLOAT_MIN вместо 0 для желаемой высоты секции.

возврат 0 заставляет UITableView использовать значение по умолчанию. Это недокументированное поведение. Если вы возвращаете очень небольшое число, вы эффективно получить заголовок нулевой высоты.

Swift 3:

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.leastNormalMagnitude
        }
        return tableView.sectionHeaderHeight
    }

Swift:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return CGFloat.min
    }
    return tableView.sectionHeaderHeight
}

Obj-C:

    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return CGFLOAT_MIN;
    return tableView.sectionHeaderHeight;
}

похоже, что я не могу установить представление заголовка таблицы с высотой 0. В итоге я сделал следующее:

- (void)viewWillAppear:(BOOL)animated{
    CGRect frame = self.tableView.tableHeaderView.frame;
    frame.size.height = 1;
    UIView *headerView = [[UIView alloc] initWithFrame:frame];
    [self.tableView setTableHeaderView:headerView];
}

если вы используете tableView стиль сгруппированы,tableView автоматическая установка верхней и нижней вставок. Чтобы избежать их и избежать установки внутренних вставок, используйте методы делегирования для верхнего и нижнего колонтитулов. никогда не возвращайте 0.0 но CGFLOAT_MIN.

С

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    // Removes extra padding in Grouped style
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // Removes extra padding in Grouped style
    return CGFLOAT_MIN;
}

Свифт

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    // Removes extra padding in Grouped style
    return CGFloat.leastNormalMagnitude
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    // Removes extra padding in Grouped style
    return CGFloat.leastNormalMagnitude
}

вы можете попробовать это:

на loadView

_tableView.sectionHeaderHeight = 0;

затем

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

Он должен быть удален до тех пор, пока у вас нет никаких объектов в заголовке...

и если вы хотите какой-то размер sectionheader, то измените только возвращаемое значение.

то же самое, если вы не получите sectionfooter удалены.

_tableView.sectionFooterHeight = 0;

и

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

Ну, это работает для моих проблем с tableview в iOS7.

Это сработало для меня с Swift 4. Измените свой UITableView например viewDidLoad:

// Remove space between sections.
tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

// Remove space at top and bottom of tableView.
tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))
tableView.tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: CGFloat.leastNormalMagnitude)))

можно использовать viewForHeaderInSection и вернуть вид с любой высоты.

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

    int height = 30 //you can change the height 
    if(section==0)
    {
       UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];

       return view;
    }
}

в swift 2.0

func tableView(tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {

        return yourHeight
    }

вы должны удалить код self.tableView.tableHeaderView = [UIView new]; после добавления

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

пример viewForHeaderInSection:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 118)];
view.backgroundColor = COLOR_DEFAULT;

NSString* key = [self.tableKeys objectAtIndex:section];
NSArray *result = (NSArray*)[self.filteredTableData objectForKey:key];
SZTicketsResult *ticketResult = [result objectAtIndex:0];

UIView *smallColoredView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 3)];
smallColoredView.backgroundColor = COLOR_DEFAULT_KOSTKY;
[view addSubview:smallColoredView];

UIView *topBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, 320, 40)];
topBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
[view addSubview:topBackgroundView];

UILabel *totalWinnings = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 300, 40)];
totalWinnings.text = ticketResult.message;
totalWinnings.minimumFontSize = 10.0f;
totalWinnings.numberOfLines = 0;
totalWinnings.backgroundColor = [UIColor clearColor];
totalWinnings.font = [UIFont boldSystemFontOfSize:15.0f];
[view addSubview:totalWinnings];

UIView *bottomBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 320, 58)];
bottomBackgroundView.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:248.0/255.0 blue:174.0/255.0 alpha:1];
[view addSubview:bottomBackgroundView];

UILabel *numberOfDraw = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 290, 58)];
numberOfDraw.text = [NSString stringWithFormat:@"sometext %@",[ticketResult.title lowercaseString]];;
numberOfDraw.minimumFontSize = 10.0f;
numberOfDraw.numberOfLines = 0;
numberOfDraw.backgroundColor = [UIColor clearColor];
numberOfDraw.font = [UIFont boldSystemFontOfSize:15.0f];
[view addSubview:numberOfDraw];

return view;

В Swift 4

удалить лишний отступ сверху в сгруппированы TableView для.

здесь высота задается 1 как минимальная высота для заголовка раздела, потому что вы не можете дать 0, поскольку tableview будет принимать его верхнее поле по умолчанию, если ему назначена нулевая высота.

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

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

Comments

    Ничего не найдено.