Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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/5/objective-c/26.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
如何在PrepareForSegue方法-iOS中获取自定义单元格的indexPath_Ios_Objective C_Uitableview_Segue_Nsindexpath - Fatal编程技术网

如何在PrepareForSegue方法-iOS中获取自定义单元格的indexPath

如何在PrepareForSegue方法-iOS中获取自定义单元格的indexPath,ios,objective-c,uitableview,segue,nsindexpath,Ios,Objective C,Uitableview,Segue,Nsindexpath,我有一个自定义的单元格,里面几乎没有视图和按钮。从其中一个按钮,我正在执行一个Segue,我需要那个单元格号码,但不知道如何获取它。请帮忙 下面是我手机的外观: 从红色圆圈按钮调用prepareforsgue 代码如下: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return _array.count; } - (NSInteger)tableView:(UITableView *)tabl

我有一个自定义的
单元格
,里面几乎没有视图和按钮。从其中一个按钮,我正在执行一个
Segue
,我需要那个单元格号码,但不知道如何获取它。请帮忙

下面是我手机的外观:

从红色圆圈按钮调用
prepareforsgue

代码如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFeedCell" forIndexPath:indexPath];
    if (cell) {
        cell.item = _array[indexPath.section];
    }

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"AudioComment"])
    {
      NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        NSLog(@"%ld",(long)indexPath.section); //printing 0 everytime :(
    }
}
请提供有关如何获取
indepath
的任何帮助/建议

谢谢。

您可以使用

UITableViewCell *cell = (UITableViewCell *)[[sender superview]superview];
NSLog(@"%ld",(long)[_generalTableView indexPathForCell:cell].row);
你可以用

UITableViewCell *cell = (UITableViewCell *)[[sender superview]superview];
NSLog(@"%ld",(long)[_generalTableView indexPathForCell:cell].row);

为索引添加一个变量,并在选择单元格时进行设置。访问perpareForSegue中的变量,将其传递给destinationVC并将其置为0。

为索引添加一个变量,并在选择单元格时进行设置。访问perpareForSegue中的变量,将其传递给destinationVC并将其置为0。

使用下面的代码。这可能对你有帮助

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainPhotoCell *cell = (MainPhotoCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifierMedia" forIndexPath:indexPath];
    if (!cell)
    {
        cell = [[MainPhotoCell alloc] init];
    }
   cell.usrCommentBtn.tag = indexPath.row;
   [cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (void)onCommentClick:(id)sender
{
   if ([GlobalManager checkUserLoginStatus:self])
   {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([sender tag]) inSection:0];

        [self performSegueWithIdentifier:@"showComments" sender:indexPath];
   }
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString: @"showComments"])
    {
        NSIndexPath * path = (NSIndexPath *)sender;
        //do the your required code here.
    }
}

使用下面的代码。这可能对你有帮助

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainPhotoCell *cell = (MainPhotoCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifierMedia" forIndexPath:indexPath];
    if (!cell)
    {
        cell = [[MainPhotoCell alloc] init];
    }
   cell.usrCommentBtn.tag = indexPath.row;
   [cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (void)onCommentClick:(id)sender
{
   if ([GlobalManager checkUserLoginStatus:self])
   {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([sender tag]) inSection:0];

        [self performSegueWithIdentifier:@"showComments" sender:indexPath];
   }
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString: @"showComments"])
    {
        NSIndexPath * path = (NSIndexPath *)sender;
        //do the your required code here.
    }
}

你必须在按钮上设置标签。在您的情况下,在CellForRowatineXpath中将按钮标记设置为indexPath.section

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFeedCell" forIndexPath:indexPath];
    if (cell) {
        cell.item = _array[indexPath.section];
    }

    cell.button1.tag = indexPath.section;
    return cell;
}
在PrepareForSegue中,从按钮标记获取索引

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"AudioComment"])
    {
        NSInteger index = ((UIButton *) sender).tag;
        NSLog(@" %ld ",index);
    }
}

你必须在按钮上设置标签。在您的情况下,在CellForRowatineXpath中将按钮标记设置为indexPath.section

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NewsFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFeedCell" forIndexPath:indexPath];
    if (cell) {
        cell.item = _array[indexPath.section];
    }

    cell.button1.tag = indexPath.section;
    return cell;
}
在PrepareForSegue中,从按钮标记获取索引

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"AudioComment"])
    {
        NSInteger index = ((UIButton *) sender).tag;
        NSLog(@" %ld ",index);
    }
}


这将不起作用,因为如果您单击表格单元格中的UIButton,该单元格将不会被选中。
sender.superview.superview
行给出错误:“在对象类型'\u strong id''上找不到属性'superview'”@SHA您可以再试一次吗?@Misha刚刚尝试了更新的代码,在每个单元格的按钮点击时仍为0。您可以记录单元格及其内容吗?我这边很好。是否要用UITableViewCell替换单元格?按钮在每个单元格中?这不起作用,因为如果您单击表单元格中的UIButton,该单元格将不会被选中。
sender.superview.superview
行给出错误:“在对象类型'\u strong id''上找不到属性'superview'”@SHA您可以再试一次吗?@Misha刚刚尝试了更新的代码,在每个单元格的按钮点击时仍为0。您可以记录单元格及其内容吗?我这边很好。是否要用UITableViewCell替换单元格?按钮在每个单元格中?不起作用:(
onCommentClick
从未被调用,直接调用
prepareforsgue
。不要这样做“[cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:)forControlEvents:uicontroleventTouchInside];”-将原型单元格与iActions和委派一起使用,从单元格到VCSure,任何代码都可以工作,但并不意味着它是好的。这种方式违反了OO设计原则。@Helium3。谢谢你的建议。我下次会提醒它。@Hardikshek我需要将
segue
也从故事板中的按钮断开吗?不工作:(
onCommentClick
永远不会被调用,直接调用
prepareforsgue
。不要这样做“[cell.usrCommentBtn addTarget:self action:@selector(onCommentClick:)forControlEvents:UIControlEventTouchUpInside];”-将原型单元格与IBActions和委派一起使用,从单元格到VCSure,任何代码都可以工作,但并不意味着它好。这种方式违反了OO设计原则。@Helium3。谢谢你的建议。我下次会提醒你。@Hardikshek我还需要将
segue
从故事板中的按钮断开吗?我没有选择cell,as
didselectrowatinex
从未被调用,因为我点击了该单元格中的特定按钮。如何获取索引我没有选择单元格,as
didselectrowatinex
从未被调用,因为我点击了该单元格中的特定按钮。我如何获得比Hardik Shekat更好的indexA解决方案:)谢谢:)一个比Hardik Shekat更好的解决方案:)谢谢:)UITableView的
indexPathForSelectedRow
会不会不起作用?(文档)UITableView的
indexPathForSelectedRow
会不会不起作用?(文件)