Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.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 情节提要自定义UITableviewcell选择_Ios_Uitableview_Segue - Fatal编程技术网

Ios 情节提要自定义UITableviewcell选择

Ios 情节提要自定义UITableviewcell选择,ios,uitableview,segue,Ios,Uitableview,Segue,在我的项目中,我有自定义的UITableview单元格,其中碰巧有一个按钮。选择此按钮后,将触发一个序列。在此过程中,我将一个对象从单元格传递到目标UIViewcontroller。我目前正在使用以下代码 else if ([[segue identifier] isEqualToString:@"Add"]) { SearchAddRecordViewController *addRecordViewController = [segue destinationViewControll

在我的项目中,我有自定义的
UITableview
单元格,其中碰巧有一个按钮。选择此按钮后,将触发一个序列。在此过程中,我将一个对象从单元格传递到目标
UIViewcontroller
。我目前正在使用以下代码

else if ([[segue identifier] isEqualToString:@"Add"])
{
    SearchAddRecordViewController *addRecordViewController = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    SearchCell *cell = [self.tableView cellForRowAtIndexPath:path];
    NSLog(@"%@", cell.record);
    addRecordViewController.workingRecord = cell.record;
}
结果我传递的是null,因为按钮没有触发单元格的选择,因此没有indexPathForSelectedRow。我的问题是,如何获取按下按钮的单元格的indexpath

编辑:
回答

在自定义单元格的.h文件中声明NSIndexpath变量,并在cellForRowAtindexPath中将indexPath分配给该变量。然后检索并使用它……希望它能对任何遇到类似问题的人起作用,这就是我解决这个问题的方法

我首先使用nsindepath属性为UIButton子类创建了实现和头文件。我在故事板中为uibutton分配了这个带有NSIndexPath属性的新自定义unbutton的超类。我在segue标识步骤中添加了以下代码(关键是我意识到我可以使用sender对象作为触发segue的源):


确定indexPath以避免对UIButton子类化的另一种方法是:

CGPoint point = [button.superview convertPoint:button.center toView:self.tableView];
NSIndexPath *path = [self.tableView indexPathForRowAtPoint:point];
编辑:

我已经将此因素考虑到一个很好的方法中,您可以将其添加到UITableView上的类别中:

-(NSIndexPath*)indexPathOfCellComponent:(UIView*)component {
    if([component isDescendantOfView:self] && component != self) {
        CGPoint point = [component.superview convertPoint:component.center toView:self];
        return [self indexPathForRowAtPoint:point];
    }
    else {
        return nil;
    }
}

您可以通过以下方式完成此操作,这有助于您唯一地识别点击的按钮,并且您可以传递适当的值,例如,我只是传递一个字符串值

假设OutputString是目标视图控制器中的字符串属性

if ([[segue identifier] isEqualToString:@"yourSegueIdentifier"]) {

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [self.myTableView indexPathForCell:clickedCell];
    [segue.destinationViewController setOutputString:@"XYZ"];        
}

这对我有什么帮助?问题是我无法通过按钮访问手机。这个按钮触发了segue,也许我可以在按钮上加个标签?我不知道如何使用序列图像板创建动态标记,尽管这对UITableViewCell中的视图层次结构做出了一些假设,但这可能不是真的。请使用
indexPathForRowAtPoint:
查看我的答案,以获得更可靠的方法。这将为我提供表中下一个单元格的索引路径。他们是不是想通过减少路径来获得正确的路径?对不起,代码不正确,我现在已经更正了
convertPoint
显然需要在
按钮上调用。superview
而不是直接在按钮上调用!
if ([[segue identifier] isEqualToString:@"yourSegueIdentifier"]) {

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [self.myTableView indexPathForCell:clickedCell];
    [segue.destinationViewController setOutputString:@"XYZ"];        
}