Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS自定义单元格可扩展-索引0问题_Objective C_Ios_Uitableview_Custom Cell - Fatal编程技术网

Objective c iOS自定义单元格可扩展-索引0问题

Objective c iOS自定义单元格可扩展-索引0问题,objective-c,ios,uitableview,custom-cell,Objective C,Ios,Uitableview,Custom Cell,谢谢你的帮助。 我有一个自定义单元格,我使用以下代码展开它。但是,第一个单元格(索引0)总是在启动ViewController时展开 我错过了什么?如何让它们在启动时都不展开,并仅在选择时展开 非常感谢 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCellCell *cell;

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

我错过了什么?如何让它们在启动时都不展开,并仅在选择时展开

非常感谢

 - (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;
    }

这是因为
selectedRow
的默认值为零。您需要将其初始化为

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

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

这是因为
selectedRow
的默认值为零。您需要将其初始化为

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

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

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

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

然后你可以这样做:

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

由于selectedRow将默认为零,因此不会得到错误的匹配。如果以后决定使用节,它也会更灵活。

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

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

然后你可以这样做:

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

由于selectedRow将默认为零,因此不会得到错误的匹配。如果您以后决定使用节,它也会更灵活。

@AhabLives,没问题。通常人们都会忽略这一点。如果这有帮助,请接受。:)@a幸运的是,没问题。通常人们都会忽略这一点。如果这有帮助,请接受。:)