Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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 如何手动调用UITableView的委托方法?_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何手动调用UITableView的委托方法?

Ios 如何手动调用UITableView的委托方法?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个按钮可以调用tableview的委托方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 当那个按钮被点击时 有人能告诉我怎么做吗。按钮不能自动调用该方法,因为它没有表视图或索引路径的概念,但如果创建自己的方法,可以直接调用该方法 - (void)buttonAction:(id)sender { NSIndexPath *path = //p

我有一个按钮可以调用tableview的委托方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
当那个按钮被点击时


有人能告诉我怎么做吗。

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

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

如果您希望在用户按下TableViewCell上的按钮时启动该操作,则当点击单元格时会发生什么情况,然后您可以执行以下操作

在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
}

别忘了添加头文件。希望能有帮助

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

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
将“i”值作为循环

检查这个答案:创建一个方法(某处),该方法由委托方法和按钮操作方法使用,并使用该方法而不是此方法。
- (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
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];