开发者社区> 问答> 正文

tableview cellForRowAtIndexPath不被执行

我用的是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];
}

展开
收起
a123456678 2016-07-20 16:59:23 1855 0
1 条回答
写回答
取消 提交回答
  • 用tag来标记这个tableview。在执行的时候在区分。

    先给TableView打上tag
    
    tb.tag = 100;
    然后在cellForRowAtIndexPath里面
    
         UITableView *a = (UITableView *)tableView;
         if(a.tag ==100){
         // 执行你的方法
         }
    2019-07-17 19:59:03
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载