ios – 在Interface Builder中设计UITableView的section标题

概述我有一个带有UITableView的xib文件,我想使用委托方法tableView:viewForHeaderInSection:添加一个自定义的部分标题视图.有没有可能在Interface Builder中设计它,然后以编程方式更改其中的一些子视图的属性? 我的UITableView具有更多的部分标题,因此在Interface Builder中创建一个UIView并返回它不起作用,因为我必须复制 我有一个带有UItableVIEw的xib文件,我想使用委托方法tableVIEw:vIEwForheaderInSection:添加一个自定义的部分标题视图.有没有可能在Interface Builder中设计它,然后以编程方式更改其中的一些子视图的属性?

我的UItableVIEw具有更多的部分标题,因此在Interface Builder中创建一个UIVIEw并返回它不起作用,因为我必须复制它,但没有任何好的方法.归档和取消归档它对于UIImages不起作用,所以UIImageVIEws将显示空白.

此外,我不想以编程方式创建它们,因为它们太复杂,并且生成的代码将难以阅读和维护.

编辑1:对于那些还不明白的人,真的需要一些无用的代码来理解它,这里是我的tableVIEw:vIEwForheaderInSection:方法:

- (UIVIEw *)tableVIEw:(UItableVIEw *)tableVIEw vIEwForheaderInSection:(NSInteger)section {    if ([tableVIEw.dataSource tableVIEw:tableVIEw numberOfRowsInSection:section] == 0) {        return nil;    }    CGSize headerSize = CGSizeMake(self.vIEw.frame.size.wIDth,100);    /* wrapper */    UIVIEw *wrapperVIEw = [UIVIEw vIEwWithSize:headerSize];    wrapperVIEw.backgroundcolor = [UIcolor colorWithHexString:@"2670ce"];    /* Title */    CGPoint Titlemargin = CGPointMake(15,8);    UILabel *TitleLabel = [UILabel labelWithText:self.categorIEsnames[section] andFrame:CGEasyRectMake(Titlemargin,CGSizeMake(headerSize.wIDth - Titlemargin.x * 2,20))];    TitleLabel.textcolor = [UIcolor whitecolor];    TitleLabel.Font = [UIFont FontWithStyle:FontStyleRegular andSize:14];    [wrapperVIEw addSubvIEw:TitleLabel];    /* body wrapper */    CGPoint bodyWrappermargin = CGPointMake(10,8);    CGPoint bodyWrapperVIEwOrigin = CGPointMake(bodyWrappermargin.x,CGRectGetMaxY(TitleLabel.frame) + bodyWrappermargin.y);    CGSize bodyWrapperVIEwSize = CGSizeMake(headerSize.wIDth - bodyWrappermargin.x * 2,headerSize.height - bodyWrapperVIEwOrigin.y - bodyWrappermargin.y);    UIVIEw *bodyWrapperVIEw = [UIVIEw vIEwWithFrame:CGEasyRectMake(bodyWrapperVIEwOrigin,bodyWrapperVIEwSize)];    [wrapperVIEw addSubvIEw:bodyWrapperVIEw];    /* image */    NSInteger imageSize = 56;    Nsstring *imagename = [self getcategoryResourceItem:section + 1][@"image"];    UIImageVIEw *imageVIEw = [UIImageVIEw imageVIEwWithImage:[UIImage imagenamed:imagename] andFrame:CGEasyRectMake(CGPointZero,CGEqualSizeMake(imageSize))];    imageVIEw.layer.masksToBounds = YES;    imageVIEw.layer.cornerRadius = imageSize / 2;    [bodyWrapperVIEw addSubvIEw:imageVIEw];    /* labels */    NSInteger labelsWIDth = 60;    UILabel *firstLabel = [UILabel labelWithText:@"first" andFrame:CGRectMake(imageSize + bodyWrappermargin.x,labelsWIDth,16)];    [bodyWrapperVIEw addSubvIEw:firstLabel];    UILabel *secondLabel = [UILabel labelWithText:@"second" andFrame:CGRectMake(imageSize + bodyWrappermargin.x,20,16)];    [bodyWrapperVIEw addSubvIEw:secondLabel];    UILabel *thirdLabel = [UILabel labelWithText:@"third" andFrame:CGRectMake(imageSize + bodyWrappermargin.x,40,16)];    [bodyWrapperVIEw addSubvIEw:thirdLabel];    [@[ firstLabel,secondLabel,thirdLabel ] forEachVIEw:^(UIVIEw *vIEw) {        UILabel *label = (UILabel *)vIEw;        label.textcolor = [UIcolor whitecolor];        label.Font = [UIFont FontWithStyle:FontStylelight andSize:11];    }];    /* line */    UIVIEw *lineVIEw = [UIVIEw vIEwWithFrame:CGRectMake(imageSize + labelsWIDth + bodyWrappermargin.x * 2,bodyWrappermargin.y,1,bodyWrapperVIEw.frame.size.height - bodyWrappermargin.y * 2)];    lineVIEw.backgroundcolor = [UIcolor whitecolorWithAlpha:0.2];    [bodyWrapperVIEw addSubvIEw:lineVIEw];    /* progress */    CGPoint progressSlIDerOrigin = CGPointMake(imageSize + labelsWIDth + bodyWrappermargin.x * 3 + 1,bodyWrapperVIEw.frame.size.height / 2 - 15);    CGSize progressSlIDerSize = CGSizeMake(bodyWrapperVIEwSize.wIDth - bodyWrappermargin.x - progressSlIDerOrigin.x,30);    UiSlider *progressSlIDer = [UiSlider vIEwWithFrame:CGEasyRectMake(progressSlIDerOrigin,progressSlIDerSize)];    progressSlIDer.value = [self getcategoryProgress];    [bodyWrapperVIEw addSubvIEw:progressSlIDer];    return wrapperVIEw;}

