开发者社区> 问答> 正文

tableview cellForRowAtIndexPath不被执行

a123456678 2016-07-20 16:59:23 823

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

#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];
}
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:59:03

    用tag来标记这个tableview。在执行的时候在区分。

    先给TableView打上tag
    
    tb.tag = 100;
    然后在cellForRowAtIndexPath里面
    
         UITableView *a = (UITableView *)tableView;
         if(a.tag ==100){
         // 执行你的方法
         }
    0 0
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

推荐文章