2013-12-11 29 views
0

我正在嘗試測驗程序。當用戶點擊正確的答案時,程序工作正常,但如果用戶單擊錯誤的答案,程序必須顯示正確的答案,程序知道哪個單元是正確的,但它不能調用該方法。設置一個整數值到indexpath.row

這是我的項目的一部分;

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 

CGRect rect = cell.frame; 
UIView * view = [[UIView alloc] init]; 
UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ListGray"] ]; 

databaseNameUser = SQL; 
NSArray *documentPathsUser = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirUser = [documentPathsUser objectAtIndex:0]; 
databasePathUser = [documentsDirUser stringByAppendingPathComponent:databaseNameUser]; 
sqlLocal= [[Sqlite alloc]initWithFile:databasePathUser]; 


NSString *query1 = [NSString stringWithFormat: @"SELECT AnswerID from tblAnswer where QuestionID = %i",[[questionsID objectAtIndex:i] intValue]]; 
answerInfo=[sqlLocal executeQuery:query1]; 
NSMutableArray *answerIDArray = [[NSMutableArray alloc]init]; 

for (NSMutableDictionary *d in answerInfo) { 

    [answerIDArray addObject:[d valueForKey:@"AnswerID"]]; 

} 


NSString *query2 = [NSString stringWithFormat: @"SELECT answerID from tblDogruCevaplar where QuestionID = %i",[[questionsID objectAtIndex:i] intValue]]; 
NSArray *answerInfo2=[sqlLocal executeQuery:query2]; 
//NSMutableArray *answerDogru = [[NSMutableArray alloc]init]; 

for (NSMutableDictionary *d in answerInfo2) { 

    answerDogruDefault=[d valueForKey:@"answerID"]; 

} 


for (int indexOfArray = 0; indexOfArray<4; indexOfArray++){ 



    if ([answerDogruDefault intValue] == [[answerIDArray objectAtIndex:indexOfArray] intValue]){ 

     //NSLog(@"indexpath = %i",indexOfArray); 

     indexPathInt = indexOfArray; 

    } 
} 



BOOL selx; 
if (answerType==2) 
    selx = [self isSelected:indexPath]; 
else 
    selx = [lastIndexPath isEqual:indexPath]; 

if (selx) { 
    back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SecildiYesilTikli2.png"] ]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    [view addSubview:back]; 
    cell.backgroundView = view; 


    if ([self isAnswerCorrect]) { 

     if (indexPathInt == indexPath.row) { 

      [NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(dogruCevapSignal:) userInfo:cell repeats:NO]; 
     } 

    }else{ 

     cell.textLabel.textColor = [UIColor redColor]; 

     NSIndexPath *a = [NSIndexPath indexPathForRow:indexPathInt inSection:0]; 

     NSLog(@"a = %i",a.row); 

     cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPathInt inSection:0]]; 

     [NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(dogruCevapSignal:) userInfo:cell repeats:NO]; 

    } 

    back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12); 
} else { 
    cell.accessoryView = nil; 
    if (lastIndexPath != nil){ 
     cell.textLabel.textColor = [UIColor blackColor]; 
     [view addSubview:back]; 
     cell.backgroundView = view; 

     back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12); 
     back = nil; 
    } 
} 
[view addSubview:back]; 
cell.backgroundView = view; 

back.frame = CGRectMake(0, 12,rect.size.width,rect.size.height-12); 

} 

-(void)dogruCevapSignal:(NSTimer *)cell{ 

UITableViewCell *defaultCell=(UITableViewCell *)[cell userInfo]; 

[UIView animateWithDuration:0.1f delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{ 
    [UIView setAnimationRepeatCount:3]; 
    defaultCell.alpha = 0; 
} completion: ^(BOOL finished) { 
    defaultCell.hidden = YES; 

    [UIView animateWithDuration:0.1 animations:^{ 
     defaultCell.alpha = 1; 
    } completion: ^(BOOL finished) { 
     defaultCell.hidden = NO; 
     defaultCell.textLabel.textColor = [UIColor blueColor]; 
    }]; 

}]; 
} 

我不能在錯誤的答案中調用此方法。

[NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(dogruCevapSignal:) userInfo:cell repeats:NO]; 

謝謝您的關注。

+3

它不是立即清楚你的問題是什麼。嘗試更具體地瞭解您需要幫助的地方。 – Snowman

+0

例如在我的問題中,我有4個答案,當用戶點擊錯誤的答案時,我想向用戶顯示正確的答案,所以我想要閃爍正確的答案單元格,但正如你可以看到上面的方法沒有調用。對不起沒有好英語,我希望你能明白我想要做什麼。 –

回答

3

NSIndexPath.row是隻讀的,所以你需要從現有做出新的NSIndexPath對象,像這樣:

NSIndexPath *original = ... 
int newRow = ...; 
NSIndexPath *changedRow = [NSIndexPath 
    indexPathForRow:newRow    // Use the new row 
      inSection:original.section]; // Use the original section 
+0

我試過了,但我得到了同樣的錯誤,如果用戶點擊第二個單元格indexpath.row = 1,我想使用indexpath到達我的第一個單元格。 cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPathInt inSection:indexPath.row]];我寫這個NSLog(@「cellString =%@」,cell.textLabel.text);但細胞文本即將'空'。 –

+0

@ TwentyThr23它應該是'cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPathInt inSection:indexPath.section]](節,而不是結尾的行)。 – dasblinkenlight

+0

好吧,我找到解決方案,你是對的,非常感謝你的興趣,問題是關於我錯誤的算法。但我修好了,再次感謝。 –