【问题标题】:UITableView crashes when scrollingUITableView 滚动时崩溃
【发布时间】:2011-07-10 17:48:59
【问题描述】:

我加载了一个带有数据的 UITableView,见下面的代码,最初一切正常。但是,当我滚动表格视图时,它会崩溃。我找不到dealloc问题出在哪里,想知道有没有经验丰富的好人能给我提示?

我已经用完整的代码更新了这篇文章,包括数组的加载。

“保留”被放在那里进行测试。

当我采取 cellValue1 的方式时,我没有问题,但是我没有任何数据要显示。

我收到以下消息:

[CFString isEqualToString:]: 消息发送到已释放实例 0x5e5ae70

- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Topplista";


AccessPlayerData *readPlayerDataFunction = [AccessPlayerData new];
NSMutableArray *leaderboardArray = [[NSMutableArray alloc] initWithCapacity:0]; 
leaderboardArray = [readPlayerDataFunction readPlayersFileForLeaderBoard];

cellArray1 = [[NSMutableArray alloc]init];
cellArray2 = [[NSMutableArray alloc]init];

NSString *keyName = [[NSString alloc]init]; // Player name
double percentCorrectAnswers = 0;           // % correct answers
double nrCorrectAnswers = 0;                // # correct answers
double totalNrQuestions = 0;                // # questions ever answered


nrOfPlayers = [leaderboardArray count] / 3;

keyName = [leaderboardArray objectAtIndex:3];
nrCorrectAnswers = [[leaderboardArray objectAtIndex:4]doubleValue];
totalNrQuestions = [[leaderboardArray objectAtIndex:5]doubleValue];

percentCorrectAnswers = (nrCorrectAnswers / totalNrQuestions) * 100;

int loop = 0;
int loop2 = 2;
int counter = 1;

for (int oo = 0; oo < nrOfPlayers; oo++) {

    if ([[leaderboardArray objectAtIndex:loop2]intValue] > 0) {

        keyName = [leaderboardArray objectAtIndex:loop];
        loop++;
        nrCorrectAnswers = [[leaderboardArray objectAtIndex:loop]doubleValue];
        loop++;
        totalNrQuestions = [[leaderboardArray objectAtIndex:loop]doubleValue];
        loop++;
        percentCorrectAnswers = (nrCorrectAnswers / totalNrQuestions) * 100;

        [cellArray1 addObject:keyName];                                         // Player name
        [cellArray1 addObject:[NSNumber numberWithDouble:totalNrQuestions]];        
        [cellArray1 addObject:[NSNumber numberWithDouble:nrCorrectAnswers]];
        [cellArray1 addObject:[NSNumber numberWithDouble:percentCorrectAnswers]];

        counter++;
    }
    else {
        loop = loop + 3;
    }
    loop2 = loop2 + 3;
}

self.sortArray;

rowCount = [cellArray1 count];

[keyName retain];
[readPlayerDataFunction release];
[keyName release];
[leaderboardArray release];

}

