简体   繁体   English

在执行UItableView委托/数据源方法之前,控制台中显示“之前没有重用表格单元的索引路径”消息

[英]before executing UItableView delegate/datasource method “no index path for table cell being reused before” message is displayed in console

I am developing an application where I am using UITableView which is added programmatically to view as follow, 我正在开发一个使用UITableView的应用程序,该程序以编程方式添加以如下方式进行查看,

//creating tableview1
    tableView1=[[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.width,heightOfTable1)];
    tableView1.scrollEnabled=NO;
    tableView1.dataSource=self;
    tableView1.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView1];

following is my cellForRowAtIndexPath method 以下是我的cellForRowAtIndexPath方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier=@"CustomHiddenViewCell";
    CustomHiddenViewCell *cell = (CustomHiddenViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomHiddenViewCell" owner:self options:nil];

        cell = [nib objectAtIndex:0];

    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.greetingLabel.hidden=YES;
    cell.detailViewButton.hidden=YES;
    cell.separatorView.hidden=YES;
    cell.accessoryType=UITableViewCellAccessoryNone;

    if (tableView==tableView1) {
        [[cell viewWithTag:101] removeFromSuperview];
        [[cell viewWithTag:102] removeFromSuperview];
        [[cell viewWithTag:103] removeFromSuperview];
        [[cell viewWithTag:104] removeFromSuperview];
        [[cell viewWithTag:105] removeFromSuperview];
        [[cell viewWithTag:106] removeFromSuperview];
        [[cell viewWithTag:107] removeFromSuperview];
        [[cell viewWithTag:108] removeFromSuperview];
        [[cell viewWithTag:109] removeFromSuperview];


        if (initialViewFlag==1) {
            if (indexPath.row==0) {
                UIImageView *logoImageView=[[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/2-60, 5, 120,50)];
                logoImageView.image=[self convertImage];
                [logoImageView setTag:101];
                [cell.contentView addSubview:logoImageView];
                [cell.contentView bringSubviewToFront:logoImageView];
            }
            else if (indexPath.row==1) {
                [cell.detailViewButton addTarget:self action:@selector(exploreViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

                UIButton *backButton=[[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+20,10,70,40)];
                [backButton setTitle:@"Back" forState:UIControlStateNormal];
                [backButton setTag:102];
                [backButton setBackgroundColor:[UIColor clearColor]];
                [cell.contentView addSubview:backButton];
                [cell bringSubviewToFront:backButton];

                UIButton *homeButton=[[UIButton alloc]initWithFrame:CGRectMake(cell.detailViewButton.frame.origin.x+cell.detailViewButton.frame.size.width+58,10,70,40)];
                [homeButton setTitle:@"Home" forState:UIControlStateNormal];
                [homeButton setTag:103];
                [homeButton setBackgroundColor:[UIColor clearColor]];
                [cell.contentView addSubview:homeButton];
                [cell bringSubviewToFront:homeButton];
            }
            else if (indexPath.row==2){
                UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,0,cell.frame.size.width,cell.frame.size.height/2)];
                [cellLabel setText:@"Personal Details"];
                [cellLabel setTag:104];
                [cell.contentView addSubview:cellLabel];
            }
        }
        else if(initialViewFlag==2){
            if (indexPath.row==0) {
                UIImageView *logoImageView=[[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/2-60, 5,120,50)];
                logoImageView.image=[self convertImage];
                [logoImageView setTag:201];
                [cell.contentView addSubview:logoImageView];
            }
            else if (indexPath.row==1) {
                cell.greetingLabel.hidden=YES;
                cell.detailViewButton.hidden=YES;
                UIButton *phoneButton=[[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+5,cell.frame.origin.y+2,70,cell.frame.size.height-4)];
                [phoneButton setTitle:@"Phone" forState:UIControlStateNormal];
                [phoneButton setTag:202];
                [cell.contentView addSubview:phoneButton];


                UIButton *emailButton=[[UIButton alloc]initWithFrame:CGRectMake(phoneButton.frame.origin.x+phoneButton.frame.size.width+10,cell.frame.origin.y+2,70,cell.frame.size.height-4)];
                [emailButton setTitle:@"Email" forState:UIControlStateNormal];
                [emailButton setTag:203];
                [cell.contentView addSubview:emailButton];
                [cell bringSubviewToFront:emailButton];

                UIButton *aboutInfoButton=[[UIButton alloc]initWithFrame:CGRectMake(emailButton.frame.origin.x+emailButton.frame.size.width+10,cell.frame.origin.y+2,70,cell.frame.size.height-4)];
                [aboutInfoButton setTitle:@"About" forState:UIControlStateNormal];
                [aboutInfoButton setTag:204];
                [cell.contentView addSubview:aboutInfoButton];
                [cell bringSubviewToFront:aboutInfoButton];

                UIButton *logoutButton=[[UIButton alloc]initWithFrame:CGRectMake(aboutInfoButton.frame.origin.x+aboutInfoButton.frame.size.width+10,cell.frame.origin.y+2,70,cell.frame.size.height-4)];
                [logoutButton setTitle:@"Logout" forState:UIControlStateNormal];
                [logoutButton setTag:205];
                [cell.contentView addSubview:logoutButton];
                [cell bringSubviewToFront:logoutButton];

            }
            else if (indexPath.row==2) {
                cell.detailViewButton.hidden=NO;
                [cell.detailViewButton addTarget:self action:@selector(exploreViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            }
            else if (indexPath.row==3) {

                cell.detailViewButton.hidden =NO;
                [cell.detailViewButton addTarget:self action:@selector(exploreViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

                UIButton *backButton=[[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+20,10,70,40)];
                [backButton setTitle:@"Back" forState:UIControlStateNormal];
                [backButton setTag:206];
                [cell.contentView addSubview:backButton];
                [cell bringSubviewToFront:backButton];

                UIButton *homeButton=[[UIButton alloc]initWithFrame:CGRectMake(cell.detailViewButton.frame.origin.x+cell.detailViewButton.frame.size.width+58,10,70,40)];
                [homeButton setTitle:@"Home" forState:UIControlStateNormal];
                [homeButton setTag:207];
                [homeButton setBackgroundColor:[UIColor clearColor]];
                [cell.contentView addSubview:homeButton];
                [cell bringSubviewToFront:homeButton];
            }
            else if (indexPath.row==4){
                UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,0,cell.frame.size.width/2,cell.frame.size.height/2)];
                [cellLabel setText:@"Personal Details"];
                [cellLabel setTag:208];
                cellLabel.backgroundColor=[UIColor clearColor];
                [cell.contentView addSubview:cellLabel];
                [cell bringSubviewToFront:cellLabel];

            }
        }
        else if (initialViewFlag==3){

            if (indexPath.row==0) {

                cell.detailViewButton.hidden =NO;
                [cell.detailViewButton addTarget:self action:@selector(exploreViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

                UIButton *backButton=[[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+20,10,70,40)];
                [backButton setTitle:@"Back" forState:UIControlStateNormal];
                [backButton setTag:301];
                [backButton setBackgroundColor:[UIColor clearColor]];
                [cell.contentView addSubview:backButton];
                [cell bringSubviewToFront:backButton];

                UIButton *homeButton=[[UIButton alloc]initWithFrame:CGRectMake(cell.detailViewButton.frame.origin.x+cell.detailViewButton.frame.size.width+58,10,70,40)];
                [homeButton setTitle:@"Home" forState:UIControlStateNormal];
                [homeButton setTag:302];
                [homeButton setBackgroundColor:[UIColor clearColor]];
                [cell.contentView addSubview:homeButton];
                [cell bringSubviewToFront:homeButton];
            }
            else if (indexPath.row==1){
                UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,0,cell.frame.size.width/2,cell.frame.size.height/2)];
                [cellLabel setText:@"Personal Details"];
                [cellLabel setTag:303];
                cellLabel.backgroundColor=[UIColor clearColor];
                [cell.contentView addSubview:cellLabel];
                [cell bringSubviewToFront:cellLabel];

            }
        }
    }
    return cell;
}




//numberOfSectionsInTableView method

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==accountStatementUpperTableView) {
        if (initialViewFlag==1) {
            return 3;
        }
        else if(initialViewFlag==2){
            return 5;
        }
        else if (initialViewFlag==3){
            return 2;
        }
    }
    return 0;
}


