objective-c - 外部 Nib 中的 TableView 单元格。 Storyboard

标签 objective-c ios

因此,我一直在 Storyboard中构建表格 View ,并使用自定义 UITableViewCell 创建了一个外部 Nib 文件。一切都很好。但是,表格 View 单元格上有一个按钮,我想通过 segue 连接到 Storyboard 中的 View 。现在我对这一切还是很陌生,那么如何将自定义单元格上的按钮连接到 Storyboard 中的 View Controller ?我这样做完全错了吗?如果是这样,我将如何快速轻松地重组我的项目?如果您能提供一些解释,我将不胜感激,以便我从中学习。

感谢您的帮助,
此致,
迈克

最佳答案

可以使用 Storyboard在 Storyboard的 UITableViewCell 中设置一个按钮。我以前做过类似的事情。所以,这是一个解决方案;

  • 在 UITableView 单元格内创建原型(prototype)单元格,并向其中添加一个 UIButton。
  • 创建从按钮到新 View Controller 的 segue,然后选择模态或推送。

现在,在 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;
}

现在,当您的按钮被点击时,您的 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 = ...
    }
}

这应该有效。

关于objective-c - 外部 Nib 中的 TableView 单元格。 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13591850/

相关文章:

ios - sendSubviewToBack 和 bringSubviewToFront 导致 Root View Controller 的 subview 向上移动并被导航栏重叠

ios - 销毁带有烟雾效果的uiimage View

restkit 中的 ios NSDateFormatter

ios - 使用 UIImagePickerControllerDelegate 获取照片的图像 URL

ios - 如何从 nib 文件加载 UITableViewCell 高度?

java - 需要帮助将简短的 Objective-C 例程转换为 Java

iphone - 如何在 UITableView 透明 header 下屏蔽 UITableViewCells

ios - 如何在 if/switch 语句中声明一个将变为全局的变量?

ios - NSFetchedResultsController 对非常大的核心数据数据库的响应不那么灵敏

ios - 如何将自定义类添加到 Storyboard ?