[英]Dynamic Table View Cell & Content Size conflicting & Pintrest Layout
目前,根據我的需求,我想開發一個Pintrest布局視圖。 為此, 我在同一個ScrollView上有2個tableView 。
標記:我沒有使用collectionView,因為為此很難自定義流程布局,我也不想為此包含3rd Party框架。
查看隨附的屏幕截圖-
我用2個數組填充它們,一個數組用於偶數,一個數組用於奇數項 。 我正在使這些tableView不可滾動並根據最高的tableView增加我的scrollView的contentView的高度 。 兩個tableView都有自定義單元格,這些單元格具有動態增加的內容(即標簽)。
在我的viewDidLoad()中
self.tableViewCol1.estimatedRowHeight = 296
self.tableViewCol1.rowHeight = UITableViewAutomaticDimension
self.tableViewCol1.separatorStyle = UITableViewCellSeparatorStyle.None
self.tableViewCol2.estimatedRowHeight = 296
self.tableViewCol2.rowHeight = UITableViewAutomaticDimension
self.tableViewCol2.separatorStyle = UITableViewCellSeparatorStyle.None
在我的DataSource方法中-
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell2 = MostLikedTVCscnd()//custom Cell for 2nd Table
var cell1 = MostLikedTVC()//custom Cell for 1st Table
if tableView == tableViewCol1
{
let cell = tableViewCol1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! MostLikedTVC
cell.imageCol1.image = imageArr1[indexPath.row] as? UIImage
cell.aboutLblCol1.text = labelArr1[indexPath.row] as? String//dynamic increasing label
cell1=cell
}
else if tableView == tableViewCol2
{
let cell = tableViewCol2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! MostLikedTVCscnd
cell.imageCol2.image = imageArr2[indexPath.row] as? UIImage
cell.aboutLblCol2.text = labelArr2[indexPath.row] as? String
cell2 = cell
}
//changing the height constraint of table's to make the table view non scrollable
tableView1HghtCnstrnt.constant = tableViewCol1.contentSize.height
tableView2HghtCnstrnt.constant = tableViewCol2.contentSize.height
//comparing the content size of table's to check which table is the tallest & adjust the height of the main ScrollView's content
if tableViewCol1.contentSize.height>tableViewCol2.contentSize.height
{
mainViewHghtCnstrnt.constant = tableViewCol1.contentSize.height+35//mainViewHghtCnstrnt :- mainScrollView's content height constraint & 35 is the padding
}
else if tableViewCol1.contentSize.height<tableViewCol2.contentSize.height
{
mainViewHghtCnstrnt.constant = tableViewCol2.contentSize.height+35
}
else if tableViewCol1.contentSize.height==tableViewCol2.contentSize.height
{
mainViewHghtCnstrnt.constant = tableViewCol2.contentSize.height+35
}
//returning the cell
if tableView == tableViewCol1
{
return cell1
}
else
{
return cell2
}
}
}
但是我的問題是表格沒有正確計算其內容的大小。 我進行了一些搜索,這里的一個問題回答是- 當您提供估計的RowHeight時,contentSize將被弄亂
那我有什么選擇呢? 如何才能正確實現相同目標?
您可以使用tableView的contentSize屬性來獲取tableView所需的高度。
我已經做了一個演示來測試這一點,並且它正在按您的期望工作。
我的約束如下:
- ScrollView:
LeadingSpace到SuperView,TrailingSpace到SuperView,TopSpace到SuperView,BottomSpace到SuperView
- ContainerView(scrollView內的UIView):
LeadingSpace到SuperView,TrailingSpace到SuperView,TopSpace到SuperView,BottomSpace到SuperView,EqualWidth到SuperView(即scrollView)
- ATableView
LeadingSpace到SuperView,TrailingSpace到BTableView,TopSpace到SuperView,寬度固定點,HeightFixed點(此約束的已創建出口),BottomSpace到具有StandardSpacing和LowPriority的SuperView(250個)
- BTableView
LeadingSpace到ATableView,TrailingSpace到SuperView,TopSpace到SuperView,寬度固定點,HeightFixed點(此約束的已創建出口),BottomSpace到具有StandardSpacing和LowPriority的SuperView(250)
這是我的完整代碼,
class ViewController: UIViewController {
@IBOutlet weak var tableViewA: UITableView!
@IBOutlet weak var tableViewB: UITableView!
@IBOutlet weak var heightConstraintBTableView: NSLayoutConstraint!
@IBOutlet weak var heightConstraintATableView: NSLayoutConstraint!
var tableViewACellCount = 50
var tableViewBCellCound = 20
override func viewDidLoad() {
super.viewDidLoad()
//Dont set estimatedRowHeight as it is displaying empty cell at the bottom of tableView (try playing with this uncomment estimatedRowHeight)
//tableViewA.estimatedRowHeight = 44.0
tableViewA.rowHeight = UITableViewAutomaticDimension
//tableViewB.estimatedRowHeight = 44.0
tableViewB.rowHeight = UITableViewAutomaticDimension
//Try both methods
self.performSelector("adjustTableViewHeight", withObject: [], afterDelay: 0.0)
//self.adjustTableViewHeight()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if(tableView.isEqual(tableViewA)) { //A TableView
return tableViewACellCount
} else { //B TableView
return tableViewBCellCound
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell")
return cell!
}
func adjustTableViewHeight() {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.tableViewA.layoutIfNeeded()
self.tableViewB.layoutIfNeeded()
}
heightConstraintATableView.constant = tableViewA.contentSize.height
heightConstraintBTableView.constant = tableViewB.contentSize.height
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.