Ios 如何从选定行上的NSArray访问int?

Ios 如何从选定行上的NSArray访问int?,ios,objective-c,nsarray,Ios,Objective C,Nsarray,抱歉,如果标题听起来很混乱。但我正在尝试制作一个具有表视图的应用程序。每行都有自己的按钮和自己的点集。我非常希望这个按钮能为计数器添加特定数量的点数。但是,我不知道如何访问所选行的NSArray的int。代码如下: @interface FBViewController () @end @implementation FBViewController { NSArray *tablePoints; NSArray *tablePointLabel; int coun

抱歉,如果标题听起来很混乱。但我正在尝试制作一个具有表视图的应用程序。每行都有自己的按钮和自己的点集。我非常希望这个按钮能为计数器添加特定数量的点数。但是,我不知道如何访问所选行的NSArray的int。代码如下:

@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???**


}

NSArray保存的是对象,而不是值。在您的例子中,它保存NSNumber对象,而NSNumber对象又保存值

使用indexPath.row获取NSNumber,然后获取值

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

一个问题是您从何处获得indexath.row,它在那一点上有效吗。

有很多改进方法,包括rmaddy提供的建议

我的建议是,不要实例化一个uibutton,我猜它等于每个表行的高度/宽度

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

此外,对于行标签,只需插入uilabel,对于UITableViewCell*tableView:UITableView*tableView cellForRowAtIndexPath:NSIndexPath*indexPath,根据数组值设置标签。实际上,您甚至不需要声明两个相同的数组,只需重新调整同一个数组的用途。

实际上,他的数组包含NSString对象。@OwenHartnett不,tablePoints数组包含NSNumber对象。我的错误是,我只看到了tablePointLabel数组,并假设他正在使用它。数组包含重复的值-他真的只需要一个。感谢你和scottdev!我改用这个方法。这似乎更简单。非常感谢。我们都必须从某个地方开始。很乐意帮忙。别忘了接受答案: