2017-09-25 86 views
1

我創建了一個非常簡單的測試用例來重現此問題。UITableView tableFooterView顯示在UITableView的頂部 - 錯誤

我想以編程方式設置頁腳視圖到tableview。請注意,我指的是在tableview的最底部的頁腳 - 而不是頁腳(大多數堆棧溢出的答案讓他們感到困惑)。

這裏是我的代碼非常簡單:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIView *footerContainer = [[UIView alloc] initWithFrame:CGRectZero]; 
    footerContainer.backgroundColor=[UIColor greenColor]; 
    footerContainer.translatesAutoresizingMaskIntoConstraints=NO; 
    [footerContainer addConstraints:@[[NSLayoutConstraint 
             constraintWithItem:footerContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual 
             toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:100 
             ], 
             [NSLayoutConstraint 
             constraintWithItem:footerContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual 
             toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:[UIScreen mainScreen].bounds.size.width 
             ]]]; 

    self.mytableview.tableFooterView=footerContainer; 
    [self.view setNeedsLayout]; 
    [self.view layoutIfNeeded]; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row]; 
    return cell; 
} 

然而,結果是這樣的:

enter image description here

你可能注意到了,頁腳顯示上的tableview的頂部。這是一個錯誤還是我錯過了什麼?

如果我將tableFooterView更改爲tableHeaderView,那麼它工作正常。所以我期待着同樣的腳步也能工作,但事實並非如此。

+0

設置'translatesAutoresizingMaskIntoConstraints = NO'很可能會破壞tableview用於定位頁腳的任何方法。 – dan

+0

您是否使用約束,因爲您將(或可能)在頁腳視圖中具有動態內容?如果是這樣,我可以給你一個解決方案...... – DonMag

+0

@DonMag是的,我一直在尋找一種解決方案,它將使用約束來設置它,而不是手動設置框架。我的實際應用具有動態大小的頁腳內容。 –

回答

0

我已經嘗試了與swift中的顯式幀值相同的操作,並且我實現了您所要求的行爲,如果您認爲它很好,請嘗試使用顯式幀值。如果不需要,則移除佈局約束。

@IBOutlet weak var tableView: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 

    let footerView = UIView(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50)) 
    footerView.backgroundColor = UIColor.green 
    tableView.tableFooterView = footerView 
} 

extension ViewController: UITableViewDelegate, UITableViewDataSource { 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
    cell?.textLabel?.text = "\(indexPath.row)" 
    return cell! 
} 

}

enter image description here

+0

確實對我有效。這是否意味着自動佈局與頁腳視圖的越野車?我應該將它記錄爲Apple的錯誤嗎? –

+0

@PranoyC:很高興它解決了你的問題,並對不起,我沒有使用手動自動佈局代碼的風格,並不能幫助你直接回答你的bug報告的想法,但我會建議,如果你要提出一個問題,嘗試在視圖控制器中設置簡單視圖的自動佈局後引發。 – Bharath

0

動態尺寸UITableView頁眉和頁腳的觀點並不總是玩自動佈局很好,所以你需要給它一點幫助。

下面是一個爲頁腳視圖創建簡單UIView並添加「展開」UILabel(將行數設置爲零)的示例。頁腳視圖是使用顯式CGRect爲其框架創建的,並且標籤以自動佈局約束固定到所有四邊。

viewDidLayoutSubviews()中,我們告訴自動佈局根據其內容約束來計算頁腳視圖的框架,然後我們更新框架值(特別是高度)。

// 
// this assumes IBOutlet has been set for "theTableView" 
// 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // standard stuff 
    [_theTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"simpleCell"]; 
    _theTableView.delegate = self; 
    _theTableView.dataSource = self; 

    // instantiate a view for the table footer 
    // width doesn't matter (it will be stretched to fit the table by default) 
    // set height to a big number to avoid a "will attempt to break constraint" warning 
    UIView *footerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1000)]; 

    // give it a color so we can see it 
    footerContainer.backgroundColor=[UIColor greenColor]; 

    // set the footer view 
    _theTableView.tableFooterView = footerContainer; 


    // instantiate a label to add to the footer view 
    UILabel *aLabel = [UILabel new]; 

    // auto-sizing the height, so set lines to zero 
    aLabel.numberOfLines = 0; 

    // give it a color so we can see it 
    aLabel.backgroundColor = [UIColor yellowColor]; 

    // set the text to 8 lines for demonstration purposes 
    aLabel.text = @"Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8"; 

    // standard, for auto-sizing 
    aLabel.translatesAutoresizingMaskIntoConstraints = NO; 

    // add the label to the footer view 
    [footerContainer addSubview:aLabel]; 

    // constraint the label to 8-pts from each edge... 
    [aLabel.topAnchor constraintEqualToAnchor:footerContainer.topAnchor constant:8.0].active = YES; 
    [aLabel.leftAnchor constraintEqualToAnchor:footerContainer.leftAnchor constant:8.0].active = YES; 
    [aLabel.rightAnchor constraintEqualToAnchor:footerContainer.rightAnchor constant:-8.0].active = YES; 
    [aLabel.bottomAnchor constraintEqualToAnchor:footerContainer.bottomAnchor constant:-8.0].active = YES; 

} 

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 

    // get a reference to the table's footer view 
    UIView *currentFooterView = [_theTableView tableFooterView]; 

    // if it's a valid reference (the table *does* have a footer view) 
    if (currentFooterView) { 

     // tell auto-layout to calculate the size based on the footer view's content 
     CGFloat newHeight = [currentFooterView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 

     // get the current frame of the footer view 
     CGRect currentFrame = currentFooterView.frame; 

     // we only want to do this when necessary (otherwise we risk infinite recursion) 
     // so... if the calculated height is not the same as the current height 
     if (newHeight != currentFrame.size.height) { 
      // use the new (calculated) height 
      currentFrame.size.height = newHeight; 
      currentFooterView.frame = currentFrame; 
     } 

    } 

} 

當嘗試使自動調整表格視圖標題視圖正常工作時,這也會很有幫助。