无法获得numberofRowsinSection?

时间:2011-07-08 00:18:57

标签: iphone objective-c uitableview

我试图根据它们所在的部分绘制行数。 代码:

-(void)configureView:(NSDictionary *)serverResult{

    NSManagedObject * detailData = [serverResult valueForKey:@"details"];
        NSSet * projID = [detailData valueForKey:@"PROJECTID"];
        NSSet * projStat = [detailData valueForKey:@"PROJECTSTATUS"];
        NSSet * appDate = [detailData valueForKey:@"BOARDAPPROVALDATE"];
        NSSet * closeDate = [detailData valueForKey:@"LOANCLOSINGDATE"];
        NSSet * countryName = [detailData valueForKey:@"COUNTRYNAME"];
        NSSet * regionName = [detailData valueForKey:@"REGIONNAME"];
        NSSet * envCatCode = [detailData valueForKey:@"ENVASSESMENTCATEGORYCODE"];
        NSSet * teamLeader = [detailData valueForKey:@"TEAMMEMFULLNAME"];
        NSSet * borrower = [detailData valueForKey:@"BORROWER"];
        NSSet * impAgency = [detailData valueForKey:@"IMPAGENCY"];
        NSSet * totalCost = [detailData valueForKey:@"LENDINGPROJECTCOST"];
        NSSet * comtAmt = [detailData valueForKey:@"IBRDPLUSIDAAMT"];
        basic = [NSArray arrayWithObjects:projID, projStat, appDate, closeDate,countryName,regionName,envCatCode,teamLeader,borrower,impAgency,totalCost,comtAmt, nil];
}
    - (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
    {
        int number;
        switch (section) {
            case 0:
                number = basic.count;
                break;
            case 1:
                number = allSectors.count;
                break;
            case 2:
                number = 1;
                break;

            default:
                break;

        }
        return number;
    }

但代码与EXC_BAD_Access崩溃 请帮忙!!!

1 个答案:

答案 0 :(得分:4)

这通常意味着您正在尝试访问已经发布的变量。从您直接访问ivars(而不是通过getter)的方式来判断,它表明您自己正在进行所有内存管理,这可能很难让您始终正确。

在您设置阵列的这一行中,您将收到一个自动释放的数组

basic = [NSArray arrayWithObjects:projID, projStat, appDate, closeDate,countryName,regionName,envCatCode,teamLeader,borrower,impAgency,totalCost,comtAmt, nil];

当您尝试访问它时,这很可能会消失。您需要为其添加保留或不使用自动释放方法

1

// The preferred way in this instance
basic = [[NSArray alloc] initWithObjects:projID, projStat, appDate, closeDate,countryName,regionName,envCatCode,teamLeader,borrower,impAgency,totalCost,comtAmt, nil];

2

// Not the best way
basic = [[NSArray arrayWithObjects:projID, projStat, appDate, closeDate,countryName,regionName,envCatCode,teamLeader,borrower,impAgency,totalCost,comtAmt, nil] retain];

无论哪种方式,阅读内存管理都很方便。

使用合成的getter / setter作为属性更容易,而不是直接访问ivars,除非你真的想用initdealloc方法。

我看到的另一个问题(与崩溃无关)是你没有将number初始化为任何东西而你没有在switch语句中的default情况下设置它可能是原因一个很难在以后跟踪的bug。