自定义视图添加SubView布局问题

时间:2017-11-16 05:53:39

标签: ios objective-c uitableview layout

有一个包含2个部分的tableView,每个部分都有自定义部分标题视图

我想要这种效果的UI,并且 0 部分的标题视图正常工作。

what wrong

虽然第1节的标题视图存在问题。

我不知道出了什么问题。

我错过了什么Apple机制,

convert rect/point

logic is wrong

这是代码: 1

部分的自定义 视图
@interface ZBWithDrawHeaderView()
     @property (nonatomic, strong ) UIView *renderView;
 @end
@implementation ZBWithDrawHeaderView

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor grayColor ];

        _renderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, -17)];
       //it works OK, I don't know the logic.

      //  _renderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 17)];
      //it works Wrong ,what mechanism do I miss

        [self addSubview: _renderView];
        _renderView.backgroundColor = [UIColor redColor ];
    }
    return self;
}
@end

我在哪里投入使用?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   if(section == 0){
         ......
         return view;
   }
    else if (section == 1) {
        ZBWithDrawHeaderView * secondHeaderView = [[ZBWithDrawHeaderView alloc] initWithFrame: CGRectMake(0, 0, KScreenWidth, 25)];
        return secondHeaderView;
    }
    return nil;
 }

1 个答案:

答案 0 :(得分:0)

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
     return [UIView new ];
}

添加了两个UITableViewDelegate方法,没有发生奇怪的布局。看起来像章节页眉和页脚必须成对出现。

相关问题