6

我有一个自定义 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);
}

这里索引路径总是返回零。任何想法 。请帮我

4

4 回答 4

5
[cell.M_CtrlBtnChat addTarget: self
                       action: @selector(buttonPressed:withEvent:)
             forControlEvents: UIControlEventTouchUpInside];
 cell.M_CtrlBtnChat.tag = indexPath.row;


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

    UIButton *btn = (UIButton *)sender;
    NSLog(@"btn.tag %d",btn.tag);
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
    NSLog(@"%d", indexPath.row);
}

试试这个它可能会帮助你。

于 2013-07-22T11:06:43.917 回答
2

我遇到了同样的问题并尝试了一切。最终做到的是实现方法

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

只需返回自定义单元格的高度(查看 NIB 文件)

于 2013-09-07T22:43:01.407 回答
1
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
     CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.mMessagesTable];
     NSIndexPath *indexPath = [self.mMessagesTable indexPathForRowAtPoint:touchPoint];
     NSLog(@"%d", indexPath.row);
}

以编程方式的按钮代码

UIButton *mNewMsgDwn = [UIButton buttonWithType:UIButtonTypeCustom];
[mNewMsgDwn setImage:[UIImage imageNamed:@"new_message.png"] forState:UIControlStateNormal];
[mNewMsgDwn addTarget:self action:@selector(newMsg) forControlEvents:UIControlEventTouchUpInside];
mNewMsgDwn.frame = CGRectMake(179, 357, 137, 27);
[self.view addSubview:mNewMsgDwn];
于 2013-07-22T11:05:41.550 回答
0

使用下面的代码尝试一下..

- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSLog(@"%d", indexPath.row);
}

或者

UIButton *button = (UIButton *)sender;
CGRect buttonFrame = [button convertRect:button.bounds toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonFrame.origin];

以编程方式在单元格中添加自定义按钮的整个示例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
         .......
    cell.textLabel.text = [NSString stringWithFormat:@"Cell #%i", indexPath.row + 1];

    //Create the button and add it to the cell
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(customActionPressed:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Custom Action" forState:UIControlStateNormal];
    button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
    [cell addSubview:button];

    return cell;
}

并使用以下代码获取索引路径...

//Get the superview from this button which will be our cell
UITableViewCell *tblCell = (UITableViewCell*)[sender superview];

//From the cell get its index path.
NSIndexPath *indexPathE = [myTableView indexPathForCell:tblCell];
NSLog(@"%d", indexPathE.row);
于 2013-07-22T11:45:48.190 回答