- (void)sortArray {

//outputArray

NSMutableArray *dummyArray = [[NSMutableArray alloc]initWithCapacity:0];
NSMutableArray *editPlayerArray = [[NSMutableArray alloc] initWithArray:cellArray1];
NSArray *sortedArray = [[NSArray alloc]init];

int nrOfActivePlayers = [cellArray1 count] / 4;
int counteR = 2;

for (int qq = 0; qq < nrOfActivePlayers; qq++) {
    [dummyArray addObject:[cellArray1 objectAtIndex:counteR]];
    counteR = counteR + 4;
}

// Sort the array
sortedArray = [dummyArray sortedArrayUsingSelector:@selector(compare:)];


int rank = 0;
int roller = [editPlayerArray count] / 4;
int oldNrAnswers = 0;

NSMutableArray *finalArray = [[NSMutableArray alloc]initWithCapacity:0];

for (int qq = nrOfActivePlayers - 1; qq > -1; qq--) {
    counteR = 2;


    for (int rr = 0; rr < roller; rr++) {

        if ([[editPlayerArray objectAtIndex:counteR]intValue] == [[sortedArray objectAtIndex:qq]intValue]) {

            //=====FIX RANKING IF THERE IS MORE THAN ONE PLAYER WITH THE SAME RESULT=====//
            if ([[editPlayerArray objectAtIndex:counteR]intValue] == oldNrAnswers) {
                if (rank == 0) rank = 1;
            }
            else {
                rank++;
            }

            [finalArray addObject:[NSNumber numberWithInt:rank]];                   // Rank
            [finalArray addObject:[editPlayerArray objectAtIndex:counteR - 2]];     // Player name
            [finalArray addObject:[editPlayerArray objectAtIndex:counteR - 1]];     // Asked questions
            [finalArray addObject:[editPlayerArray objectAtIndex:counteR]];         // Correct answers

            oldNrAnswers = [[editPlayerArray objectAtIndex:counteR]intValue];

            [finalArray addObject:[editPlayerArray objectAtIndex:counteR + 1]];     // % correct answers

            [editPlayerArray removeObjectAtIndex:counteR - 2];
            [editPlayerArray removeObjectAtIndex:counteR - 2];
            [editPlayerArray removeObjectAtIndex:counteR - 2];
            [editPlayerArray removeObjectAtIndex:counteR - 2];

            roller = [editPlayerArray count] / 4;

            break;
        }
        else {
            counteR = counteR + 4;
        }

} }

[sortedArray retain];

[cellArray1 removeAllObjects];

nrOfPlayers = [finalArray count] / 5;

counteR = 0;
NSString *cellValue1 = [[NSString alloc]init];
NSString *cellValue2 = [[NSString alloc]init];
//======FORMAT THE ARRAY INTO TWO ARRAYS FOR DEPLOYMENT=======//
for (int qq = 0; qq < nrOfPlayers; qq++) {
    cellValue1 = [NSString stringWithFormat:@"%i. %@ (Antal rätt svar: %.0f)",
                            [[finalArray objectAtIndex:counteR]intValue], 
                            [finalArray objectAtIndex:counteR + 1],
                            [[finalArray objectAtIndex:counteR + 3]doubleValue]];

    cellValue2 = [NSString stringWithFormat:@"Antal frågor: %.0f : Procent rätt svar: %.1f%%", 
                            [[finalArray objectAtIndex:counteR + 2]doubleValue], 
                            [[finalArray objectAtIndex:counteR + 4]doubleValue]];
    counteR = counteR + 5;

    [cellArray1 addObject:cellValue1];
    [cellArray2 addObject:cellValue2];

}

    [dummyArray release];
[editPlayerArray release];
[sortedArray release];
[finalArray release];
[cellValue1 release];
[cellValue2 release];

}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
CGRect Label2Frame = CGRectMake(30, 33, 270, 25);
UILabel *lblTemp;

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.backgroundColor = [UIColor orangeColor];
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:16]];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];

//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.backgroundColor = [UIColor orangeColor];
lblTemp.tag = 2;
[lblTemp setFont:[UIFont fontWithName:@"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];

return cell;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

tableView.backgroundColor = [UIColor orangeColor];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)
    cell = [self getCellContentView:CellIdentifier];

UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];

NSLog(@" ");
NSLog(@" ");
NSLog(@" ");

NSLog(@"============");
NSLog(@"cA1: %i", [cellArray1 count]);
NSLog(@"cA2: %i", [cellArray2 count]);
NSLog(@"rowCount %i", rowCount);
NSLog(@"ixP row: %i", indexPath.row);

NSString *cellValue1 = [cellArray1 objectAtIndex:indexPath.row];

// NSString *cellValue2 = [cellArray2 objectAtIndex:indexPath.row];

[cellValue1 retain];  // for testing

// [cellValue2 保留]; // 用于测试

NSLog(@"cV1: %@", cellValue1);
NSLog(@"cV2: %@", @"cellValue2");
lblTemp1.text = cellValue1;
lblTemp2.text = @"cellValue2";

return cell;

}

结果如下:

