簡體   English   中英

對於不同的尺寸類別,是否可以具有不同的表格視圖單元格行高?

[英]Is it possible to have different table view cell row heights for different size classes?

我正在開發具有自適應布局的應用程序,到目前為止,所有工作都可以進行,除了我需要具有自適應表格視圖單元格高度。 我正在使用情節提要,看不到有任何方法可以做到這一點。 是不可能完成任務嗎?

如果我正確理解了您的問題,則希望使用大小類將單元格高度調整為不同的字體大小。 因此在較大尺寸的屏幕上使用較大的字體會導致單元格相應地進行調整。 我認為唯一的方法是為特定的大小類安裝不同的文本視圖。

我制作了一個測試應用程序來演示這一點。 在情節提要中,我添加了帶有自定義單元格的表視圖控制器。 我將大小類更改為wCompact hAny ,並向該單元格添加了文本視圖(IBOutlet:labelPhone)。 我將其固定到單元格的所有4側,禁用了滾動,使文本變為紅色,並將字體的大小調整為8。我將size類更改為wRegular hAny ,並使用相同的約束條件,字體大小為30的綠色文本。表視圖控制器中的代碼如下,

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 50;
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated]; 
    [self.tableView reloadData]; // this was necessary to have the cells size correctly when the table view first appeared
}


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *theText = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt";

    cell.labelPhone.text = theText;
    cell.labelPad.text = theText;
    return cell;
}

在iPhone模擬器上運行時,我得到了紅色的小文本,在iPad模擬器上運行時,我得到了綠色的大文本。 我將示例應用程序放在這里, http://jmp.sh/WfeZ8Pi

編輯后:

實際上,我找到了一種更簡單的方法。 您可以使用默認的wAny hAny size類添加子視圖(文本視圖或標簽)(您不需要兩個不同的視圖)。 在“屬性”檢查器的“字體”字段旁邊,有一個小的“ +”按鈕。 單擊該按鈕,然后可以為不同的字體大小添加不同的字體大小。 這就是您需要做的。 然后cellForRowAtIndexPath變成這樣,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textView.text = self.theData[indexPath.row];
        return cell;
    }

暫無
暫無

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

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