簡體   English   中英

故事板上的兩個具有不同標識符和不同高度的UITableViewCell

[英]Two UITableViewCell with different identifier and different height in storyboard

我有兩種類型的單元格將要使用UITableView,因此我創建了兩個具有不同標識符的原型單元格。 我手動更改了大小,但是在編譯和運行時,兩個單元格的大小相同。

有沒有辦法通過情節提要而無需每次都檢查

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

我至少會有大約100個單元。

更新 :就我而言,我有一個包含3個部分的表視圖,第一個部分較小,第二個和第三個較大。

我認為最好的選擇是使用列出的-tableView:heightForRowAtIndexPath:委托方法並返回所需的高度。

您可以為每個原型分配標簽,並在條件條件下使用if / else。

或者,如果每個原型都有一個子類UITableViewCell,則可以執行以下操作

...
id cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[CustomCell1 class]]) {
    // return aNumber;
} else if ([cell isKindOfClass:[CustomCell2 class]]) {
    // return aNumber;
} else {
    return aNumber;
}
...

如果您知道這些部分決定了單元的高度,那么您可以像這樣實現它:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath {
    CGFloat result;
    if (indexPath.section == 0) {
        result = 160;
    } else {
        result = 80;
    }
    return result;
}

當然,取決於您所需的高度。

暫無
暫無

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

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