这是我的代码:
- (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);
}
答案 0 :(得分:1)
你得到的是随机问题,因为每次你滚动并且新细胞变得可见时,你会掷骰子并产生另一个问题。
您应该预先生成所有随机问题,而不是cellForRowAtIndexPath
方法。
因此,您应该生成一个随机数组,引用viewDidLoad
处的问答数组,与表中的单元格相同。然后在cellForRowAtIndexPath
中,访问该数组中的数字并配置单元格以显示该问题。
这样你就会有不变的问题!
代码有点混乱,但实际上,我们希望将生成逻辑的问题移到viewDidLoad
,创建一个数组来保存这些东西,然后从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);
}
注意,我在标题中声明firstNumbers
,lastNumbers
为NSMutableArray
,并假设您引用了tableView
,名为tv
。最后,我假设你的表中只有一个部分。希望这有帮助!