5

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 Image TB2 crash log 1 crash log 2 properties

Update:

breakpoint

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

first line shown after 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?

kamran619
  • 531
  • 10
  • 32
  • 2
    That `tableView:cellForRowAtIndexPath:` method **begs** for refactoring. –  Jul 05 '13 at 22:33
  • @H2CO3 I definitely agree with you on this. Still, I don't see anything wrong with that **selector** – kamran619 Jul 05 '13 at 22:38
  • So where is your `numberOfSections` method? – ott-- Jul 06 '13 at 20:13
  • numberOfSections is not in the UITableView Delegate nor in the Data Source protocol. I tried googling more into it and can't find an instance where it is called. The actual selector would be `-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView` – kamran619 Jul 07 '13 at 17:20
  • 2
    Try to set exception breakpoint to see on which particular line the app crashes? – Valent Richie Jul 08 '13 at 01:35
  • @verbumdei Tried this..see update – kamran619 Jul 08 '13 at 04:58
  • 1
    Check that your data source is not deallocated at some instance. See this for hint: http://stackoverflow.com/questions/10560693/load-uitableview-crash-caused-by-message-sent-to-deallocated-instance – NightFury Jul 08 '13 at 05:19
  • @anum90 - I tried turning watchpoints on to investigate further and it showed that my data source is not deallocated. It gets deallocated when I hit the back button - in which case the whole view is deallocated. – kamran619 Jul 08 '13 at 05:45
  • Any chance you are getting this error after loading the beta builds of XCode for ios7? – sosborn Jul 08 '13 at 12:03
  • I see that, in your stack trace, there is the symbol UIViewAccessibility. Are you using the Accessibility Inspector in the simulator? That symbol, in my trying, is only present when the accessibility inspector is on. That shouldn't be the cause of your issue, but start trying to turn it off, it can be a good starting point to investigate. – LombaX Jul 08 '13 at 13:23
  • @sosborn - I will double check but I don't think I upgraded to the new beta. I will look into it when I am near my laptop. – kamran619 Jul 08 '13 at 16:09
  • I have XCode version 4.6.3 – kamran619 Jul 09 '13 at 16:45
  • To tell you the truth dear OP, looking at the code, the crash is the least of your concerns. – Can Jul 11 '13 at 06:09

2 Answers2

1

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;
Vinny Coyne
  • 2,365
  • 15
  • 24
  • Hey, I thought this would work, but it didn't :(. same problem. – kamran619 Jul 11 '13 at 06:09
  • Have you tried the following to better track down the issue: .1 Enable zombies: http://stackoverflow.com/questions/2190227/how-do-i-set-up-nszombieenabled-in-xcode-4 and 2. Run the static analyzer: http://stackoverflow.com/questions/5650771/clang-static-analyzer-for-xcode4 – Vinny Coyne Jul 11 '13 at 10:23
  • I have..none of which helped. Thanks anyways. – kamran619 Jul 14 '13 at 00:37
0

You just mentioned you are using ARC, so did you make your second TableView A strong property? is it an ivar?

The issue is that if you didn't make it a strong property, it will eventually get released thanks to ARC and you won't be able to get to know the problem, can you post code from .h file?

Azhar Yousuf
  • 111
  • 6