【问题标题】:How to call the delegate method of UITableView manually?如何手动调用 UITableView 的委托方法?
【发布时间】:2013-11-06 13:17:03
【问题描述】:

我有一个按钮应该调用tableview的委托方法

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

点击该按钮时。

谁能告诉我怎么做。

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];

【讨论】:

    【解决方案2】:

    按钮无法自动调用该方法,因为它没有表视图或索引路径的概念,但如果您创建自己的方法,则可以直接调用该方法

    - (void)buttonAction:(id)sender
    {
        NSIndexPath *path = //path for row you want to select
        [self tableView:self.tableView didSelectRowAtIndexPath:path];
    }
    

    【讨论】:

      【解决方案3】:
      [self tableView:_myTbl didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:_rowNeeded inSection:_sectionNeeded];
      //call method, like user selected needed cell
      
      [_myTbl selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
      // show selected cell in UI
      

      【讨论】:

        【解决方案4】:

        如果您希望当用户按下 TableViewCell 上的按钮时触发 Action what's occur when a cell is taped ,那么您可以执行以下操作。

        在您的 cellForRowAtIndexPath 方法中,在您的按钮上添加一个手势识别器。像这样,

        cell.myButton.userInteractionEnabled = YES;
        cell.myButton.tag = indexPath.row ;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [tap setNumberOfTouchesRequired:1];
        [tap setNumberOfTapsRequired:1];
        [tap setDelegate:self];
        [cell.myButton addGestureRecognizer:tap]; 
        

        这里是handleTap方法

        - (void)handleTap:(UITapGestureRecognizer *)recognizer
        {
            NSLog(@"Handle Tap method");
            NSLog(@"Row Index : %d" , recognizer.view.tag);
            int row_index = recognizer.view.tag ;
        
            // Perform your Action woth row_index
        }
        

        并且不要忘记添加您的头文件。希望能帮助到你。

        【讨论】:

          【解决方案5】:

          对于在表格视图单元格中选择整行复选框:

          NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
          [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
          [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
          

          取“i”值作为for循环

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-06
            相关资源
            最近更新 更多