简体   繁体   English

iOS使用自定义TableView单元格prepareForSegue

[英]IOS prepareForSegue using custom tableview cell

i created a .xib with my custom cell that I'm using on one of my tableviewcontroller in my storyboard. 我用自己的自定义单元格创建了一个.xib,该自定义单元格用于我的情节提要中的tableviewcontroller之一。

In the storyboard, I linked the current tableview with another tableview with a push segue. 在情节提要中,我将当前表视图与具有推送功能的另一个表视图链接。 (when the user click on one of the cell, he has a new view). (当用户单击一个单元格时,他将拥有一个新视图)。

The problem is, I don't understand, how to "link" this segue, with my CUSTOM cell ? 问题是,我不了解,如何将这个序列与我的CUSTOM单元“链接”?

I'm loading my custom cell like that : 我正在像这样加载我的自定义单元格:

static NSString *simpleTableIdentifier = @"cellCustom";

cellCustomView *cell = (cellCustomView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"cellCustom" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

and when I try to implement the method : 当我尝试实现该方法时:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"test"); 
}

I've nothing, because according me, I missed something to do with my custom cell, and the segue of my storyboard, but I don't understand how it works :/ 我一无所有,因为根据我的说法,我错过了与自定义单元和情节提要有关的东西,但我不了解它的工作原理:/

thx, 谢谢,

Did you try this? 你有尝试过吗?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"MySegue"
                              sender:self];
}

This method belong in whichever class contains the tableView. 该方法属于包含tableView的任何类。 Make sure that the tableView's delegate is equal to self in that class. 确保tableView的委托在该类中等于self。 If you're using a storyboard, control-drag this view to whichever view you want to segue to. 如果您使用的是情节提要板,请按住Control并将其拖到您要锁定的视图上。 Click on that segue, and change its identifier to "MySegue" or whatever you want, as long as its the same in your code. 单击该序列,然后将其标识符更改为“ MySegue”或任何您想要的名称,只要它在代码中相同即可。

You can try the following approach 您可以尝试以下方法

Control + Drag from your cell to next view controller to setup a segue called "showDetail", that you want to show on clicking tableview cell. Control +从单元格拖动到下一个视图控制器,以设置一个名为“ showDetail”的序列,您想在单击tableview单元时显示它。

and in your view controller 并在您的视图控制器中

if you have nsarray such as 如果您有nsarray之类的

NSArray *names = @[@"Apple", @"Google", @"Microsoft"];

and you want to pass the array value based on the selected table row cell index, you can as follows 并且要基于选定的表行单元格索引传递数组值,可以如下

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([[segue identifier] isEqualToString:@"showDetail"]) 
   {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *name = _names[indexPath.row]; 

        // For explanation sake, I am assuming name as property defined 
        // in your destination view controller
        [[segue destinationViewController] setName:name];  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM