didSelectRowAtIndexpath

didSelectRowAtIndexpath

本文介绍了didSelectRowAtIndexPath 不会被调用到 cellForRowAtIndex 的 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that there are many duplicates here regarding this question but my requirement is that I added 2 UIButtons on one cell and both the buttons will open two different views. If I set the property userInteractionEnabled to YES, then it will not pic the 'finalID' from didSelectRowAtIndexPath from the below code.

 -(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 是

And my DidSelectRowAtIndexPath is

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

    }
}

推荐答案

如果你想在cell内部调用UIButton的选择器,那么你不需要使用didSelectRowAtIndexPath 方法.

If you want to call the selector of UIButton inside cell, then what you dont need to use didSelectRowAtIndexPath method.

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

What you did was correct upto adding handler to UIButton. Now, remove your didSelectRowAtIndexPath code to button's click handler. Here is how you can get indexPath from the button click handler.

- (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.
}

希望这会有所帮助.

这篇关于didSelectRowAtIndexPath 不会被调用到 cellForRowAtIndex 的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:08