将整数值设置为indexpath.row

时间:2013-12-11 16:45:17

标签: ios uitableview

我正在尝试制作测验计划。当用户单击正确答案时,程序运行正常,但如果用户单击错误答案,程序必须显示正确答案,程序知道哪个单元格正确但无法调用该方法。

这是我的项目的一部分;

-(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];

感谢您的关注。

1 个答案:

答案 0 :(得分:3)

NSIndexPath.row是只读的,因此您需要从现有对象中创建一个新的NSIndexPath对象,如下所示:

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