i have a tableView contains a couple af answers, the user will select an answer, if the answer is true the selected cell will be colored by green, else : wrong answer, two cells will be coloured: the selected by red color and the right by green color.
My problem is that i couldn’t change the value of indexPath by the index of val1 to locate the right cell.
here is my tableView tableView:didSelectRowAtIndexPath method :
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSNumber *value = [truerep objectAtIndex:0];
NSUInteger val1 = [value integerValue];
NSUInteger val2 = [indexPath row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (val1==val2) {//right answer so the color of the selected cell will be green
cell.contentView.backgroundColor = [UIColor greenColor];
}else {//wrong answer so 2 cells will be colored
//the color of the selected cell will be red and the right cell will be green
cell.contentView.backgroundColor = [UIColor redColor];
// idk What to do here to change the value of indexpath by val1
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Why want to do that (changing the indexPath value) ?
This method method is called whenever the user selects a row in the table view, and
[indexPath row]will give you that row index.The problem probably comes from the way you store the indexes of the true answers in
truereparray, unable to compare direct row index withval1.I don’t know what
truerepand[truerep objectAtIndex:0]are suppose to contain, but in your example,val1looks like the row index for the correct answer, and it doesn’t correspond to the real right answer row index.Also, if you want the two cells to be colored, you will have to change a bit your code.
Here, with the
if/else, you only get one colored when the user selects a row.EDIT based on your comments
You probably want to loop over all row and determine which are to be colored in red and green. Here is an example :
You surely know the value for
totalRowsCount..