tableview cellForRowAtIndexPath不被执行


我用的是storyboard,在其中一个viewcontroller中拖入tableview,并与对应的类进行链接,但是在运行时,numberOfSectionsInTableView和numberOfRowsInSection均可执行,但cellForRowAtIndexPath就是不执行,界面也不能显示tableview的内容,求解啊!

tableview的链接


 #pragma address table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger number = 5;
//    dropDownOpen = true;
//    if (dropDownOpen) {
//        number = [addressArray count];
//    }
    return number;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    static NSString *DropDownCellIdentifier = @"DropDownCellIdentifier";

    switch ([indexPath row]) {
        case 0: {
            DropDownCell *cell = (DropDownCell*) [tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier];

            if (cell == nil){
                NSLog(@"New Cell Made");

                NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DropDownCell" owner:nil options:nil];

                for(id currentObject in topLevelObjects)
                {
                    if([currentObject isKindOfClass:[DropDownCell class]])
                    {
                        cell = (DropDownCell *)currentObject;
                        break;
                    }
                }
            }

            [[cell textLabel] setText:@"Option 1"];
            dropDown = @"Option 1";
            // Configure the cell.
            return cell;
            break;
            }
            default: {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                }

                NSString *label = [NSString stringWithFormat:@"Option %i", [indexPath row]];

                [[cell textLabel] setText:label];

                // Configure the cell.
                return cell;

                break;
            }
    }
}

#pragma address table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    switch ([indexPath row]) {
        case 0:
        {
            DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:indexPath];

            NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+1 inSection:[indexPath section]];
            NSIndexPath *path1 = [NSIndexPath indexPathForRow:[indexPath row]+2 inSection:[indexPath section]];
            NSIndexPath *path2 = [NSIndexPath indexPathForRow:[indexPath row]+3 inSection:[indexPath section]];

            NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil];

            if ([cell isOpen])
            {
                [cell setClosed];
                dropDownOpen = [cell isOpen];

                [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
            }
            else
            {
                [cell setOpen];
                dropDownOpen = [cell isOpen];

                [tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
            }

            break;
        }
        default:
        {
            dropDown = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];

            NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]];
            DropDownCell *cell = (DropDownCell*) [tableView cellForRowAtIndexPath:path];

            [[cell textLabel] setText:dropDown];

            // close the dropdown cell

            NSIndexPath *path0 = [NSIndexPath indexPathForRow:[path row]+1 inSection:[indexPath section]];
            NSIndexPath *path1 = [NSIndexPath indexPathForRow:[path row]+2 inSection:[indexPath section]];
            NSIndexPath *path2 = [NSIndexPath indexPathForRow:[path row]+3 inSection:[indexPath section]];

            NSArray *indexPathArray = [NSArray arrayWithObjects:path0, path1, path2, nil];

            [cell setClosed];
            dropDownOpen = [cell isOpen];

            [tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];

            break;
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

storyboard ios cocoa-touch objective-c uitableview

honoha 10 years, 9 months ago

看过一个类似的问题,说是tableview不是同一个。

三倍速法兰西 answered 10 years, 9 months ago

Your Answer