为 indexpath.row 设置一个整数值 [英] Set a integer value to indexpath.row

查看:16
本文介绍了为 indexpath.row 设置一个整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make quiz program. When user click the right answer, the program is working correctly, but if user click the wrong answer, program have to show the right answer, program knows which cell is correct but it can't call the method.

这是我的项目部分;

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

}];
}

我不能在错误的答案中调用这个方法.

i cant call this method in wrong answer.

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

感谢您的关注.

推荐答案

NSIndexPath.row 是只读的,所以你需要从一个新的 NSIndexPath 对象现有的,像这样:

NSIndexPath.row is read-only, so you need to make a new NSIndexPath object from an existing one, like this:

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

这篇关于为 indexpath.row 设置一个整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