简体   繁体   中英

UITableView crashes when scrolling

I load a UITableView with data, see code below, and all works well initially. 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.

I get the following message:

[CFString isEqualToString:]: message sent to deallocated instance 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]; // 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 . Getting them by calling objectAtIndex: does not retain them.

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

OR

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

In the first case, the program crashes when you try to assign a value to a null lblTemp1.txt. In the second case, the program crashes because NSString* cellValue is null and it then tries to display the null string.

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. Maybe you have less data than the number of lines you have specified in the TableViewDatasource?

The data is being released right after the tableView has been created so when you scroll, the app crashes. Try to post all of the codes in .m file for the people to see the problem. I am sure it's not in the codes you posted.

How do you add the data in cellArray1 and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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