-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);
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
puneeth
  • 3
  • 1
  • 5
  • What is your problem? – tuledev Dec 12 '15 at 02: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 – puneeth Dec 12 '15 at 02:40

1 Answers1

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!

Flying_Banana
  • 2,864
  • 2
  • 22
  • 38
  • 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 – puneeth Dec 12 '15 at 02:48
  • 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. – Flying_Banana Dec 12 '15 at 03:07
  • Thank you so much for the code. I will check on it now. – puneeth Dec 12 '15 at 03:28
  • 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 – puneeth Dec 12 '15 at 03:38
  • Its supposed to be `firstNumbers[indexPath.row]`. You are taking out your first number from the previously generated array. – Flying_Banana Dec 12 '15 at 04:11
  • it's displaying an error, use of undeclared identifier firstNumbers and advised to change it to firstNumber.. In the statement "int firstNumber= [firstNumbers[indexPath.row] intValue];" – puneeth Dec 12 '15 at 04:15
  • Yeah, I declared extra parameters. See the last paragraph. – Flying_Banana Dec 12 '15 at 04:15
  • And even this statement is throwing an error "firstNumbers = [NSMutableArray array];" when i change the code to NSMutableArray *firstNumbers = [NSMutableArray array]; it's not throwing an error. Is their anything wrong I making – puneeth Dec 12 '15 at 04:18
  • Yes, you didn't declare the parameters in the header as I suggested you should... You need somewhere to store the data. Therefore you need to declare new variables... – Flying_Banana Dec 12 '15 at 04:19
  • In the header as in? In the (void) viewDidLoad method? – puneeth Dec 12 '15 at 04:23
  • No. That would be a local variable. You need to declare an instance variable. Find either the first or the second [here](http://stackoverflow.com/questions/18616709/instance-variables-in-interface-header-vs-implementation) and put those names in. – Flying_Banana Dec 12 '15 at 04:25
  • I did include it in the same way in .h file. still the error remains same – puneeth Dec 12 '15 at 04:33
  • Can you please help with that – puneeth Dec 12 '15 at 04:45
  • . h file #import @interface MathViewController : UITableViewController { NSMutableArray* mathcal; NSMutableArray *arSelectedRows; NSMutableArray* answer; IBOutlet UIBarButtonItem *firstCountDownLabel1; NSTimer *firstCountDownTimer1; bool timerActive; int secondsCount; NSMutableArray *finalanswers; int firstNumbers; int lastNumbers; } – puneeth Dec 12 '15 at 05:18
  • @interface MathViewController : UITableViewController { NSMutableArray *finalanswers; int firstNumbers; int lastNumbers; } – puneeth Dec 12 '15 at 05:19
  • Change `firstNumbers` and `lastNumbers` types to `NSMutableArray`...you can't just copy the code, you have to understand what I'm doing here to solve the problem... – Flying_Banana Dec 12 '15 at 06:13
  • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 200; } It showing error index 0 beyond bounds for empty array' – puneeth Dec 12 '15 at 20:07