ios - 扩展的 UITableviewCell 高度不可管理

标签 ios objective-c xcode uitableview uicollectionviewcell

我正在使用 this在我的应用程序中,我为 UITableview 创建了两个标识符,一个是“正常单元格”,另一个是“扩展单元格”,当有人点击“正常单元格”时它会显示。我的问题是我无法分别管理两个单元格的高度,例如我希望正常单元格高度为 120,扩展单元格为 60。但是如果我更改 heightForRowAtIndexPath 中的高度,则两个单元格显示相同的高度。这是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FSParallaxTableViewCell *cell = nil;
    static NSString *cellIdentifier = nil;
    if ([indexPath isEqual:self.expandedIndexPath]) {
        cellIdentifier = @"ExpandedCell";
    }
    else {
        cellIdentifier = @"NormalCell";
    }
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[FSParallaxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCell"]) {
        cell.contentView.backgroundColor = [UIColor grayColor];
    }
    if ([[cell reuseIdentifier] isEqualToString:@"NormalCell"]) {
        [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]]
                              placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
        cell.clipsToBounds = YES;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // disable touch on expanded cell
    UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPath];
    if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCell"]) {
        return;
    }
    // deselect row
    [tableView deselectRowAtIndexPath:indexPath
                             animated:NO];
    // get the actual index path
    indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
    // save the expanded cell to delete it later
    NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
    // same row tapped twice - get rid of the expanded cell
    if ([indexPath isEqual:self.expandingIndexPath]) {
        self.expandingIndexPath = nil;
        self.expandedIndexPath = nil;
    }
    // add the expanded cell
    else {
        self.expandingIndexPath = indexPath;
        self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
                                                    inSection:[indexPath section]];
    }
    [tableView beginUpdates];
    if (theExpandedIndexPath) {
        [theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
                            withRowAnimation:UITableViewRowAnimationNone];
    }
    if (self.expandedIndexPath) {
        [theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
                            withRowAnimation:UITableViewRowAnimationNone];
    }
    [tableView endUpdates];
    // scroll to the expanded cell
    [self.theTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle
                                     animated:YES];
}

谁能帮我解决这个问题,我想分别为两个单元格设置高度。

最佳答案

在 heightForAtIndexpath 方法中添加以下代码

 if ([indexPath isEqual:self.expandedIndexPath]) {
    return 60.0;
}
else 
   return 120.0f;

关于ios - 扩展的 UITableviewCell 高度不可管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29342501/

相关文章:

ios - 设置 UILabel.text 属性会导致线程中的内存泄漏

iphone - 我应该使用 if 还是 switch 语句? Objective-C

iphone - 如何在 iPhone 中创建自定义时间栏?

ios - UITableView 在 iOS 7 中不根据其高度显示

ios - 重新启动应用程序时导入 header 错误 Swift

ios - CAShapeLayer 和 CABasicAnimation for circle - loader style - blinking

ios - 检查 SKAction 是否正在运行

ios - 在文本框中显示 API 数据

ios - tableView 滚动真的很慢——我调用了太多方法?

ios - 圆形框架 iOS 中的图像