numberOfSections被调用? iOS版

时间:2013-07-05 22:25:52

标签: ios objective-c uitableview unrecognized-selector

跳过所有这些信息并跳到底部的更新...

之前我曾经处理过无法识别的选择器,但我无法弄清楚这次发生了什么。调用堆栈是不透明的,我无法找出问题的起源。我尝试过使用符号和异常断点。这段代码在之前工作得很好..我再回来研究这个程序了,现在我正在进入这个

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

我在这个视图控制器中有两个UITableViews。其中一个与IB(datasource and delegate)联系在一起。第二个在代码中实例化,其数据源/委托也指向视图控制器。我在第二个tableview上放了一个2的标记,以区分它们。 UITableViewTwo添加到UIView,如果我有要显示的有效数据,则会将其作为第一个tableview的页脚返回。再一次,这段代码完美地工作,一段时间没有任何崩溃,我似乎无法弄清楚为什么它现在崩溃。我正在使用ARC。

以下是一些代码:

当我检查发送选择器的对象的地址是什么时:它确实是UITableViewSectionElement。为什么H接收到呼叫号码部分?它不应该是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;
}

查看已加载(相关信息):

 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];

我附上了该应用的一些截图:

每当我点击或尝试滚动tableviewTwo时,我就会崩溃。

App Image TB2 crash log 1 crash log 2 properties

更新

breakpoint

这是我滚动第二个UITableView时显示的内容(启用所有例外):

first line shown after clicking continue

我有一个完美的功能在我的手机上工作(一个月前)..我不知道为什么我现在收到这个错误。如果Interface Builder导致这种情况可以改变吗?我检查了所有的商店并将其包括在内。感谢..

更新

奇怪的是,当我在另一台笔记本电脑上克隆回购时,它的运行没有问题。我想我用的项目不知何故被破坏了?我会回家进一步调查。

为什么这不会崩溃在我的工作笔记本电脑(类似的规格)并且在我的笔记本电脑上崩溃的任何想法。我已经在两台笔记本电脑中克隆了git目录,我在这里遇到了崩溃,但不是那里。我在笔记本电脑上运行XCode版本4.6.3。

更新2:

它不再在我的笔记本电脑上工作..WTF正在进行中? Xcode 4.6.3中有错误吗?有什么办法可以解决这个问题吗?为什么要调用numberOfSections?我无法在任何地方找到该文档。

更新3:

好的,事实证明,viewForFooterInSection选择器中的这行代码是罪魁祸首:

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

它在设备上工作正常,但在模拟器上崩溃。当我只是将其改为以下时..它可以工作。

return tableViewTwo;

有谁知道为什么在视图中返回tableview会出错?

2 个答案:

答案 0 :(得分:1)

numberOfSectionsUITableView上的一种方法 - 它不是委托方法。看起来系统在内部调用此方法,并且由于某种原因,您的表将自动释放(因此崩溃)。

我会在您的viewForFooterInSection方法中尝试以下操作:

static UIView *view = nil;

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

return view;

答案 1 :(得分:0)

您刚才提到您正在使用ARC,那么您是否将第二个TableView强大的属性?这是一个伊娃吗?

问题是如果你没有把它变成一个强大的属性,它最终会因ARC而被释放,你将无法知道这个问题,你能从.h文件发布代码吗? / p>