简体   繁体   中英

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,

//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

-(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.

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.

Solved this issue by removing reload statement from completion block of presentViewController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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