Почему эти ограничения в представлении заголовка таблицы неоднозначны? - PullRequest
0 голосов
/ 04 декабря 2018

У меня есть «tableHeaderView», который является UIView.Он содержит UIImageView, высота которого установлена ​​на 200:

let tableView: UITableView = .init(frame: .zero)
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)

let tableHeaderView: UIView = .init(frame: .zero)
tableHeaderView.translatesAutoresizingMaskIntoConstraints = false

let imageView: UIImageView = .init(frame: .zero)
imageView.translatesAutoresizingMaskIntoConstraints = false
tableHeaderView.addSubview(imageView)

tableView.tableHeaderView = tableHeaderView

var constraints: [NSLayoutConstraint] = []

// Set imageView's leading and trailing to tableHeaderView's leading and trailing
constraints += NSLayoutConstraint.constraints(
    withVisualFormat: "H:|-0-[imageView]-0-|",
    options: [],
    metrics: nil,
    views: ["imageView": imageView]
)

// Set imageView's top and bottom to tableHeaderView's top and bottom
// Also, set imageView's height to 200
constraints += NSLayoutConstraint.constraints(
    withVisualFormat: "V:|-0-[imageView(200)]-0|",
    options: [],
    metrics: nil,
    views: ["imageView": imageView]
)

// Set tableHeaderView's width to tableView's width
constraints.append(
    NSLayoutConstraint.init(
        item: tableHeaderView,
        attribute: .width,
        relatedBy: .equal,
        toItem: tableView,
        attribute: .width,
        multiplier: 1,
        constant: 0
    )
)

// Set tableView's leading and trailing to view's leading and trailing
constraints += NSLayoutConstraint.constraints(
    withVisualFormat: "H:|-0-[tableView]-0-|",
    options: [],
    metrics: nil,
    views: ["tableView": tableView]
)

// Set tableView's top and bottom to view's top and bottom
constraints += NSLayoutConstraint.constraints(
    withVisualFormat: "V:|-0-[tableView]-0-|",
    options: [],
    metrics: nil,
    views: ["tableView": tableView]
)

NSLayoutConstraint.activate(constraints)

Эти ограничения дают мне такой результат:

enter image description here

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

enter image description here

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

Ответы [ 2 ]

0 голосов
/ 04 декабря 2018

UITableView должен управлять макетом заголовка для вас, поэтому вам просто нужно установить высоту представления контейнера (в вашем случае tableHeaderView) и добавить / расположить его подпредставления в соответствии с вашими потребностями.Почему бы не попробовать это и посмотреть, работает ли оно:

    let tableHeaderView = UIView(frame: CGRect(origin: CGPoint(),
                                     size: CGSize(width: 0, height: someHeightConstant)) )
    let imageView = UIImageView(frame: .zero)

    tableHeaderView.addSubview(imageView)
    //now you should set constraints on the imageview (omitting the code for readability)

    tableView.tableHeaderView = tableHeaderView

Если вам нужен заголовок для наложения ячеек при прокрутке пользователя, рассмотрите возможность использования заголовка раздела.Надеюсь, это поможет, и я правильно понял ваш вопрос.

0 голосов
/ 04 декабря 2018

Вот код Objective c, с помощью которого вы можете понять, что будет неправильно.

  • Ограничение Tableview должно быть с self.view
  • Ограничение заголовка должно быть с вашим таблицей Viewview
  • Добавьте ваше последнее ограничение в self.view

Я только что создал пример класса для цели c, не имеет значения для swift, просто сравните логику.

#import "ViewController.h"

@implementation ViewController

@synthesize tableview;
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];

NSLayoutConstraint *leftconstraint = [NSLayoutConstraint
                                      constraintWithItem:self.tableview
                                      attribute:NSLayoutAttributeLeft
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeLeft
                                      multiplier:1.0
                                      constant:0.0];
leftconstraint.active = YES;

NSLayoutConstraint *rightconstraint = [NSLayoutConstraint
                                       constraintWithItem:self.tableview
                                       attribute:NSLayoutAttributeRight
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:self.view
                                       attribute:NSLayoutAttributeRight
                                       multiplier:1.0
                                       constant:0.0];
rightconstraint.active = YES;

NSLayoutConstraint *topconstraint = [NSLayoutConstraint
                                     constraintWithItem:self.tableview
                                     attribute:NSLayoutAttributeTop
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:self.view
                                     attribute:NSLayoutAttributeTop
                                     multiplier:1.0
                                     constant:0.0];
topconstraint.active = YES;

NSLayoutConstraint *bottomconstraint = [NSLayoutConstraint
                                        constraintWithItem:self.tableview
                                        attribute:NSLayoutAttributeBottom
                                        relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                        attribute:NSLayoutAttributeBottom
                                        multiplier:1.0
                                        constant:0.0];
bottomconstraint.active = YES;

[self.view addConstraints:@[leftconstraint, rightconstraint, topconstraint, bottomconstraint]];

// header view constraint

NSLayoutConstraint *left = [NSLayoutConstraint
                            constraintWithItem:headeerView
                            attribute:NSLayoutAttributeLeft
                            relatedBy:NSLayoutRelationEqual
                            toItem:self.tableview
                            attribute:NSLayoutAttributeLeft
                            multiplier:1.0
                            constant:0.0];
left.active = YES;


NSLayoutConstraint *right = [NSLayoutConstraint
                            constraintWithItem:headeerView
                            attribute:NSLayoutAttributeRight
                            relatedBy:NSLayoutRelationEqual
                            toItem:self.tableview
                            attribute:NSLayoutAttributeRight
                            multiplier:1.0
                            constant:0.0];
right.active = YES;

NSLayoutConstraint *top = [NSLayoutConstraint
                             constraintWithItem:headeerView
                             attribute:NSLayoutAttributeTop
                             relatedBy:NSLayoutRelationEqual
                             toItem:self.tableview
                             attribute:NSLayoutAttributeTop
                             multiplier:1.0
                             constant:0.0];
top.active = YES;

NSLayoutConstraint *width = [NSLayoutConstraint
                           constraintWithItem:headeerView
                           attribute:NSLayoutAttributeWidth
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.tableview
                           attribute:NSLayoutAttributeWidth
                           multiplier:1.0
                           constant:0.0];
width.active = YES;

[self.view addConstraints: @[left,right,top,width]];

}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];



self.tableview = [[UITableView alloc] initWithFrame: CGRectZero style:UITableViewStyleGrouped];
self.tableview.delegate=self;
self.tableview.dataSource=self;

[self.tableview setTranslatesAutoresizingMaskIntoConstraints:NO];

UIImage *img = [[UIImage alloc] init];
img = [UIImage imageNamed:@"header.png"];

headeerView = [[UIImageView alloc] initWithImage:img];
[headeerView setTranslatesAutoresizingMaskIntoConstraints:NO];

self.tableview.tableHeaderView = headeerView;

[self.view addSubview:self.tableview];

}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableviewcell"];
if (cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableviewcell"];

return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}


@end

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...