Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义单元格按钮单击_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 自定义单元格按钮单击

Ios 自定义单元格按钮单击,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我的设想如下: 自定义单元视图 1) 我正在自定义单元格视图中填充来自服务器的数据,该视图已完全填充,我希望在数组中有一个链接存储区,我希望在浏览器中打开该链接存储区,因此我希望检查单击了哪个index.row按钮,以便我可以根据该行获取url链接并在浏览器中打开它,到目前为止,我已经找到了按钮标签的解决方案,但这也不起作用,就好像我们在屏幕上同时有两个单元格,点击按钮两个按钮返回相同的标签 2) 作为附加的图像,我有另一个故事板,我想在其中切换到一个新的视图控制器,如上所述,我想传递一个特定

我的设想如下:

自定义单元视图

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

2) 作为附加的图像,我有另一个故事板,我想在其中切换到一个新的视图控制器,如上所述,我想传递一个特定的文章标题和键以及

我希望一切都清楚


提前感谢。

您可以扩展
UITableViewCell
并向其添加一个touple属性。包含
index.row
的键和包含URL的值,例如:-1:“www.stackoverflow.com”。当你点击一个按钮时,你只需要找出
index.row
,从中你可以得到你想要的值

我找到了按钮标签的解决方案,但那也不管用

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

  • cellforrowatinexpath:
    方法中键入下面给出的代码段

    - (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];
    
    }
    
  • 为选中按钮添加以下方法:

    - (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); }
    
    这里,我们检查cell上的用户交互<代码>索引XPath。行将返回用户选择的单元格的编号。您可以执行其他操作来代替
    NSLog
    方法

    - (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];
    
    }
    
    为了在执行segue时发送值,请使用以下步骤:

    • NewViewController.h
      文件中创建要执行以下步骤的属性:

      @property (nonatomic) NSString* receivedValue;
      
    • @通过在
      @implementation
      @end

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

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

      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      NewViewController *nVC = [segue destinationViewController];
      [nVC setReceivedValue:sendValue];
      }
      
    此处
    sendValue
    是您要发送的值

    就这些。
    构建并运行您的项目并享受。

    回答问题的第一部分。标签是一种痛苦。最好将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
    
    }
    

    您可以在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..
        }
    }
    

    当然它会起作用………

    显示一些代码,说明如何分配按钮标记?@ChintaN Maddy Ramani cell.PostsButton.tag=indexPath.row;这是习俗cell@ChintaN-Maddy Ramani在按钮上单击…-(iAction)CustomButton单击:(id)发送方{int tagid=(int)cell.PostsButton.tag;NSLog(@“%d”,tagid);}我建议使用委派模式。下面是我用Swift写的一个类似的答案;适应目标C是非常直接的另一个答案供您参考:它给了我一个错误:在类型为“\u strong id”
    -(iAction)CustomButton的对象上找不到属性“bounds”;(iAction)CustomButton单击:(id)sender{CGPoint location=[tableviewcustom convertPoint:sender.bounds.origin fromView:sender];NSIndexPath*indexPath=[tableviewcustom indexPathForRowAtPoint:location];UITableViewCell*cell=[tableviewcustom CellForRowatineXpath];}
    操作需要在视图控制器子类中。并且(id)发送方需要是(UIButton*)发送方SindexPath*indexPath=[self.tableView indexPathForSelectedRow];NSLog(@“ShowindexPath%@),indexPath);按钮单击时返回(nul)亮起抱歉给您带来不便。我已更改代码,该代码将为您提供所选单元格的索引。此代码100%正常工作。单击表视图单元格时将调用didSelectRowAtIndexPath,在我的情况下,我要单击按钮