1

爲了在視圖和設備之間獲得一致的大小比例,我在視圖和超級視圖之間制定了相同的寬度/高度約束。但是,在設計UITableViewCell或UICollectionViewCell時,我無法在界面構建器中找到此選項。我需要設置單元格內的視圖和超級視圖之間的比率。完成此行爲的最簡潔方法是什麼?ios uitableviewcell項目與superview故事板界面生成器的高度相等

回答

0

如果你想使你的UITableViewCell大小作爲其表上海華一樣,你應該這樣做:

1)設置約束UITableView等於寬度/高度,以上海華,也設置它的起源(例如在superview中心x/y)。

2)返回尺寸電池:

情況下UITableViewCell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return tableView.frame.size.height; 
} 

情況下UICollectionViewCell

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    return collectionView.frame.size; 
} 
+0

對不起,我有點不清楚,我不需要的細胞是上海華盈的大小,我需要裏面的一個視圖單元格是超視圖的大小。我已經更新了我的問題以更具體。 – Siamaster

+0

@Siamaster,superview的tableView? – rkyr

+0

是的,還是tableview,只是不與細胞本身成比例。 – Siamaster

1

你不能做到這一點的故事板和必須做的代碼:對於UITable查看

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return tableView.frame.size.width; 
} 

對於UICollectionView

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int columns = 2; // Change this to whatever you want 
    float width = collectionView.frame.size.width/columns; 
    return CGSizeMake(width, width); 
}