-1

Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdent = @"mycal";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    yes=(UIButton *)[cell viewWithTag:1];
    no=(UIButton *)[cell viewWithTag:2];

  /*  if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];


    // Configure the cell...
    cell.textLabel.text=[mathcal objectAtIndex:indexPath.row];
    if([arSelectedRows containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }*/

    int firstNumber=0,lastNumber = 0,calculation,ans=0;
    int todisplay= [self getRandomNumberBetween:0 to:1];
    NSLog(@"to display:::%d",todisplay);
    if(finalanswers==nil)
    {
        finalanswers=[[NSMutableArray alloc]init];
    }
    [finalanswers addObject:[NSNumber numberWithInt:todisplay]];
        NSLog(@"finalanswers::::%@",finalanswers);
    calculation=[self getRandomNumberBetween:0 to:3];
    NSLog(@"cat value %@",mathCat);
    if ([mathCat isEqualToString:@"Easy"]) {
        firstNumber = [self getRandomNumberBetween:1 to:9];
        lastNumber = [self getRandomNumberBetween:1 to:9];
       // NSLog(@"firstNumber %d lastnumber %d",firstNumber,lastNumber);
    }
    else if ([mathCat isEqualToString:@"Medium"]){
        firstNumber = [self getRandomNumberBetween:10 to:99];
        lastNumber = [self getRandomNumberBetween:10 to:99];
       // NSLog(@"firstNumber %d lastnumber %d",firstNumber,lastNumber);
    }
    else if ([mathCat isEqualToString:@"Hard"]){
        firstNumber = [self getRandomNumberBetween:100 to:999];
        lastNumber = [self getRandomNumberBetween:100 to:999];
       // NSLog(@"firstNumber %d lastnumber %d",firstNumber,lastNumber);
    }

   // NSLog(@"calc value is -->  %@",[mathcal objectAtIndex:calculation]);




    //cell.textLabel.text = [NSString stringWithFormat: @"%d+%d=%d",firstNumber,lastNumber,todisplay];
    if (todisplay==0) {
        if (calculation==0) {
            ans=firstNumber+lastNumber;
            answer=[NSString stringWithFormat:@"%d + %d=%d",firstNumber,lastNumber,ans];

            cell.textLabel.text=answer;
            return cell;
        }
        else if (calculation==1){
            ans=firstNumber-lastNumber;
            answer=[NSString stringWithFormat:@"%d - %d=%d",firstNumber,lastNumber,ans];

            cell.textLabel.text=answer;
            return cell;
        }
        else if (calculation==2){
            ans=firstNumber*lastNumber;
            answer=[NSString stringWithFormat:@"%d * %d=%d",firstNumber,lastNumber,ans];

            cell.textLabel.text=answer;
            return cell;
        }
        else if (calculation==3){
            ans=firstNumber/lastNumber;
            answer=[NSString stringWithFormat:@"%d / %d=%d",firstNumber,lastNumber,ans];

            cell.textLabel.text=answer;
            return cell;
        }

    }
    else if (todisplay==1){
        int ans=[self getRandomNumberBetween:10 to:50];
        answer=[NSString stringWithFormat:@"%d+%d=%d",firstNumber,lastNumber,ans];
        cell.textLabel.text=answer;
        return cell;
    }

      return cell;
}
-(int)getRandomNumberBetween:(int)from to:(int)to {

    return (int)from + arc4random() % (to-from+1);
}
2
  • What is your problem? Commented Dec 12, 2015 at 2:37
  • I am generating random questions, but when i scroll up and comes back, the questions are changing. I want to make them static. only if the user exits from the application or finishes the game and starts a new game only then it should generate different questions Commented Dec 12, 2015 at 2:40

1 Answer 1

1

You are getting random questions because every time you scroll and a new cell becomes visible, you throw a dice and generate another question.

You should generate all the random questions beforehand, instead of in the cellForRowAtIndexPath method.

So you should generate an array of random numbers that refers to your question and answer array at viewDidLoad, same number as the cells in your table. Then in cellForRowAtIndexPath, access the number in that array and configure the cell to display that question.

This way you will have non-changing questions!

The code is a little bit messy, but essentially, we want to move the question generating logic into viewDidLoad, create an array to hold those things, then read it from the cellForRowAtIndexPath.

