【问题标题】:Button inside a custom uitableviewcell not working自定义 uitableviewcell 内的按钮不起作用
【发布时间】:2013-07-22 10:59:39
【问题描述】:

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

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

【问题讨论】:

  • 什么是self?你的表格视图单元格?它有tableView 属性吗?当你NSLog(@"%@", self.tableView) 时会发生什么?
  • 请记住,您可以使用关联对象将 indexPath 直接关联到表格视图单元格。
  • @Vishnu 你试过我的答案了吗?
  • @Vishnu,你的代码是正确的。你能发布你的'cellForIndexPath'方法

标签: iphone ios objective-c ipad uitableview


【解决方案1】:
[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);
}

试试这个可能对你有帮助。

【讨论】:

  • @DharmbirChoudharythanx :)
  • @Divz 你能给我两个 NSLog(@"btn.tag %d",btn.tag);和 NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];...?两者都不一样..价值来了?
  • @NitinGohel 当您获取触摸区域的位置点时,它总是返回相同的值,因为它从超级视图返回值而不是从 tableview 并且超级视图是 UITableViewCell
  • 很好的答案,虽然我必须做一些修改才能使其工作
【解决方案2】:

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

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

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

【讨论】:

  • 非常感谢你,伙计!您的回答将引导我修复我的错误并节省许多时间浪费!
【解决方案3】:
- (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];

【讨论】:

  • 试过了,但它也总是返回零。我认为问题是我在自定义单元格中通过 Ib 添加了按钮如何解决????
  • 为我工作,谢谢@iPhone 6
【解决方案4】:

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

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

【讨论】:

  • 哦,现在我看到了你的问题,为此我看到了我的另一个链接选项来满足你的这个要求stackoverflow.com/questions/2863940/… 看到这个不同的答案..
  • 如果您为 UITableViewCell 创建整个 3 个文件,例如 .h、.m 和 xib,然后在 .m 类中,如果您使用自定义方法设置值,那么它适用于我使用不同的标签...我如果您使用此流程,则为客户要求创建该代码然后告诉我我将发布答案..
猜你喜欢
  • 2014-02-24
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多