2011-03-07 129 views
1

我使用數據加載UITableView,請參閱下面的代碼,並且所有代碼都最初運行良好。但是,當我滾動表視圖它崩潰。我無法找到dealloc問題在哪裏,並想知道如果某個好的和有經驗的人可以給我一個提示?UITableView在滾動時崩潰

我已經用完整的代碼更新了這篇文章,包括數組的加載。

'保留'(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 
+0

getCellContentView是什麼樣的? – blindjesse 2011-03-07 23:55:30

回答

1

你不應該釋放cellValue1 releasecellValue2。通過致電objectAtIndex:獲取它們不會保留它們。

+0

嗨,謝謝你回答維京.... Oppps我改變了代碼,並忘記刪除這些行,但仍然沒有釋放相同的promlem。 – PeterK 2011-03-07 23:45:10

+0

您應該包含getContentView方法的代碼。 – 2011-03-07 23:53:20

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]爲空。

在第一種情況下,當您嘗試將值分配給空lblTemp1.txt時,程序崩潰。 在第二種情況下,程序崩潰,因爲NSString * cellValue爲空,然後嘗試顯示空字符串。

您應該能夠使用調試器逐步找出這些事情的值,並找出錯誤發生的位置。

鑑於您的程序最初工作,然後死於滾動時,我想知道,您正在繪製您的值的cellArray1和cellArray2實際上具有儘可能多的數據作爲您要求scrollView它顯示的行數。也許你的數據少於你在TableViewDatasource中指定的行數?

+0

看到更新 – PeterK 2011-03-08 01:03:07

+0

我有正確的行數,因爲我顯示。 – PeterK 2011-03-08 01:10:59

0

數據正在創建tableView後立即釋放,所以當您滾動時,應用程序崩潰。嘗試發佈.m文件中的所有代碼以供人們查看問題。我相信它不在你發佈的代碼中。

0

如何在cellArray1和cellArray2中添加數據。你可以請你填寫這些數組的代碼嗎?

可能出現這種情況,您將過度釋放要添加到cellArrays中的NSString。