Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numberOfSections being called? iOS

Skip all this info and skip to the update at the bottom...

I have dealt with unrecognized selectors before, but I cannot figure out what is going on this time. The call stack is opaque and I can't find out where the problem is originating from. I have tried using symbolic and exception breakpoints. This code was working perfectly before..I come back to work on this program some more and am now running into this

[UITableViewSectionElement numberOfSections]: unrecognized selector sent to instance 0xa285d50

I have two UITableViews in this view controller. One of them is hooked up in IB(datasource and delegate). The second one is instantiated in code and its datasource/delegate are also pointing to the view controller. I put a tag of 2 on the second tableview to distinguish them from each other. UITableViewTwo is add to a UIView which is returned as the first tableview's footer if I have valid data to show. Again, this code was working perfectly without any crashes a while back, and I can't seem to figure out why it is crashing now. I am using ARC.

Here is some code:

When I check what the address of the object the selector being sent to is: it is indeed a UITableViewSectionElement. Why the H is this receiving the call numberOfSections? Isn't it supposed to be numberOfSectionsInTableView?

UITableView Delegate/DataSource methods

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 0) {
        if (indexPath.row == 0) {
            PCFCustomNumberReviewsCell *cell = [self.tableViewOne dequeueReusableCellWithIdentifier:@"PCFNumberOfRatings"];
            [cell.numberOfReviews setText:[NSString stringWithFormat:@"%d", numReviews.integerValue]];
            [cell.numberOfReviews setFont:[PCFFontFactory droidSansFontWithSize:17]];
            [cell.numberOfReviews setTextColor:[UIColor lightGrayColor]];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
            [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
            [cell setBackgroundView:imgView];
            return cell;
        }else if(indexPath.row == 1) {
            PCFCustomReviewCell *cell = [self.tableViewOne dequeueReusableCellWithIdentifier:@"RateEasiness"];
            [cell.stars setBackgroundImage:[self getImageForStars:[NSString stringWithFormat:@"%d", totalEasiness.integerValue]] forState:UIControlStateNormal];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
            [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
            [cell setBackgroundView:imgView];
            return cell;
        }else if(indexPath.row == 2) {
            PCFCustomReviewCell *cell = [self.tableViewOne dequeueReusableCellWithIdentifier:@"RateUsefulness"];
            [cell.stars setBackgroundImage:[self getImageForStars:[NSString stringWithFormat:@"%d", totalUsefulness.integerValue]] forState:UIControlStateNormal];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
            [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
            [cell setBackgroundView:imgView];
            return cell;
        }else if(indexPath.row == 3) {
            PCFCustomReviewCell *cell = [self.tableViewOne dequeueReusableCellWithIdentifier:@"RateFunness"];
            [cell.stars setBackgroundImage:[self getImageForStars:[NSString stringWithFormat:@"%d", totalFunness.integerValue]] forState:UIControlStateNormal];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
            [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
            [cell setBackgroundView:imgView];
            return cell;
        }else if(indexPath.row == 4) {
            PCFCustomReviewCell *cell = [self.tableViewOne dequeueReusableCellWithIdentifier:@"RateInterestLevel"];
            [cell.stars setBackgroundImage:[self getImageForStars:[NSString stringWithFormat:@"%d", totalInterestLevel.integerValue]] forState:UIControlStateNormal];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
            [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
            [cell setBackgroundView:imgView];
            return cell;
        }else if(indexPath.row == 5) {
            PCFCustomReviewCell *cell = [self.tableViewOne dequeueReusableCellWithIdentifier:@"RateTexbookUse"];
            [cell.stars setBackgroundImage:[self getImageForStars:[NSString stringWithFormat:@"%d", totalTextbookUse.integerValue]] forState:UIControlStateNormal];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
            [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
            [cell setBackgroundView:imgView];
            return cell;
        }else if(indexPath.row == 6) {
            PCFCustomReviewCell *cell = [self.tableViewOne dequeueReusableCellWithIdentifier:@"RateOverall"];
            [cell.stars setBackgroundImage:[self getImageForStars:[NSString stringWithFormat:@"%d", totalOverall.integerValue]] forState:UIControlStateNormal];
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
            [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
            [cell setBackgroundView:imgView];
            return cell;
        }

    }else {
        PCFCustomCourseCommentCell *cell = (PCFCustomCourseCommentCell *) [self.tableViewTwo dequeueReusableCellWithIdentifier:@"PCFCourseCommentCell"];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PCFCustomCourseReviewCell" owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];
        }
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
        [imgView setImage:[UIImage imageNamed:@"1slot2.png"]];
        [cell setBackgroundView:imgView];

        PCFRateModel *rateObject = [courseReviews objectAtIndex:indexPath.section];
        [cell.userName setText:rateObject.username];
        [cell.date setText:rateObject.date];
        [cell.professor setText:rateObject.course];
        [cell.comment setText:rateObject.message];
        [cell.term setText:rateObject.term];
        for (UIView *view in cell.contentView.subviews) {
            if ([view isMemberOfClass:[UILabel class]]) {
                UILabel *tempLabel = (UILabel *)view;
                if ([tempLabel tag] != 0) {
                    [tempLabel setFont:[PCFFontFactory droidSansFontWithSize:tempLabel.tag]];
                }
            }
        }
        CGSize size = [rateObject.message sizeWithFont:[PCFFontFactory droidSansFontWithSize:11] constrainedToSize:CGSizeMake(290, 100000)];
        [cell.comment setFrame:CGRectMake(cell.comment.frame.origin.x, cell.comment.frame.origin.y, size.width, size.height)];
        [cell.comment setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];
        [cell.comment setPreferredMaxLayoutWidth:290];
        [cell.comment setLineBreakMode:NSLineBreakByWordWrapping];
        [cell.starFunness setBackgroundImage:[self getImageForStars:rateObject.totalClarity] forState:UIControlStateNormal];
        [cell.starEasiness setBackgroundImage:[self getImageForStars:rateObject.totalEasiness] forState:UIControlStateNormal];
        [cell.starUsefulness setBackgroundImage:[self getImageForStars:rateObject.totalHelpfulness] forState:UIControlStateNormal];
        [cell.starInterestLevel setBackgroundImage:[self getImageForStars:rateObject.totalInterestLevel] forState:UIControlStateNormal];
        [cell.starOverall setBackgroundImage:[self getImageForStars:rateObject.totalOverall] forState:UIControlStateNormal];
        [cell.starTextbookUse setBackgroundImage:[self getImageForStars:rateObject.totalTextbookUse] forState:UIControlStateNormal];
        return cell;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 0) {
        if (isLoading == YES) {
            return 0;
        }else {
            return 7;
        }
    }else {
        if (isLoadingComments == NO && courseReviews) {
            return 1;
        }else {
            return 0;
        }

    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (isLoading == YES) return 0;
    if (tableView.tag != 0) return courseReviews.count;
    return  1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0 && tableView.tag == 0) {
        if (isLoading == YES) {
            return  activityIndicator;
        }else if (isLoading == NO) {
            if (isLoadingComments == NO && courseReviews.count > 0) {
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, tableViewTwo.frame.size.height)];
                [view addSubview:tableViewTwo];
                return view;
            }else if (isLoadingComments == YES){
                UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
                UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(145, 10, 36, 36)];
                [view setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
                [view setColor:[UIColor whiteColor]];
                [view startAnimating];
                [subView addSubview:view];
                return subView;
            }else if (!courseReviews) {
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 320, 30)];
                [label setTextColor:[UIColor whiteColor]];
                [label setTextAlignment:NSTextAlignmentCenter];
                [label setFont:[PCFFontFactory droidSansFontWithSize:22]];
                [label setText:@"No Reviews"];
                [label setBackgroundColor:[UIColor clearColor]];
                [view addSubview:label];
                return view;
            }

        }
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView.tag == 0) {
        if (isLoadingComments == NO && courseReviews.count > 0) return tableViewTwo.frame.size.height + 50;
    }
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 2) {
        PCFRateModel *model = [courseReviews objectAtIndex:indexPath.section];
        CGSize size = [model.message sizeWithFont:[PCFFontFactory droidSansFontWithSize:11] constrainedToSize:CGSizeMake(290, 100000)];
        return (93 + size.height + 10);
    }else {
        return tableView.rowHeight;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0 && tableView.tag == 0) {
        if ([PCFInAppPurchases boughtRemoveAds] == NO) {
            if (adView && adView.hidden == NO) {
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
                CGRect frame = adView.frame;
                frame.origin.y = 0;
                adView.frame = frame;
                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 320, 30)];
                [label setNumberOfLines:0];
                [label setText:classTitle];
                [label setFont:[PCFFontFactory droidSansFontWithSize:14]];
                [label setTextColor:[UIColor whiteColor]];
                [label setBackgroundColor:[UIColor clearColor]];
                [view addSubview:adView];
                [view addSubview:label];
                return view;
            }
        }else {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 30)];
            [label setNumberOfLines:0];
            [label setText:classTitle];
            [label setFont:[PCFFontFactory droidSansFontWithSize:14]];
            [label setTextColor:[UIColor whiteColor]];
            [label setBackgroundColor:[UIColor clearColor]];
            return label;

        }
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 & tableView.tag == 0) {
        if ([PCFInAppPurchases boughtRemoveAds] == NO) {
            if (adView && adView.hidden == NO)  {
                return 90;
            }return 10;
        }else {
            return 30;
        }

    }
    return 5;
}