2011-03-09 21:33:23.850 FamQuiz_R0_1[842:207] cV2: cellValue2
2011-03-09 21:33:23.850 FamQuiz_R0_1[842:207]  
2011-03-09 21:33:23.851 FamQuiz_R0_1[842:207]  
2011-03-09 21:33:23.851 FamQuiz_R0_1[842:207]  
2011-03-09 21:33:23.852 FamQuiz_R0_1[842:207] ============
2011-03-09 21:33:23.852 FamQuiz_R0_1[842:207] cA1: 7
2011-03-09 21:33:23.853 FamQuiz_R0_1[842:207] cA2: 7
2011-03-09 21:33:23.853 FamQuiz_R0_1[842:207] rowCount 7
2011-03-09 21:33:23.853 FamQuiz_R0_1[842:207] ixP row: 5
2011-03-09 21:33:23.854 FamQuiz_R0_1[842:207] cV1: 5. Barnspelare (Antal rätt svar: 2)
2011-03-09 21:33:23.854 FamQuiz_R0_1[842:207] cV2: cellValue2
2011-03-09 21:33:27.370 FamQuiz_R0_1[842:207]  
2011-03-09 21:33:27.371 FamQuiz_R0_1[842:207]  
2011-03-09 21:33:27.371 FamQuiz_R0_1[842:207]  
2011-03-09 21:33:27.372 FamQuiz_R0_1[842:207] ============
2011-03-09 21:33:27.373 FamQuiz_R0_1[842:207] cA1: 7
2011-03-09 21:33:27.373 FamQuiz_R0_1[842:207] cA2: 7
2011-03-09 21:33:27.374 FamQuiz_R0_1[842:207] rowCount 7
2011-03-09 21:33:27.374 FamQuiz_R0_1[842:207] ixP row: 6
2011-03-09 21:33:27.375 FamQuiz_R0_1[842:207] *** -[CFString retain]: message sent to deallocated instance 0x5b446a0

【问题讨论】:

  • getCellContentView 是什么样的?

标签: iphone objective-c uitableview memory-management


【解决方案1】:

你不应该释放cellValue1 releasecellValue2。通过调用objectAtIndex: 获取它们不会保留它们。

【讨论】:

  • 嗨,谢谢你回答 viking.... Oppps 我更改了代码并忘记删除这些行,但在没有版本的情况下仍然有相同的问题。
  • 您可能应该包含 getContentView 方法的代码。
【解决方案2】:

这是一个想法 - 也许错误就在这里。

UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];


NSString *cellValue1 = [cellArray1 objectAtIndex:indexPath.row];
NSString *cellValue2 = [cellArray2 objectAtIndex:indexPath.row];

lblTemp1.text = cellValue1;
lblTemp2.text = cellValue2;

[cellArray1 objectAtIndex:indexPath.row] 或 [cellArray2 objectAtIndex:indexPath.row] 为 null

[cellArray1 objectAtIndex:indexPath.row] 或 [cellArray2 objectAtIndex:indexPath.row] 为空。

在第一种情况下,当您尝试将值分配给空 lblTemp1.txt 时,程序会崩溃。 在第二种情况下,程序崩溃,因为 NSString* cellValue 为空,然后它尝试显示空字符串。

您应该能够使用调试器逐步检查并找出这些东西的值,并找出错误发生的位置。

鉴于您的程序最初可以工作,然后在您滚动时死机,我想知道您从中提取值的 cellArray1 和 cellArray2 实际上是否具有与您要求 scrollView 显示的行数一样多的数据。也许您的数据少于您在 TableViewDatasource 中指定的行数?

【讨论】:

    【解决方案3】:

    数据在 tableView 创建后立即释放,因此当您滚动时,应用程序崩溃。尝试将所有代码发布在 .m 文件中,以供人们查看问题。我确定它不在您发布的代码中。

    【讨论】:

      【解决方案4】:

      如何在cellArray1 和cellArray2 中添加数据。能否请您发布填充这些数组的代码?

      这可能是您过度释放要添加到 cellArrays 的 NSStrings 的情况。

      【讨论】:

        猜你喜欢
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多