ios - tableview cellForRowAtIndexPath不被执行
高洛峰
高洛峰 2017-04-17 11:50:48
[iOS讨论组]

我用的是storyboard,在其中一个viewcontroller中拖入tableview,并与对应的类进行链接,但是在运行时,numberOfSectionsInTableView和numberOfRowsInSection均可执行,但cellForRowAtIndexPath就是不执行,界面也不能显示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];
}
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(3)
怪我咯

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

阿神

朋友,问题解决了没,我也遇到这种问题

ringa_lee

解决思路:用tag来标记这个tableview。在执行的时候在区分。

先给TableView打上tag

tb.tag = 100;

然后在cellForRowAtIndexPath里面

     UITableView *a = (UITableView *)tableView;
     if(a.tag ==100){
     // 执行你的方法
     }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号