How to get index of UIButton that viewwithtag in uitableview ios

左心房为你撑大大i 提交于 2020-01-07 09:20:51

问题


I have a problem: I want to get index of UIButton in tableview. I created uitableview has 2 uibutton in each row, but when i click on uibutton, it get index incorrect. Code create UITableview :

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

cellForRowAtIndexPath function

    - (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;
}

And marketPressedAction function

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

When table has 5 row, I click on button, it shows error:

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

回答1:


you set tag using 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];



回答2:


Based on the comments you put below the question, I think you are trying to access the item in your array based on which button is clicked in table view. First of all, you are setting the tag of your button to 4000, then when you try to access the item from your array at index of the tag of button clicked, obviously it will be out of bounds because you are trying to access the object in the array at index 4000 whereas the array has only 5 items.

To achieve that with your approach, your button should have the tag equal to the index of object that particular cell is representing. So instead of

[market setTag:4000];

you should do this instead:

[market setTag:indexPath.row];

Then when you access the tag in your button's selector method, it will be the same as the index of object that cell represented.



来源:https://stackoverflow.com/questions/18394721/how-to-get-index-of-uibutton-that-viewwithtag-in-uitableview-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!