滚动时UITableView崩溃

时间:2011-03-07 23:31:09

标签: iphone objective-c uitableview memory-management

我加载了一个带有数据的UITableView,请参阅下面的代码,一切都很好。但是,当我滚动表格视图时,它会崩溃。我无法找到dealloc问题的位置,并想知道一个好的和有经验的人能给我一个提示吗?

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

'保留'(s)被放在那里进行测试。

当我采取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 retain]; //用于测试

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

4 个答案:

答案 0 :(得分:1)

您不应该发布cellValue1 releasecellValue2。通过致电objectAtIndex:来获取它们并不会保留它们。

答案 1 :(得分:0)

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

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]为空

OR

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

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

您应该能够使用调试器逐步完成并找出这些内容的值,并找出错误发生的位置。

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如何在cellArray1和cellArray2中添加数据。你可以在填充这些数组的地方发布代码吗?

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