在UITableView中仅更改一个部分的页脚大小

时间:2014-04-12 12:37:12

标签: ios static uilabel uitableview

我有一个静态UITableView分组,其中有4个部分。在所有4个部分中,我有一个标题,只有最后一个部分有一个页脚(代表应用程序的版本)。

因为我使用UITableViewControllerInterface Builder构建了我的Storyboard,所以我可以设置检查器中页眉和页脚边距的大小,因此我将其设置为每个10个。

我面临的问题是,由于检查员的页脚大小,代表页脚的UILabel被切断了。

我正在使用自定义标签,因为我将页脚标题放在中间,使其变为白色并更改字体大小。

以下是代码:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 3)
    {
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];

        UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, screenRect.size.width, 44.0)];

        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 80, tableView.frame.size.width, 80.0)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        NSString* currentAppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

        label.text = [NSString stringWithFormat:@"Version: %@", currentAppVersion];
        label.font = [UIFont systemFontOfSize:14];
        [headerView addSubview:label];

        return label;
    }
    else
    {
        return nil;
    }
}

问题在于,如果我在Inspector中增加页脚边距的大小,它会为每个部分增加它,然后使这些部分之间的间隙更大。

我只是想增加最后一部分的页脚,以便标签不会被遮挡。

我也尝试在上面的代码中更改UIView和UILabel值,但没有运气。

对此的任何指导都将非常感激。

2 个答案:

答案 0 :(得分:4)

您可以使用委托方法- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

尝试这样的事情:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    CGFloat footerHeight = 0;
    if (section == 3) {
        footerHeight = 100;
    }
    return footerHeight;
}

答案 1 :(得分:0)

SWIFT

上面接受的答案满足了我的需求,只是想将来将其转换为Swift。

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    var footerHeight: CGFloat = 0

    if section == 3 {
       footerHeight = 100
    }

    return footerHeight
}