相关疑难解决方法(0)

检测在UITableView中按下了哪个UIButton

我有一个UITableView5 UITableViewCells.每个单元格包含一个UIButton设置如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil) {
         cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
         [cell autorelelase];

         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
         [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
         [button setTag:1];
         [cell.contentView addSubview:button];

         [button release];
     }

     UIButton *button = (UIButton *)[cell viewWithTag:1];
     [button setTitle:@"Edit" forState:UIControlStateNormal];

     return cell;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:在buttonPressedAction:方法中,我如何知道按下了哪个按钮.我考虑过使用标签,但我不确定这是最好的路线.我希望能够以某种方式标记indexPath到控件上.

- (void)buttonPressedAction:(id)sender
{
    UIButton *button = (UIButton …
Run Code Online (Sandbox Code Playgroud)

iphone uibutton uitableview ios

211
推荐指数
8
解决办法
13万
查看次数

自定义uitableviewcell内的按钮不起作用

我有一个自定义的TableCell,UiButton通过IB与此相关联.但是按钮操作方法总是将按钮索引返回为零

以下是我的代码

//Inside CellForRow
[cell.M_CtrlBtnChat addTarget: self
                       action: @selector(buttonPressed:withEvent:)
             forControlEvents: UIControlEventTouchUpInside];


- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{

    UITouch * touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
    NSLog(@"%d", indexPath.row);
}
Run Code Online (Sandbox Code Playgroud)

索引路径始终返回零.任何的想法 .请帮我

iphone objective-c uitableview ipad ios

5
推荐指数
2
解决办法
4902
查看次数

标签 统计

ios ×2

iphone ×2

uitableview ×2

ipad ×1

objective-c ×1

uibutton ×1