2016-11-21 31 views
0

我正在與AFCollectionView在一個表中工作,它不是像我所期望的那樣呈現。爲了調試問題,我創建了具有所需佈局的另一個自定義表格單元格,並將其放入AFCollectionView單元格中。集合視圖單元格仍然無法正確顯示。什麼原因導致具有相同自動佈局約束的兩個UITableViewCell行爲不同?

這兩個單元格都是沒有xib或storyboard構建的,所以我努力嘗試調試自動佈局控制檯問題。我知道UICollectionView比UIView更復雜,但我不確定在顯示問題的情況下甚至可以開始嘗試進行更改。

enter image description here

兩種細胞具有從- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法的內側稱爲以下updateConstraints方法。

[self updateConstraints]; 
    return self; 
} 

- (void)updateConstraints{ 
    [super updateConstraints]; 

    //Create local versions of the ivars 
    UILabel *titleLabelP = self.titleLabel; 
    UIView *bottomViewP = self.collectionView; 
    NSNumber *bottomViewHeightP = [NSNumber numberWithInteger:self.bottomViewHeight]; 


    //Build the visual constraints 
    NSDictionary *views = NSDictionaryOfVariableBindings(titleLabelP, bottomViewP); 
    NSDictionary *metrics = @{ @"padding" : @8.0, @"viewHeight": bottomViewHeightP }; 

    // title and bottom view fill the width of the superview (cell content view) 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padding-[titleLabelP]-padding-|" options:0 metrics:metrics views:views]]; 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padding-[bottomViewP]-padding-|" options:0 metrics:metrics views:views]]; 
    // title and bottom view are setup vertically with 8px of padding between. The cell should expand to fit the full size of the bottom view. 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-padding-[titleLabelP]-padding-[bottomViewP(viewHeight)]-padding-|" options:0 metrics:metrics views:views]]; 

} 
自動版式

控制檯登錄問題:

2016-11-21 14:19:06.423003 AutoLayoutTableCellTest[71645:4731377] [Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead. 
2016-11-21 14:19:06.425136 AutoLayoutTableCellTest[71645:4731377] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x608000096710 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.midY == 22.25 (active)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x6080000966c0 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.height == 44.5 (active)>", 
    "<NSLayoutConstraint:0x608000095860 V:|-(8)-[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1'] (active, names: '|':AFTableViewCell:0x7fb87b050400'CellIdentifier')>", 
    "<NSLayoutConstraint:0x608000095900 AFIndexedCollectionView:0x7fb87b051000.height == 150 (active)>", 
    "<NSLayoutConstraint:0x608000095950 V:[AFIndexedCollectionView:0x7fb87b051000]-(8)-| (active, names: '|':UITableViewCellContentView:0x7fb87ac1a770)>", 
    "<NSLayoutConstraint:0x608000095fe0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000095180 h=-&- v=--& 'UIView-Encapsulated-Layout-Top' AFTableViewCell:0x7fb87b050400'CellIdentifier'.minY == 0 (active, names: '|':UITableViewWrapperView:0x7fb87b03b800)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000095fe0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2016-11-21 14:19:06.425934 AutoLayoutTableCellTest[71645:4731377] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x608000096710 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.midY == 22.25 (active)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x6080000966c0 h=--& v=--& UITableViewCellContentView:0x7fb87ac1a770.height == 44.5 (active)>", 
    "<NSLayoutConstraint:0x608000095860 V:|-(8)-[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1'] (active, names: '|':AFTableViewCell:0x7fb87b050400'CellIdentifier')>", 
    "<NSLayoutConstraint:0x6080000958b0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)>", 
    "<NSLayoutConstraint:0x608000095900 AFIndexedCollectionView:0x7fb87b051000.height == 150 (active)>", 
    "<NSLayoutConstraint:0x608000095950 V:[AFIndexedCollectionView:0x7fb87b051000]-(8)-| (active, names: '|':UITableViewCellContentView:0x7fb87ac1a770)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000095180 h=-&- v=--& 'UIView-Encapsulated-Layout-Top' AFTableViewCell:0x7fb87b050400'CellIdentifier'.minY == 0 (active, names: '|':UITableViewWrapperView:0x7fb87b03b800)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6080000958b0 V:[UILabel:0x7fb87ac1b670'CURRENT INDEXPATH ROW: 1']-(8)-[AFIndexedCollectionView:0x7fb87b051000] (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

我已經把樣本項目在GitHub上@https://github.com/propstm/AutoLayoutTableCellTest

編輯以顯示INIT FOR CELL WITH的CollectionView:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil; 

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 9, 10); 
    layout.itemSize = CGSizeMake(100, 100); 

    layout.scrollDirection = UICollectionViewScrollDirectionVertical; 
    self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier]; 
    [self.collectionView setTranslatesAutoresizingMaskIntoConstraints:FALSE]; 
    self.collectionView.backgroundColor = [UIColor whiteColor]; 
    self.collectionView.showsHorizontalScrollIndicator = NO; 
    [self.contentView addSubview:self.collectionView]; 
    if(!self.titleLabel){ 
     self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 8, self.frame.size.width-8, 20)]; 
     [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:FALSE]; 
     UIFont *boldFont = [UIFont fontWithName:@".SFUIText-Bold" size:16.0f]; 
     [self.titleLabel setFont:boldFont]; 
     [self addSubview:self.titleLabel]; 
    } 

    self.bottomViewHeight = 150; 

    [self updateConstraints]; 
    return self; 
} 

回答

0

每當您創建視圖並以編程方式設置自動佈局約束時,您必須在您添加約束的視圖上設置translatesAutoresizingMaskIntoConstraints = false。如果不是,則自動調整掩碼將轉換爲自動佈局約束。

在這種情況下,你需要添加類似:

self.titleLabel.translateAutoresizingMaskIntoConstraints = false 
self.collectionView.translateAutoresizingMaskIntoConstraints = false 

新增

根據日誌您添加的的CollectionView的身高是150,但內容查看的身高比小。你必須返回正確的高度UITableViewCell

+0

我曾經在單元格的init方法的上面那部分。我也已將該代碼添加到帖子中。我試圖用FALSE替換NO,仍然沒有解決。 – propstm

+0

我使用估計的行高。 titleLabel的高度爲20,collectionView的高度爲150,所以對於我的垂直約束,我期望自動調整的高度約爲200. – propstm

+0

您也有填充。返回所有高度和填充的總和的確切高度 – Ryan

1

我能得到這些自動佈局警告有以下改動走開:

  • 在你AFTableViewCell-initWithStyle:reuseIdentifier:方法,返回之前,設置的translatesAutoresizingMaskIntoConstraints財產細胞的contentViewNO
  • 將標題標籤添加到單元格的內容視圖中,而不是單元格本身。

這會導致出現新的警告:

[Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead. 

然而,佈局似乎更接近你的原意。

相關問題