1

谢谢您的帮助。我有一个使用以下代码扩展的自定义单元格。但是,第一个单元格(索引 0)总是在 ViewControllers 启动时展开?

我错过了什么?您如何让它们在启动时全部未扩展,仅在选择时扩展。

非常感谢。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomCellCell *cell;
        static NSString *cellID=@"myCustomCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellID];

        if (cell == nil) 
        {
            NSArray *test = [[NSBundle mainBundle]loadNibNamed:@"myCustomCell" owner:nil options:nil];
            if([test count]>0)
            {
                for(id someObject in test)
                { 
                    if ([someObject isKindOfClass:[CustomCellCell class]]) {
                        cell=someObject;
                        break;
                    }
                }
            }
        }

        cell.LableCell.text = [testArray objectAtIndex:[indexPath row]];
        NSLog( @"data testarray table %@", [testArray objectAtIndex:[indexPath row]]);
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        self.selectedRow = indexPath.row;
        CustomCellCell *cell = (CustomCellCell *)[tableView cellForRowAtIndexPath:indexPath];

        [tableView beginUpdates];
        [tableView endUpdates];

        cell.buttonCell.hidden = NO;
        cell.textLabel.hidden = NO;
        cell.textfiledCell.hidden = NO;
        cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        cell.clipsToBounds = YES;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if(selectedRow == indexPath.row) {
            return 175;
        }

        return 44;
    }
4

2 回答 2

1

那是因为默认值为selectedRow0。您需要将其初始化为,

selectedRow = NSIntegerMax; //or selectedRow = -1;

或其他一些默认值。您可以在viewDidLoad方法中添加它。每当您声明一个 int 类型变量时,它的默认值为零。因此,如果您有需要检查零的情况,例如在上述情况下,您应该将其默认为一个根本不会使用的值。要么是负值,要么NSIntegerMax可以用于此。

于 2012-12-07T20:22:55.170 回答
0

我猜 selectedRow 是一个整数实例变量。该整数以 0 值开始生命。由于第一个表格单元格是第 0 行,因此即使您没有有意设置它,它也会匹配 selectedRow。

解决此问题的一种方法是将 selectedRow 存储为 NSIndexPath 而不是整数。

然后你可以这样做:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([selectedRow isEqual:indexPath]) {
        return 175;
    }
    return 44;
}

而且因为 selectedRow 将默认为 nil,所以您不会得到错误的匹配。如果您决定稍后使用部分,它也会更加灵活。

于 2012-12-07T20:25:13.877 回答