我發現樣本的靜態高度不是我想要使用的,因爲我有一些單元格中包含大量需要爲每個單元格設置變量高度的文本。因此,我使用UITableViewAutomaticDimension來自動調整所有單元格的高度,而不是樣例中從預定義的故事板大小抓取拾取器單元格高度的方法。如果你不希望自動高度,它仍然很容易適應以下解決方案的高度設置爲靜態值,正如我在第3步
設置的tableview行的高度提自動當視圖負載:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tableView.estimatedRowHeight = 60.0; // Set your average height here.
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
添加預測的高度,使其正常工作(至少在iOS版8)。如果你不添加這個,當它繪製單元高度時,你可能會得到一些錯誤行爲。
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
對於汽車高度,記住,如果你有一個刪除tableView:heightForRowAtIndexPath:
定義,因爲它可能與自動設置干擾。
// Don't define this at all if you want the picker cell to have auto height.
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{ }
如果你想使用一個靜態高度爲拾取單元格,然後定義tableView:heightForRowAtIndexPath:
功能,只是返回靜態值,而不是在這裏一樣樣做它使用自動尺寸。例子是這樣的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// This would return the static height for the picker, but auto heights for the rest of the cells.
return ([self indexPathHasPicker:indexPath] ? self.pickerCellRowHeight : UITableViewCellAutoDimension);
}
然後你會修改示例設置您的靜態高度,而不是在viewDidLoad
從幀值抓住它的。
// obtain the picker view cell's height, works because the cell was pre-defined in our storyboard
//UITableViewCell *pickerViewCellToCheck = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerID];
self.pickerCellRowHeight = 216;//CGRectGetHeight(pickerViewCellToCheck.frame);
而不是試圖重新從樣品相同的約束,我告訴故事板復位到建議約束的UIPickerView,然後增加了一個。它添加了圖片中的前兩個,然後我再添加一個來對齊中心X.我的視圖碰巧有自動設置中顯示的數字,但是您的視圖可能會有所不同。
![Storyboard of cell and constraints](https://i.stack.imgur.com/2qAl9.png)
![Reset to suggested constraints](https://i.stack.imgur.com/XjmNf.png)
在你還沒有做過的情況下,使機械手工作的一部分,以確保數據源和委託的UIPickerView設置爲視圖控制器用故事板並控制點擊和拖動。
![enter image description here](https://i.stack.imgur.com/iwqn2.png)
什麼問題?目前還不清楚你想要完成什麼或者什麼不工作。 – Mundi
基本上,我在問什麼步驟重現樣本中日期選擇器的相同約束和維度。 我在第二段中描述的(在鏈接之後)基本上是我遇到的問題。在示例項目中,我看到日期選擇器被限制爲高度爲216的表視圖單元格。我不知道如何實現此目的。我只能將日期選擇器約束到單元格的內容視圖,這不是樣例故事板中的內容視圖,並且不會產生相同的結果。 – ryanthon
您是否嘗試連接新項目?您是否嘗試刪除連接並重新插入連接? – Mundi