【问题标题】:How can I understand that which tableViewCell contains the button that is clicked? [duplicate]我如何理解哪个 tableViewCell 包含被点击的按钮? [复制]
【发布时间】:2013-01-07 08:05:45
【问题描述】:

可能重复:
How to know the UITableview row number

我在这个 tableView 上有一个 tableView 和一个 tableViewCell。我为 tableViewCell 定义了另一个名为 CustomCell 的类,以便编写必要的自定义代码并创建一个按钮(在此单元格上)。单击 tableViewCell 上的按钮时,我想了解哪个 tableViewCell 包含该按钮,以便我可以仅对该单元格(包含单击的按钮)进行必要的更改

如何理解哪个 tableViewCell 包含被点击的按钮?

【问题讨论】:

  • 给每个按钮加标签怎么样?

标签: iphone objective-c uitableview uibutton


【解决方案1】:

实现这一点的一种策略是将带有行号的标签分配给按下的按钮:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexpath {
    YourCustomCell *cell = // Code
    cell.button.tag = indexPath.row;

    // Other cell preparation code

    return cell;
}

然后,在按钮的action中,可以看到它的tag是什么,来判断对应的是什么模型对象:

- (void)didSelectButton:(id)sender {
    UIButton *button = sender;
    NSInteger tag = button.tag;

    YourModelObject *item = self.items[tag];

    //    Continue with the business logic for what should
    //    happen when that button is pressed.
}

【讨论】:

  • 标签运行良好,直到您开始删除单元格。然后突然标签不再对应正确的模型对象。
【解决方案2】:

如果您将附件详细信息按钮替换为自定义按钮,则可以调用附件ButtonTappedForRowWithIndexPath: 方法。

示例 - 将其放在 cellForRowAtIndexPath 中(设置单元格时):

UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 125, 24)];
[myAccessoryButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
[myAccessoryButton release];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

然后作为单独的方法:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:    (NSIndexPath *)indexPath
{
    //do something
}

【讨论】:

  • 我会避免在该方法中创建任何不必要的对象,方法是检查附件视图的存在并仅在它是 nil 时创建它。
  • 很好,韦恩。完全同意!
【解决方案3】:

这个怎么样...

- (UITableViewCell *)cellWithSubview:(UIView *)subview {
    while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview;
    return (UITableViewCell *)subview;
}

- (IBAction)buttonClicked:(id)sender {
    UITableViewCell *theCell = [self cellWithSubview:sender];
}

【讨论】:

    【解决方案4】:

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

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    CGRect frame = CGRectMake(0.0, 10.0, 24, 24);
    button.frame = frame;
    [button setTag:((indexPath.section & 0xFFFF) << 16) |
     (indexPath.row & 0xFFFF)];
    [button setImage:[UIImage imageNamed:@"link-buttoni4.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"link-button-onclicki4.png"] forState:UIControlStateHighlighted];
    [button setSelected:NO];
    
    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    
    [button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
    

    }

    -(void)checkButtonTapped:(UIButton *)sender {

    NSUInteger section = ((sender.tag >> 16) & 0xFFFF);
    NSUInteger row     = (sender.tag & 0xFFFF);
    NSLog(@"Button in section %i on row %i was pressed.", section, row);
    

    }

    【讨论】:

      【解决方案5】:

      试试这个,
      您可以通过此方法知道单击的单元格的部分和行号:
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      }

      1>部分可以通过
      int sectionno = indexPath.section
      2>单元格索引可以通过
      int rowno = indexPath.row
      然后使用你可以得到这样的表格单元格,

      UITableViewCell *cell=[product_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowno inSection:sectionno]];
      

      【讨论】:

        【解决方案6】:

        你需要用这种方式调用这个cellForRowAtIndexPath方法。

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            static NSString *CellIdentifier = @"Cell";
        
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        
        
            button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(10 , 10, 200, 20);
            [button addTarget:self action:@selector(passRowValueMethod:) forControlEvents:UIControlEventTouchUpInside];
            [button setTag:indexPath.row];
            [button setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal];
            [cell addSubview:button];
        
        
            return cell;
        }
        

        然后定义按钮目标方法,并在该方法中获取标签值。

        -(void)passRowValueMethod:(UIButton*)sender
        {
            UIButton *buttonGet = sender;
            NSLog(@"%d", buttonGet.tag);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-16
          • 1970-01-01
          • 1970-01-01
          • 2022-08-19
          • 2019-12-09
          相关资源
          最近更新 更多