根据它使用的UITableViewCell的类型更改heightForRowAtIndexPath?

时间:2013-02-20 11:36:04

标签: ios objective-c uitableview

cellForRowAtIndexPath中,我使用随机化来创建两种不同的自定义UITableViewCell类型中的一种,我们称之为LCImageCellLCTextCell(一个包含一个图像,一个包含一些文本,它是随机的,将显示在每一行中)。这基本上是:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    // Determine whether the cell should contain an image or text..
    BOOL isCellAnImage;
    int randomChanceOfImageAppearing = arc4random() % 5;
    if (randomChanceOfImageAppearing == 4) isCellAnImage = YES;
    else isCellAnImage = NO;

    // If the cell is going to contain an image..
    if (isCellAnImage) {
        LCIImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier: @"ImageCell"];
        if (imageCell == nil) {
            imageCell = [[LCImageCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"ImageCell"];
        }

        return imageCell;

    // Else the cell will contain text..
    } else {
        // Make and allocate the cell if necessary.
        LCTextCell *customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
        if (customCell == nil) {
            customCell = [[LCTextCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
        }
        return customCell;
    }
}

我需要动态设置带文本的那些(LCTextCell个实例)的高度,这样才能正常工作。我现在要集成图像单元格,我想知道如何让heightForRowAtIndexPath知道有问题的单元格是LCImageCell还是LCTextCell,这样我才能如果相关单元格为LCTextCell,则仅应用该高度调整。

我可以在设置高度之前访问要应用高度的单元格吗?它是否已经在那个时间点创建/分配/初始化了?

3 个答案:

答案 0 :(得分:6)

看似合乎逻辑的事情是从-cellForRowAtIndexPath:内调用-tableView:heightForRowAtIndexPath:。然而,这是一种糟糕的形式(后者在前者之前被称为有充分理由)并且可能导致性能问题(表中的每个单元都会调用heightForRowAtIndexPath,即使是非每次显示表格时都可见。

相反,将单元格随机化移至viewController生命周期的早期阶段。例如,假设您事先知道每个单元格类型的高度(并且您的tableView只有一个部分):

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < [self.tableView numberOfRowsInSection:0]; i++) {
        if (arc4random() % 5 == 4) {
            [mutableArray addObject:[LCImageCell class]];
        } else {
            [mutableArray addObject:[LCImageCell class]];
        }
    }

    self.cellTypes = [NSArray arrayWithArray:mutableArray];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.cellTypes objectAtIndex:indexPath.row] isEqual:[LCImageCell class]]) {
        [LCImageCell height];  // class method returns static height for an image cell
    } else {
        [LCTextCell height];   // class method returns static height for a text cell
    };
}

如果高度是动态的,您应该calculate and store that in advance as well

答案 1 :(得分:1)

为LCTextCell创建一个类(比如说A),为LCImageCell创建另一个类(比如说B)。您可以定义类来保存特定于单元格的信息(您可以在类A中定义一个NSString实例,它可以是在LCTextCell中显示的字符串,类似地,您可以在类B中有一个url实例来表示显示的图像在LCImageCell里面)。您必须在viewcontroller中保存一个数组(比如myArray),该数组将包含A或B的实例,具体取决于您在特定索引(tableview行)上需要​​的单元格。在调用heightForRowAtIndexPath方法之前,您必须确保数组具有正确的条目。

然后你可以

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    if([[self.myArray objectAtIndex:row] isKindOfClass:[A class]])
    {
        //LCTextCell
        return ...;
    }
        else if([[self.myArray objectAtIndex:row] isKindOfClass:[B class]])
    {
        //LCImageCell
        return ...;
    }
    return 50.0f;
}

答案 2 :(得分:-1)

您可以这样做:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *customCell = [self.tableView cellForRowAtIndexPath:indexPath];
    if([customCell isMemberOfClass:[LCImageCell class])
    {
        return ...;
    }
        else if([customCell isMemberOfClass:[LCTextCell class])
    {
        return ...;
    }
    return 0.0f;
}