- (void)viewDidLoad {
  // GENERATING RANDOM QUESTIONS
  firstNumbers = [NSMutableArray array];
  lastNumbers = [NSMutableArray array];

  for (int i = 0; i < [tv numberOfRowsInSection:0]; i++) {
    int firstNumber, lastNumber;
    if ([mathCat isEqualToString:@"Easy"]) {
      firstNumber = [self getRandomNumberBetween:1 to:9];
      lastNumber = [self getRandomNumberBetween:1 to:9];
      // NSLog(@"firstNumber %d lastnumber %d",firstNumber,lastNumber);
    }
    else if ([mathCat isEqualToString:@"Medium"]){
      firstNumber = [self getRandomNumberBetween:10 to:99];
      lastNumber = [self getRandomNumberBetween:10 to:99];
      // NSLog(@"firstNumber %d lastnumber %d",firstNumber,lastNumber);
    }
    else if ([mathCat isEqualToString:@"Hard"]){
      firstNumber = [self getRandomNumberBetween:100 to:999];
      lastNumber = [self getRandomNumberBetween:100 to:999];
      // NSLog(@"firstNumber %d lastnumber %d",firstNumber,lastNumber);
    }

    [firstNumbers addObject:[NSNumber numberWithInt:firstNumber]];
    [lastNumbers addObject:[NSNumber numberWithInt:lastNumber]];
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSString *cellIdent = @"mycal";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
  yes=(UIButton *)[cell viewWithTag:1];
  no=(UIButton *)[cell viewWithTag:2];

  int firstNumber= [firstNumbers[indexPath.row] intValue];
  int lastNumber = [lastNumbers[indexPath.row] intValue];

  int calculation, ans=0;
  int todisplay= [self getRandomNumberBetween:0 to:1];
  NSLog(@"to display:::%d",todisplay);
  if(finalanswers==nil) {
    finalanswers=[[NSMutableArray alloc]init];
  }

  [finalanswers addObject:[NSNumber numberWithInt:todisplay]];
  NSLog(@"finalanswers::::%@",finalanswers);
  calculation=[self getRandomNumberBetween:0 to:3];
  NSLog(@"cat value %@",mathCat);


  // NSLog(@"calc value is -->  %@",[mathcal objectAtIndex:calculation]);

  // REMOVED GENERATION TO VIEW DID LOAD

  //cell.textLabel.text = [NSString stringWithFormat: @"%d+%d=%d",firstNumber,lastNumber,todisplay];
  if (todisplay==0) {
    if (calculation==0) {
      ans=firstNumber+lastNumber;
      answer=[NSString stringWithFormat:@"%d + %d=%d",firstNumber,lastNumber,ans];

      cell.textLabel.text=answer;
      return cell;
    }
    else if (calculation==1){
      ans=firstNumber-lastNumber;
      answer=[NSString stringWithFormat:@"%d - %d=%d",firstNumber,lastNumber,ans];

      cell.textLabel.text=answer;
      return cell;
    }
    else if (calculation==2){
      ans=firstNumber*lastNumber;
      answer=[NSString stringWithFormat:@"%d * %d=%d",firstNumber,lastNumber,ans];

      cell.textLabel.text=answer;
      return cell;
    }
    else if (calculation==3){
      ans=firstNumber/lastNumber;
      answer=[NSString stringWithFormat:@"%d / %d=%d",firstNumber,lastNumber,ans];

      cell.textLabel.text=answer;
      return cell;
    }

  }
  else if (todisplay==1){
    int ans=[self getRandomNumberBetween:10 to:50];
    answer=[NSString stringWithFormat:@"%d+%d=%d",firstNumber,lastNumber,ans];
    cell.textLabel.text=answer;
    return cell;
  }

  return cell;
}

-(int)getRandomNumberBetween:(int)from to:(int)to {
  return (int)from + arc4random() % (to-from+1);
}

Note, I declared firstNumbers, lastNumbers as NSMutableArray in the header, as well as assumed you have a reference to your tableView, named tv. Finally, I assumed you only have a single section in your table. Hope this helps!

Sign up to request clarification or add additional context in comments.

17 Comments

Thanks for the response. Can you please change the code and show it to me? i even tried that. But it's not working for me. It's throwing an error
Added the code. It's a little messy and I'm working with less information that I can here. Made a few assumptions. Hope it helps.
Thank you so much for the code. I will check on it now.
int firstNumber= [firstNumber[indexPath.row] intValue]; it's throwing an error in this statement, "Subscripted value is not an array, pointer or vector. What changes do i need to make
Its supposed to be firstNumbers[indexPath.row]. You are taking out your first number from the previously generated array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.