didSelectRowAtIndexPath不会被调用到cellForRowAtIndex的UIButton

时间:2013-11-21 09:34:59

标签: ios objective-c uitableview uibutton

我知道这个问题有很多重复,但我的要求是我在一个单元格上添加了两个UIButtons,两个按钮都会打开两个不同的视图。如果我将属性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];

    }
}

3 个答案:

答案 0 :(得分:5)

如果要在单元格内调用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.
}

希望这有帮助。

答案 1 :(得分:1)

更改按钮选择器的代码

[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

答案 2 :(得分:1)

如果U使用按钮而不是U则不需要使用didSelectRowAtIndexPath。在按钮上设置标签以匹配cellForRowAtIndexPath中单元格的indexPath.row ...这样当U按下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;
}
相关问题