简体   繁体   English

如何从所选行的NSArray访问int?

[英]How to access an int from a NSArray on the selected row?

Sorry if the title sounds confusing. 抱歉,标题听起来令人困惑。 But I'm trying to make an app that has a table view. 但是我正在尝试制作一个具有表视图的应用程序。 Each row has its own button with its own set of points. 每行都有自己的按钮,并带有自己的一组点。 I pretty much want the button to add the specific amount of points to a counter. 我非常希望按钮将特定数量的点添加到计数器。 However, I don't know how to access an int of an NSArray of the selected row. 但是,我不知道如何访问所选行的NSArray的int。 Here is the code: 这是代码:

@interface FBViewController ()

@end

@implementation FBViewController {

    NSArray *tablePoints;
    NSArray *tablePointLabel;
    int counter;

}

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

    //A table that displays the points on the buttons
    tablePointLabel = [NSArray arrayWithObjects:
                   @"10",
                   @"10",
                   @"10",
                   @"20",
                   @"20",
                   @"20",
                   @"30",
                   @"30",
                   @"50",
                   @"70",
                   @"100",
                   @"300",
                   @"300",
                   nil];

    //table of ints (points)
    tablePoints = [NSArray arrayWithObjects:
                   @10,
                   @10,
                   @10,
                   @20,
                   @20,
                   @20,
                   @30,
                   @30,
                   @50,
                   @70,
                   @100,
                   @300,
                   @300,
                   nil];

    counter = 0;

}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [tableData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"FBCell";

    FBCell *cell = (FBCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FBCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }


    UIButton *pointButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pointButton.frame = CGRectMake(250.0f, 5.0f, 75.0f, 30.0f);
    [pointButton setBackgroundColor:[UIColor greenColor]];
    [pointButton setTitle:[tablePointLabel objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    [cell addSubview:pointButton];
    [pointButton addTarget:self
                    action:@selector(addPoint:)
              forControlEvents:UIControlEventTouchUpInside];


    return cell;
}

-(IBAction)addPoint:(id)sender {


    counter + [tablePoints objectAtIndex:indexPath.row] = counter; 
    **//This is where I have an issue. indexPath.row does not work for int arrays. what do i do???**


}

A NSArray holds objects, not values. NSArray保存对象,而不保存值。 In your case it holds NSNumber objects which in turn holds the values. 在您的情况下,它包含NSNumber对象,这些对象又包含值。

Use the indexPath.row to get the NSNumber and then get the value. 使用indexPath.row获取NSNumber,然后获取值。

NSNumber *numberObject = [tablePoints objectAtIndex:myIndex];
int myInt = [numberObject intValue]; // NSNumber covers a range of different types of values

One question is where you're obtaining your indexPath.row and is it valid at that point. 一个问题是,您在哪里获取indexPath.row,并且在那时是否有效。

Lots of ways to improve this (including suggestions offered by rmaddy) 许多改善方法(包括rmaddy提供的建议)

My recommendation is instead of instantiating a uibutton (that I'm guessing is equal to the height/width of each table row, is to implement 我的建议是不要实例化uibutton(我猜它等于每个表行的高度/宽度,而是实现

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//increments counte
counter += [tablePoints[indexPath.row] intValue];
}

Moreover, for the row label, just stick in uilabel, and for (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, set the label according to the array value. 此外,对于行标签,只需粘贴uilabel,对于(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,请根据数组值设置标签。 In fact, you don't even need to declare two identical arrays, just repurpose the same one. 实际上,您甚至不需要声明两个相同的数组,只需重新使用相同的数组即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM