【问题标题】:How to access an int from a NSArray on the selected row?如何从选定行上的 NSArray 访问 int?
【发布时间】:2014-06-17 19:18:00
【问题描述】:

对不起,如果标题听起来令人困惑。但我正在尝试制作一个具有表格视图的应用程序。每行都有自己的按钮和自己的一组点。我非常希望按钮将特定数量的点添加到计数器。但是,我不知道如何访问所选行的 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???**


}

【问题讨论】:

    标签: ios objective-c nsarray


    【解决方案1】:

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

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

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

    另外,对于行标签,只需粘贴uilabel即可,对于(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,根据数组值设置标签。事实上,您甚至不需要声明两个相同的数组,只需重新使用同一个数组即可。

    【讨论】:

    • 感谢您和 scottdev!我改用这种方法。看起来更简单了。谢谢!!
    • 我们都必须从某个地方开始。乐意效劳。 (不要忘记接受答案:)
    【解决方案2】:

    NSArray 保存对象,而不是值。在您的情况下,它包含 NSNumber 对象,而这些对象又包含值。

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

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

    一个问题是您从哪里获得 indexPath.row 以及它在那时是否有效。

    【讨论】:

    • 其实他的数组里面有NSString对象。
    • @OwenHartnett 不,tablePoints 数组包含 NSNumber 对象。
    • 我的错误,我只看到了 tablePointLabel 数组并假设他正在使用它。数组包含重复的值 - 他只需要一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多