简体   繁体   English

故事板上的两个具有不同标识符和不同高度的UITableViewCell

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

I have two type of cells that I'll be using my UITableView, so I created two prototype cell with different identifier. 我有两种类型的单元格将要使用UITableView,因此我创建了两个具有不同标识符的原型单元格。 I manuelly changed the size but when I compile and run, the two cells have same size. 我手动更改了大小,但是在编译和运行时,两个单元格的大小相同。

Is there any way to do it through storyboard and without checking every single time 有没有办法通过情节提要而无需每次都检查

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

I will have around a good 100 cells at least. 我至少会有大约100个单元。

UPDATE : in my case I have a tableview with 3 sections, the first one is small, the second and the third one are bigger. 更新 :就我而言,我有一个包含3个部分的表视图,第一个部分较小,第二个和第三个较大。

I think your best option is to use that -tableView:heightForRowAtIndexPath: delegate method you listed and return the height you would like. 我认为最好的选择是使用列出的-tableView:heightForRowAtIndexPath:委托方法并返回所需的高度。

You could assign tags to each prototype and use an if/else if conditional. 您可以为每个原型分配标签,并在条件条件下使用if / else。

Or if you have a subclassed UITableViewCell for each prototype you could do something like 或者,如果每个原型都有一个子类UITableViewCell,则可以执行以下操作

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

If you know that the sections if what determines the height of the cell, then you can implement it like this: 如果您知道这些部分决定了单元的高度,那么您可以像这样实现它:

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

Depending on the heights that you need of course. 当然,取决于您所需的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重新加载具有不同高度和内容的现有UITableViewCell - reload existing UITableViewCell with different height and content 在不同的iPhone屏幕中保持UITableViewCell高度 - Keep UITableViewCell height in different IPhone screen UITableViewCell内部的自动布局-基于两个不同元素的可变大小的动态高度 - Auto Layout inside UITableViewCell - dynamic height based on variable size of two different elements 两个应用程序在Xcode上具有共同的故事板,但它们是不同的 - Two application with a common storyboard on Xcode but they are different Storyboard-在故事板上为同一ViewController创建两个不同的视图 - Storyboard - Creating two different Views in storyboard for the same ViewController 具有可变高度单元的UITableViewCell重用标识符 - UITableViewCell Reuse Identifier with Variable Height Cells 将两个不同的storyboard viewcontrollers连接到同一个类 - hooking up two different storyboard viewcontrollers to a same class 如何在不使用StoryBoard的情况下对齐两个不同UILabel的基线 - How to align baseline of two different UILabel without using StoryBoard iPhone 6的不同故事板 - Different storyboard for iPhone 6 适用于iPhone 7和7 Plus的不同故事板 - Different storyboard for iPhone 7 and 7 plus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM