ios - 为 indexpath.row 设置一个整数值

标签 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];

感谢您的关注。

最佳答案

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

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

关于ios - 为 indexpath.row 设置一个整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20524943/

相关文章:

ios - 使用 Storyboard 而不是 Nib 时如何解决此问题? : Getting “this class is not key value coding-compliant"?

ios - 在检索已更改的 CloudKit 记录时识别记录更改的类型

iphone - 在iOS上使用后台任务进行UI更新

ios - 编辑模式 UITableView,无法阻止内容 View 向右移动

objective-c - 上传图片到 Dropbox

ios - 如何在 SASlideMenu 中默认设置选定的第一个单元格?

ios - 创建数学视频库应用程序。需要有关如何存储数据的建议

objective-c - 我的 UINavBar 不响应触摸,并随 tableview 滚动

ios - UI View 框架在 UItableview ios 中不刷新

Iphone 应用程序在 iOS 4.3 中因 EXC_BAD_ACCESS 错误而崩溃,但在以前的版本中运行良好