didSelectRowAtIndexPath 没有被调用到 cellForRowAtIndex 的 UIButton

Posted

技术标签:

【中文标题】didSelectRowAtIndexPath 没有被调用到 cellForRowAtIndex 的 UIButton【英文标题】:didSelectRowAtIndexPath is not invoked to UIButton of cellForRowAtIndex 【发布时间】:2013-11-21 09:34:58 【问题描述】:

我知道这里有很多关于这个问题的重复项,但我的要求是我在一个单元格上添加了 2 个 UIButton,并且两个按钮都会打开两个不同的视图。如果我将属性 userInteractionEnabled 设置为 YES,那么它不会从下面的代码中从 didSelectRowAtIndexPath 中获取“finalID”。

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    if(tableView == self.tableViewProject)
        static NSString *cellId = @"attachmentCellId";
        attachmentCell *cell = (attachmentCell *)[self.tableViewProject dequeueReusableCellWithIdentifier:cellId];
        if(!cell)
        
            if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
            
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"attachmentCell" owner:self options:Nil];
                for(id object in nib)
                
                    if([object isKindOfClass:[attachmentCell class]])
                    
                        cell = (attachmentCell *)object;
                        break;
                    

                
                UIButton *button;
                button = [[UIButton alloc] initWithFrame:CGRectMake(162, 0, 75, 53)];
                [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
                button.userInteractionEnabled = YES;
//button.userInteractionEnabled = NO;
                [cell.contentView addSubview:button];

                UIButton *buttonAttach = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 75, 53)];
                [buttonAttach addTarget:self action:@selector(buttonAttachClicked) forControlEvents:UIControlEventTouchUpInside];


                buttonAttach.userInteractionEnabled = YES;
//buttonAttach.userInteractionEnabled = NO;
                [cell.contentView addSubview:buttonAttach];

                cell = [nib objectAtIndex:0];
                SaveAttachment *attach = [array objectAtIndex:indexPath.row];
                cell.name.text = attach.name;
                cell.list.text = [NSString stringWithFormat:@"%d", attach.list];
                cell.attachment.text = [NSString stringWithFormat:@"%d", attach.attachment];
                cell.date.text = attach.date;
            
    return cell;
    

而我的 DidSelectRowAtIndexPath 是

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

     NSLog(@"Array == %@", anotherTempArray);

        NSString *finalId = [NSString stringWithFormat:@"%@", [anotherTempArray objectAtIndex:indexPath.row]];
        NSLog(@"final id for selected row = %@", finalId);
        NSUserDefaults *defaultForFinalId = [NSUserDefaults standardUserDefaults];
        NSString *setFinalId = finalId;
        [defaultForFinalId setObject:setFinalId forKey:@"SETFINALID"];

    if(tableView == self.tableViewProject)
    

        [self buttonClicked];
        //[self viewDidLoadForList];

    
    if(tableView == self.tableViewAttachmentList)
    


        [self buttonAttachClicked];

    

【问题讨论】:

【参考方案1】:

如果你想在单元格内调用UIButton的选择器,那么你不需要使用didSelectRowAtIndexPath方法。

在将处理程序添加到UIButton 之前,您所做的是正确的。现在,将您的didSelectRowAtIndexPath 代码删除到按钮的单击处理程序。以下是如何从按钮单击处理程序中获取 indexPath。

- (void)buttonClicked:(UIButton *)sender 

    UITableViewCell *cell = (UITableViewCell*)sender.superview.superview; //Since you are adding to cell.contentView, navigate two levels to get cell object
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];

    // Now you can do the code you put in didSelectRow here.

希望这会有所帮助。

【讨论】:

干得好,伙计们。我对 swift 3 进行了一些更改。所以工作真棒!【参考方案2】:

更改按钮选择器的代码

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; //add colon in selector buttonClicked


[buttonAttach addTarget:self action:@selector(buttonAttachClicked:) forControlEvents:UIControlEventTouchUpInside]; //add colon in selector buttonAttachClicked

【讨论】:

方法没有参数 suhit 在按钮上调用方法时,需要将该按钮的引用传递给该方法,可以通过标签或按钮标题进行验证。 什么是self.tableViewProject和self.tableViewAttachmentList?你在用两个 tableview 插座吗【参考方案3】:

如果你使用按钮而不是你不需要使用 didSelectRowAtIndexPath。将按钮上的标签设置为与 cellForRowAtIndexPath 中单元格的 indexPath.row 匹配...这样,当您按下 buttonAttachClicked 或 buttonClicked 中的按钮时,只需检查发件人标签并从 UITableView 中拉出该单元格

将此代码用于 cellForRowAtIndexPath

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


if(tableView == self.tableViewProject)
    static NSString *cellId = @"attachmentCellId";
    attachmentCell *cell = (attachmentCell *)[self.tableViewProject dequeueReusableCellWithIdentifier:cellId];
    if(!cell)
    
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"attachmentCell" owner:self options:Nil];
            for(id object in nib)
            
                if([object isKindOfClass:[attachmentCell class]])
                
                    cell = (attachmentCell *)object;
                    break;
                

            

            cell.button = [[UIButton alloc] initWithFrame:CGRectMake(162, 0, 75, 53)];
            [cell.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:cell.button];

            cell.buttonAttach = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 75, 53)];
            [cell.buttonAttach addTarget:self action:@selector(buttonAttachClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:cell.buttonAttach];

            cell = [nib objectAtIndex:0];
        
SaveAttachment *attach = [array objectAtIndex:indexPath.row];
cell.name.text = attach.name;
cell.list.text = [NSString stringWithFormat:@"%d", attach.list];
cell.attachment.text = [NSString stringWithFormat:@"%d", attach.attachment];
cell.date.text = attach.date;        
[cell.button setTag:indexPath.row];
[cell.buttonAttach setTag:indexPath.row];            

return cell;

【讨论】:

那么如何放置标签呢? button.tag = indexpath.row? ? 我已经编辑了我的答案......然后你定义了按钮方法,如 -(void)buttonAttachClicked:(id)sender,另一个就像那个 好吧,你需要你的按钮成为 attachmentCell 的对象,这样你就可以引用它们,然后你只需添加单元格。在按钮和按钮附加之前 cell.button 将引发编译时错误,因为 attachmentCell 没有按钮。我们在这里以编程方式制作一个按钮 把它放在一个笔尖和插座上,或者以编程方式添加它,不管你怎么做,你只需要参考原因,当使用可重复使用的单元格时,你将使用相同的按钮来呈现多个标签,如果你在单元格按钮上没有引用,你将无法阻止从数组中提取哪个记录......你还有另一个错误

以上是关于didSelectRowAtIndexPath 没有被调用到 cellForRowAtIndex 的 UIButton的主要内容,如果未能解决你的问题,请参考以下文章

在 didSelectRowAtIndexPath 之后 prepareForSegue?

我的 didSelectRowAtIndexPath 没有被调用

didSelectRowAtIndexPath 没有被调用?

tableView - didSelectRowAtIndexPath 未调用

快速:didSelectRowAtIndexPath

我可以麻烦UITableViewDelegate的didSelectRowAtIndexPath:吗?