-1

我有一个问题:我想在 tableview 中获取 UIButton 的索引。我创建的 uitableview 每行有 2 个 uibutton,但是当我点击 uibutton 时,它的索引不正确。代码创建 UITableview :

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"AllItemsArrayarray: %@", temp);
    return [temp count];
}

cellForRowAtIndexPath功能

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
        [market setFrame:CGRectMake(200, 6, 30, 30)];
        [market setTag:4000];
        [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
        [cell.contentView addSubview:market];
    }

    UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
  if([sellingArray count]>0)
{
    NSLog(@"sellingArray %@",sellingArray);
    if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing
    {

        [marketButton setSelected:NO];
        [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
        marketButton.enabled = YES;

    }
    else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"])  // marketplace
    {
        [marketButton setSelected:YES];
        [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
        marketButton.enabled = YES;

    }
}

    return cell;
}

marketPressedAction功能

- (void)marketPressedAction:(id)sender
{
    buttonPressed = (UIButton *)sender;
    buttontag = buttonPressed.tag ;
    NSLog(@"Market button click at row %d",buttontag);
}

当表格有 5 行时,我点击按钮,它显示错误:

-[__NSArrayM objectAtIndex:]: index 4000 beyond bounds [0 .. 4]'
4

2 回答 2

1

根据您在问题下方提出的评论,我认为您正在尝试根据在表格视图中单击哪个按钮来访问数组中的项目。首先,您将按钮的标签设置为 4000,然后当您尝试在单击的按钮标签的索引处访问数组中的项目时,显然它将超出范围,因为您正在尝试访问该对象在索引 4000 的数组中,而数组只有 5 个项目。

为了通过您的方法实现这一点,您的按钮的标签应该等于特定单元格所代表的对象的索引。所以而不是

[market setTag:4000];

你应该这样做:

[market setTag:indexPath.row];

然后,当您在按钮的选择器方法中访问标签时,它将与单元格表示的对象的索引相同。

于 2013-08-23T05:01:31.883 回答
0

您使用 indexpath.row 设置标签

 UIButton *market ;

       if (nil == cell) 
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
           }

              market = [UIButton buttonWithType:UIButtonTypeCustom];
                [market setFrame:CGRectMake(200, 6, 30, 30)];
                [market setTag:indexPath.row];
                [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
             if([sellingArray count]>0)
       {
    NSLog(@"sellingArray %@",sellingArray);
    if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing
    {

        [market setSelected:NO];
        [market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
        market.enabled = YES;

    }
    else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"])  // marketplace
    {
        [market setSelected:YES];
        [market setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
        market.enabled = YES;

    }
                [cell.contentView addSubview:market];
于 2013-08-23T05:14:44.633 回答