我希望它看起来像这样:

- (UIVIEw *)tableVIEw:(UItableVIEw *)tableVIEw vIEwForheaderInSection:(NSInteger)section {    if ([tableVIEw.dataSource tableVIEw:tableVIEw numberOfRowsInSection:section] == 0) {        return nil;    }    SectionVIEw *sectionVIEw = ... // get the vIEw that is already designed in the Interface Builder    sectionVIEw.headerText = self.categorIEsnames[section];    sectionVIEw.headerImage = [self getcategoryResourceItem:section + 1][@"image"];    sectionVIEw.firstLabelText = @"first";    sectionVIEw.secondLabelText = @"second";    sectionVIEw.thirdLabelText = @"third";    sectionVIEw.progress = [self getcategoryProgress];    return wrapperVIEw;}

编辑2:我不使用故事板,只是.xib文件.此外,我没有一个UItableVIEwController,只是一个UIVIEwController,其中我添加了一个UItableVIEw.

解决方法 故事板或XIB

>相同的故事板:

return tableVIEw.dequeueReusableCell(withIDentifIEr: "header")


>分离XIB(附加步骤:您必须先注册该Nib):

tableVIEw.register(UINib(nibname: "XIBSectionheader",bundle:nil),forCellReuseIDentifIEr: "xibheader")

要从故事板而不是XIB加载,请参阅this Stack Overflow answer.

使用UItableVIEwCell在IB中创建节标题

利用一个section头是一个普通的UIVIEw,UItableVIEwCell也是一个UIVIEw的事实.在Interface Builder中,拖动&将表视图单元格从对象库中删除到表视图原型内容.

向新添加的表视图单元添加标识符,并根据需要自定义其外观.对于这个例子,我使用了标题.

使用dequeueReusableCell:withIDentifIEr来定位您的段标题,就像任何表视图单元格一样.您将需要提供heightForheaderInSection,为了清楚,硬编码为44

//MARK: UItableVIEwDelegateoverrIDe func tableVIEw(_ tableVIEw: UItableVIEw,vIEwForheaderInSection section: Int) -> UIVIEw?{    // This is where you would change section header content    return tableVIEw.dequeueReusableCell(withIDentifIEr: "header")}overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,heightForheaderInSection section: Int) -> CGfloat{    return 44}

Swift 2&早期:

return tableVIEw.dequeueReusableCellWithIDentifIEr("header") as? UIVIEwself.tableVIEw.registerNib(UINib(nibname: "XIBSectionheader",forCellReuseIDentifIEr: "xibheader")

►在GitHub上查找此解决方案,并在Swift Recipes上查看更多详细信息.

总结

以上是内存溢出为你收集整理的ios – 在Interface Builder中设计UITableView的section标题全部内容,希望文章能够帮你解决ios – 在Interface Builder中设计UITableView的section标题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1111780.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存