//heightForRowAtIndexPath method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier=@"CustomHiddenViewCell";
    CustomHiddenViewCell *cell = (CustomHiddenViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomHiddenViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
    if (tableView==accountStatementUpperTableView) {
        [accountStatementUpperTableView beginUpdates];
        [accountStatementUpperTableView endUpdates];
        if (initialViewFlag==1) {
            if (indexPath.row==0 || indexPath.row==1) {
                return cell.frame.size.height;
            }
            else if (indexPath.row==2){
                return cell.frame.size.height/2;
            }
        }
        else if (initialViewFlag==2) {
            if (indexPath.row==0 || indexPath.row==1 || indexPath.row==3) {
                return cell.frame.size.height;
            }
            else if(indexPath.row==2){
                return cell.detailViewButton.frame.size.height;
            }
            else if(indexPath.row==4){
                double heightOfCell=(cell.frame.size.height/2);
                return heightOfCell;
            }
        }
        else if (initialViewFlag==3){
            if (indexPath.row==0) {
                return cell.frame.size.height;
            }
            else if (indexPath.row==1){
                return cell.frame.size.height/2;
            }
        }

    }
    return cell.frame.size.height;
}
#pragma mark -

When I run my application it gives me "no index path for table cell being reused before" message in console. 当我运行我的应用程序时,它在控制台中显示“之前没有重用表单元的索引路径”消息。 When I tried to run it with debugger I found that this message is displayed first and then it starts executing UItableView's delegate and datasource methods. 当我尝试使用调试器运行它时,我发现该消息首先显示,然后开始执行UItableView的委托和数据源方法。

I have also tried running after removing all reload, begin update and end update statement but still I am getting this message. 在删除所有重新加载,开始更新和结束更新语句之后,我也尝试运行,但是仍然收到此消息。 And this message is not shown during reload or update statement it is only shown before loading 并且此消息在重新加载或更新语句期间不显示,仅在加载之前显示

Can anyone please tell me why this is happening? 谁能告诉我为什么会这样? Is anything wrong in my code? 我的代码有什么问题吗? Any help is appreciated. 任何帮助表示赞赏。

Got the solution. 得到了解决方案。 I am getting this message just because I am reloading tableview in completion block of presentViewController of earlier view controller while presenting another view controller. 我收到此消息只是因为我在呈现另一个视图控制器时在较早的视图控制器的presentViewController的完成块中重新加载了tableview。

Solved this issue by removing reload statement from completion block of presentViewController 通过从presentViewController的完成块中删除重载语句解决了此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM