簡體   English   中英

如何使用大型列表調整UITableView的大小以適應所有內容而無需滾動?

[英]How to resize UITableView with a large list to fit all the content without scrolling?

我有一個UITableView,里面至少有60個單元格,需要適合而不是滾動。 我怎樣才能讓它適合? 我已經嘗試過了:

CGRect frame = self.tableView.frame; 
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;

正如這個答案說, 在這里 ,但在評論中提到,它不會有非常大名單的工作,你可以告訴,因為反彈將揭示更多的細胞。 沒有為此提供解決方案。 我該如何解決這個問題?

顯而易見的解決方案是不使用UITableView。 您可以以編程方式或在xib中將普通視圖創建為contentView,然后將其添加到主視圖中,並使用約束將其連接到以前的自定義視圖。 我已經多次完成它,它就像一個魅力。

var lastView: UIView? = nil

for item in model.items {

    var customView: CustomContentView

    let objects = NSBundle.mainBundle().loadNibNamed("CustomContentView", owner: self, options: nil)
    customView = objects.first! as! CustomContentView

    customView.translatesAutoresizingMaskIntoConstraints = false

    customView.setItemModel(item)

    viewWrap.addSubview(customView)

    viewWrap.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[customView]|", options: [], metrics: nil, views: ["customView": customView]))

    if lastView == nil {
        viewWrap.addConstraint(NSLayoutConstraint(item: customView, attribute: .Top, relatedBy: .Equal, toItem: viewWrap, attribute: .Top, multiplier: 1, constant: 0))
    } else {
        viewWrap.addConstraint(NSLayoutConstraint(item: customView, attribute: .Top, relatedBy: .Equal, toItem: lastView!, attribute: .Bottom, multiplier: 1, constant: 0))
    }

    lastView = customView
}

if lastView != nil {
    viewWrap.addConstraint(NSLayoutConstraint(item: viewWrap, attribute: .Bottom, relatedBy: .Equal, toItem: lastView!, attribute: .Bottom, multiplier: 1, constant: 17))
}

然后,您只需確保自定義視圖可以調整大小以適應屏幕,如果它們太多。 我假設你有一些最大限制,他們都可以適應。 如果沒有,那么您需要將其添加到滾動視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM