【问题标题】:Custom Cell button Click自定义单元格按钮单击
【发布时间】:2015-12-04 03:50:35
【问题描述】:

我的情况如下:

自定义单元格视图

1) 我在完全填充的自定义单元格视图中填充来自服务器的数据,我想在我的数组中存储一个链接存储,我想在浏览器中打开它,所以我想检查单击了哪个 index.row 按钮所以在那一行我可以获得url链接并在浏览器中打开它,到目前为止我已经找到了按钮标签的解决方案,但这也不起作用,就好像我们在点击屏幕上同时有两个单元格一样按钮两个按钮返回相同的标签。

2)作为附加的图像,我有另一个故事板,我想在其中切换到一个新的视图控制器,与上面提到的一样,我也想传递一个特定的帖子标题和键。

我希望一切都清楚。

提前致谢。

【问题讨论】:

  • 显示一些代码如何分配按钮标签?
  • @ChintaN-Maddy-Ramani cell.PostsButton.tag = indexPath.row;这是自定义单元格
  • @ChintaN-Maddy-Ramani 在按钮点击... - (IBAction)Custombuttonclick:(id)sender { int tagid = (int)cell.PostsButton.tag; NSLog(@"%d",tagid); }
  • 我建议使用委托模式。这是我在 Swift 中写的类似答案;适应目标 C 非常简单
  • 另一个答案供您参考:stackoverflow.com/questions/28733924

标签: ios objective-c uitableview


【解决方案1】:

回答您问题的第一部分。标签是一种痛苦。最好将 UIButton 作为发送者传递,并且在按钮的操作中,您可以这样说:

-(void) buttonPresse:(UIButton *)sender
{
    CGPoint location = [self.tableView convertPoint:sender.bounds.origin fromView:sender];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *cell = [self.table cellForRowAtIndexPath];
    //Do what you want with the cell or indexPath

}

【讨论】:

  • 它给了我一个错误:在“__strong id”类型的对象上找不到属性“边界”- (IBAction)Custombuttonclick:(id)sender { CGPoint location = [tableviewcustom convertPoint:sender.bounds.origin fromView:sender]; NSIndexPath *indexPath = [tableviewcustom indexPathForRowAtPoint:location]; UITableViewCell *cell = [tableviewcustom cellForRowAtIndexPath]; }
  • 动作需要在你的视图控制器子类中。并且 (id)sender 需要是 (UIButton *)sender
【解决方案2】:

我找到了按钮标签的解决方案,但也没有用

执行以下步骤来检测选择了哪个按钮。

  1. cellForRowAtIndexPath:方法中输入下面给出的代码sn-p。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // DEFINE CELL
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        // SET ACTION METHOD ON CALL BUTTON
        UIButton *button = (UIButton *) [self.view viewWithTag:40];
    
        [button setUserInteractionEnabled:YES];
    
        [button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventAllTouchEvents];
    
    }
    
  2. checkButtonTapped:添加以下方法。

    - (void)checkButtonTapped:(UIButton *)sender
    {
        // GET POINT OF THE BUTTON PRESSED
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    
        // GET INDEXPATH FOR THE CELL WHERE THE BUTTON IS PRESSED
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    
        if (indexPath != nil)
        {
            //Use indxPath.row to get item
            //Perform operations
        }
    }
    

如果要根据用户选择的单元格执行操作,还有另一种方法。

如果你想检查单元格的哪一行被点击了,你应该使用下面的方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(@"ShowindexPath%ld", (long)indexPath.row); }

在这里,我们检查单元格上的用户交互。 indexPath.row 将为用户选择的单元格返回 index 数字。您可以执行其他操作来代替NSLog 方法。

为了在执行 segue 时发送价值,请使用以下步骤:

  • NewViewController.h 文件中创建要执行segue 的属性,如下所示:

    @property (nonatomic) NSString* receivedValue;
    
  • @synthesize 属性,在@implementation@end 内的NewViewController.m 文件中写入以下代码

    @synthesize receivedValue;
    
  • NewViewController.h 文件导入TableViewController.m 文件中。

  • 转到TableViewController.m 文件中的prepareForSegue: 方法,您要从该方法发送值并根据其中的类名编写以下代码。

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NewViewController *nVC = [segue destinationViewController];
    [nVC setReceivedValue:sendValue];
    }
    

这里sendValue是你要发送的值。

就是这样。 构建并运行您的项目,尽情享受吧。

【讨论】:

  • NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; NSLog(@"ShowindexPath%@", indexPath);返回的 (nul)l
  • 点击按钮
  • 很抱歉给您带来不便。我已经更改了代码,它将为您提供所选单元格的索引。此代码 100% 有效。
  • didSelectRowAtIndexPath 将在我们单击表格视图单元格时被调用,在我的情况下,我想单击按钮
【解决方案3】:

您可以扩展 UITableViewCell 并为其添加一个 touple 属性。包含index.row 的键和包含 URL 的值,例如:- 1:"www.stackoverflow.com"。当您单击一个按钮时,您只需找出index.row,然后您就可以获得所需的值。

【讨论】:

    【解决方案4】:

    您可以在 cellForRowAtIndexPath 中为按钮添加目标,例如:

     [cell.yourButton addTarget:self action:@selector(buttonAction:event:) forControlEvents:UIControlEventTouchUpInside];
    

    您可以在此处接收此按钮的操作

    -(void)buttonAction:(UIButton *)sender event:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchPos = [touch locationInView:self.tblview];
    NSIndexPath *indexPath = [self.tblview  indexPathForRowAtPoint:touchPos];
    if(indexPath != nil){
    
    // handle click event..
        }
    }
    

    肯定会成功的…………

    【讨论】:

      猜你喜欢
      • 2023-02-07
      • 1970-01-01
      • 2014-12-31
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      相关资源
      最近更新 更多