简体   繁体   English

滚动时,UITableView崩溃

[英]UITableView crashes when scrolling

I load a UITableView with data, see code below, and all works well initially. 我用数据加载了一个UITableView,请参见下面的代码,所有这些最初都能很好地工作。 However, when i scroll the table view it crashes. 但是,当我滚动表格视图时会崩溃。 I cannot find where the dealloc problem is and wonder if someone nice and experienced could give me a hint? 我找不到分配问题的所在,想知道是否有人会给我一个提示?

I have updated this post with the full code including the loading of the array. 我已经用完整的代码(包括数组的加载)更新了这篇文章。

The 'retain'(s) was put there for testing. “保留物”被放在那里进行测试。

When i take a way the cellValue1 i do not have the problem but then i do not have any data to display. 当我采取一种方法cellValue1时,我没有问题,但是然后我没有任何数据可以显示。

I get the following message: 我收到以下消息:

[CFString isEqualToString:]: message sent to deallocated instance 0x5e5ae70 [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]; // NSString * cellValue2 = [cellArray2 objectAtIndex:indexPath.row];

[cellValue1 retain];  // for testing

// [cellValue2 retain]; // [cellValue2 keep]; // for testing // 供测试用

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

return cell;

} }

Here is the result: 结果如下:

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

You shouldn't release cellValue1 release and cellValue2 . 您不应该发布cellValue1 releasecellValue2 Getting them by calling objectAtIndex: does not retain them. 通过调用objectAtIndex:获取它们不会保留它们。

Here's an idea - perhaps the error is somewhere in here. 这是一个主意-也许错误在这里。

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;

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

OR 要么

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

In the first case, the program crashes when you try to assign a value to a null lblTemp1.txt. 在第一种情况下,当您尝试为空的lblTemp1.txt分配值时,程序崩溃。 In the second case, the program crashes because NSString* cellValue is null and it then tries to display the null string. 在第二种情况下,程序崩溃,因为NSString * cellValue为null,然后它试图显示空字符串。

You should be able to use the debugger to step through and figure out the values of these things as you go, and figure where the error is happening. 您应该能够使用调试器逐步了解这些事物的值,并确定错误发生的位置。

Given that your program initially works then dies when you scroll, I am wondering if the cellArray1 and cellArray2 where you are drawing your values from actually have as much data as the number of lines you have asked the scrollView it to display. 假设您的程序最初可以运行,然后在滚动时终止,所以我想知道从中绘制值的cellArray1和cellArray2是否实际上具有与要求scrollView显示的行数一样多的数据。 Maybe you have less data than the number of lines you have specified in the TableViewDatasource? 也许您的数据少于TableViewDatasource中指定的行数?

The data is being released right after the tableView has been created so when you scroll, the app crashes. 在创建tableView之后立即释放数据,因此当您滚动时,应用程序崩溃。 Try to post all of the codes in .m file for the people to see the problem. 尝试将所有代码发布到.m文件中,以使人们可以看到问题。 I am sure it's not in the codes you posted. 我确定它不在您发布的代码中。

How do you add the data in cellArray1 and cellArray2. 如何在cellArray1和cellArray2中添加数据。 Can you please post the code where you populate these arrays? 您能否将代码发布到填充这些数组的位置?

It could be the case that you are over-releasing the NSStrings you are adding to the cellArrays. 可能是您过度释放要添加到cellArrays的NSString。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM