0

我有一个 tableView,我想通过单击单元格中心的按钮切换到另一个视图。我使用动态单元格。

所以,我想通过单击单元格中的按钮切换到视图(在导航控制器中),我该如何在 prepareForSegue 中做到这一点?我可以获取单元格的索引,然后通过单击公开来执行此步骤,但是当我单击位于单元格中心的按钮时,索引路径不存在。这里的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewController *destination = segue.destinationViewController;
    if ([destination respondsToSelector:@selector(setDelegate:)]) {
        [destination setValue:self forKey:@"delegate"];
    }
    if ([destination respondsToSelector:@selector(setSelection:)]) {
        NSIndexPath *indexPath = [self.tableFeed indexPathForCell:sender];
        NSString *idUser = [self.arrIDUser objectAtIndex:indexPath.row];
        NSLog(@"%@",idUser);
        NSString *pattern = [self.arrPattern objectAtIndex:indexPath.row];
        NSMutableDictionary *selection = [[NSMutableDictionary alloc] initWithObjectsAndKeys:idUser,@"idUser", pattern, @"pattern", nil];
        [destination setValue:selection forKey:@"selection"];
    }
}
4

3 回答 3

0

您可以通过转到按钮的父视图获取内容视图,然后到内容视图的父视图获取单元格来获取 indexPath。但这有点丑陋。

更好的方法可以在iOS 食谱中找到,它们通过convertPoint:toView:将按钮的原点转换为表格视图,然后调用indexPathForRowAtPoint:来获取 indexPath。

于 2013-08-01T10:30:34.880 回答
0
 you have to create button in cellForRowAtIndexPath delegate method and assign tag to taht button. Based on that you will get that which row has selected.   

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     selectedDealIndex = indexPath.row;  //here you will get selected index 
    [self performSegueWithIdentifier:@"your_seguename" sender:self]; //here you have to pass your seguename which you have written in xib[in properywindow]
}
于 2013-08-01T10:42:35.013 回答
0

首先你需要抓取单元格,然后是它的超级视图,即 UITableViewCellContentView,然后是包含这个单元格的表格。这是代码。

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"prepare for segue");   
    //sender is UIRoundRectButton
    UITableViewCell *cell = (UITableViewCell*) [[sender superview] superview];
    UITableView *table = (UITableView*)[[[sender superview] superview] superview];

    NSIndexPath *indexPath = [table indexPathForCell:cell];

    NSLog(@"%i %i",indexPath.section,indexPath.row);  

}

这将为您提供点击按钮的单元格的索引路径。

于 2013-08-02T06:46:41.683 回答