ios - 保存在 TableView 中生成的随机数

标签 ios objective-c uitableview

我的应用程序当前正在生成随机数(请参阅下面的代码)。我想要的是在用户点击“保存”按钮后保存该数字并将其显示在表格 View 上。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    [self.clickyButton setTitle:@"Generate" forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (IBAction)handleButtonClick:(id)sender {
    // Generates numbers 1 to 49, without creating duplicate numbers.
    NSMutableSet * numberSet = [NSMutableSet setWithCapacity:5];
    while ([numberSet count] < 7 ) {
        NSNumber * randomNumber = [NSNumber numberWithInt:(arc4random() % 49 + 1)];
        [numberSet addObject:randomNumber];
    }

    NSArray * numbers = [numberSet allObjects];


    self.n1.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:0]];
    self.n2.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:2]];
    self.n3.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:3]];
    self.n4.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:4]];
    self.n5.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:5]];
    self.n6.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:6]];
}






@end

请解释一下如何将其保存在表格 View 中。 xcode初学者。

最佳答案

您应该创建一个在整个类范围内可访问的变量,而不仅仅是特定的 -handleButtonClick: 方法,然后将生成的数字添加到该变量 - 数组、集合等...

从那里,您可以实现 TableView 以通过 var[indexPath.row] 从变量读取值(假设它是一个数组),并显示它。一旦数组填充了对象,您将需要调用 [tableView reloadData]; 以确保 tableview 显示数据。

关于ios - 保存在 TableView 中生成的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326280/

相关文章:

ios - 如何使用 "generateCGImagesAsynchronously"从视频中加载图像序列?

objective-c - 如何将 NSMutableArray 中的元素放入 C char 中?

iphone - TableView 不会显示提供的数据

ios - 在 Xcode 中检测后台应用程序启动以调试报亭应用程序

ios - 如何在每个 View Controller 中使用变量?

ios - 选择新单元格后,Xcode 仅记录先前选择的表格 View 单元格

ios - 基本注册页面 - iOS

ios - 如何找到哪个 textField 加载了 TableView

iphone - 在为 iPhone UITextView 输入时建议 # 标签(如 Twitter)

iphone - CGContext 错误/警告