简体   繁体   English

外部笔尖中的表视图单元格。 故事板

[英]Table View Cell in external nib. Storyboards

So I have been building a table view in storyboard and I created an external Nib file with my custom UITableViewCell. 因此,我一直在情节提要中构建表视图,并使用自定义UITableViewCell创建了一个外部Nib文件。 Everything is working great. 一切都很好。 However, there is a button on the table view cell that I would like to have connected to a view in the storyboard, via a segue. 但是,表格视图单元格上有一个按钮,我想通过segue连接到情节提要中的视图。 Now I'm still very new to all this, so how would I connect the button on the custom cell to the view controller in the storyboard? 现在我对这一切仍然很陌生,那么如何将自定义单元上的按钮连接到情节提要中的视图控制器? Have I gone about this completely wrong? 我完全犯错了吗? If so, how would I restructure my project quickly and easily? 如果是这样,我将如何快速,轻松地重组项目? I would be grateful if you could provide a little explanation so I can learn from this. 如果您能提供一点解释,我将不胜感激。

Thanks for any help, 谢谢你的帮助,
Regards, 问候,
Mike 麦克风

It is possible to set a button inside UITableViewCell in storyboard using story board. 可以使用故事板在故事板中的UITableViewCell内设置按钮。 I have done similar thing before. 我以前做过类似的事情。 So, here's a solution; 因此,这是一个解决方案;

  • Create prototyoe cell inside UITableView cell and add a UIButton to it. 在UITableView单元内创建原型单元,并向其中添加一个UIButton。
  • Create a segue from the button to a new view controller and then choose the modal or push. 从按钮到新的视图控制器创建序列,然后选择模式或按钮。

Now, in the UITableViewData datasource cellForRowAtIndexPath; 现在,在UITableViewData数据源cellForRowAtIndexPath;中。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    UIButton *button = ((UIButton*)[cell.contentView.subviews objectAtIndex:0])
    button.tag = indexPath.row;
    cell.textLabel.text = @"Hello Text"
    ;
    return cell;
}

Now, when your button is clicked your prepareForSegue: is called. 现在,单击按钮时,将调用prepareForSegue:。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"identifier"]){
        UIButton *button = (UIButton*)sender;
        NewViewController *viewController = [segue destinationViewController];
       if(button.tag == 1)
          viewController.someobject = ...
       else 
          viewController.somobject = ...
    }
}

And that should work. 那应该起作用。

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

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