View Did Load(The relevant information):

 tableViewTwo = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 320, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    [tableViewTwo setSectionFooterHeight:0.0f];
    [tableViewTwo setDataSource:self];
    [tableViewTwo setDelegate:self];
    [tableViewTwo setTag:2];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 320, 30)];
    [label setTextColor:[UIColor whiteColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[PCFFontFactory droidSansFontWithSize:22]];
    [label setText:@"User Reviews"];
    [label setBackgroundColor:[UIColor clearColor]];
    [tableViewTwo setTableHeaderView:label];
    [tableViewTwo setRowHeight:136];
    [tableViewTwo setSectionFooterHeight:.01f];
    [tableViewTwo setSectionHeaderHeight:.01f];

I am attaching some screenshots of the app:

Whenever I click on or try to scroll tableviewTwo I get this crash.

App ImageTB2crash log 1crash log 2properties

Update:

breakpoint

This is what it shows when I scroll the second UITableView(with all exceptions enabled):

first line shownafter clicking continue

I have a build of this working perfectly on my phone(a month ago)..I don't know why I am getting this error now. Could a change if Interface Builder have caused this? I have checked all the outlets and included them. Thanks..

Update:

Oddly enough, when I clone the repo on another laptop it is working without problems. I think somehow the project I was using was corrupted? I will go home and investigate further.

Any ideas as to why this is not crashing on my work laptop(similiar specs) and is crashing on my laptop. I have cloned the git directory in both laptops and I am getting a crash here, but not there. I am running XCode version 4.6.3 on my laptop.

Update 2:

It is no longer working on my work laptop..WTF is going on? Is there a bug in Xcode 4.6.3? Is there anything I can do to troubleshoot this further? Why is numberOfSections being called? I can't find documentation on that online ANYWHERE.

Update 3:

Okay, it turns out that this line of code in the viewForFooterInSection selector is the culprit:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, tableViewTwo.frame.size.height)];
                [view addSubview:tableViewTwo];
                return view;

It works fine on the device but crashes on the simulator. When I simply change it to the following..it works.

return tableViewTwo;

Does anyone know why the returning the tableview in a view is giving an error?

like image 318
kamran619 Avatar asked Nov 12 '22 02:11

kamran619


1 Answers

numberOfSections is a method on UITableView - it's not a delegate method. It looks like the system calls this method internally, and for some reason your table is being released automatically (hence the crash).

I would try the following in your viewForFooterInSection method:

static UIView *view = nil;

if (!view) {
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, tableViewTwo.frame.size.height)];
    [view addSubview:tableViewTwo];
}

return view;
like image 180
Vinny Coyne Avatar answered Nov 15 '22 09:11

Vinny Coyne