• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何从 UITableViewCell 推送新 Controller

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:12:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有两个类。一个用于ItemController(扩展UITableViewController),另一个用于ItemCell(扩展'UITableViewCell')。

当点击每个单元格时,我会从 ItemControllerdidSelectRowAtIndexPath 中推送一个新 Controller 。

另外,在 ItemCell 中,我有几个小按钮,每个单元格都有不同的 tag。当点击这些按钮中的任何一个时,我想插入一个新的 Controller 。我怎样才能做到这一点?

self.navigationController.pushViewController 来自 ItemCell 不起作用,因为它没有 navigationController

我希望在 RubyMotion 中看到解决方案,但如果没有,那也很好

编辑

我读过 delegate 可以是一个解决方案,但我不知道如何实现它。这就是我所做的

项目 Controller :

  def tableView(table_view, cellForRowAtIndexPath: index_path)
    data_row = self.data[index_path.row]
    cell = table_view.dequeueReusableCellWithIdentifier(CATEGORY_CELL_ID) || begin
      rmq.create(ItemCell.initWithSomething(self), :category_cell, reuse_identifier: CATEGORY_CELL_ID).get
    end
    cell.update(data_row)
    cell
  end

项目单元:

class ItemCell < UITableViewCell

  attr_accessor :delegate
  def initWithSomething(delegate)
    @delegate = delegate
  end

  ...use @delegate to push the new controller
end

但我得到一个错误

item_controller.rb:114:in tableView:cellForRowAtIndexPath:': undefined methodinitWithSomething' for ItemCell:Class (NoMethodError)



Best Answer-推荐答案


一般的想法是你的单元应该告诉你的 View Controller 发生了什么事,而不是 View Controller 决定推送另一个 View Controller 。

您可以使用委托(delegate)设计模式做到这一点:

ItemCell 具有符合协议(protocol)的 delegate 属性。例如

@class ItemCell
@protocol ItemCellDelegate
- (void)itemCellDidClickSubmitButtonItemCell *)cell;
@end

@interface ItemCell
@property (nonatomic, weak) id<ItemCellDelegate> delegate
...
@end

tableView:cellForRowAtIndexPath: 中,您将 Controller 设置为单元格委托(delegate)(显然 View Controller 应符合 ItemCellDelegate):

cell.delegate = self

单元格上的按钮将触发单元格本身的 IBAction,进而调用委托(delegate)方法

- (IBAction)submitButtonTappedid)sender
{
    id <ItemCellDelegate> delegate = self.delegate;
    if ([delegate respondToSelectorselector(itemCellDidClickSubmitButton]) {
        [delegate itemCellDidClickSubmitButton:self];
    } 
}

显然,在您的 View Controller 中,您应该执行以下操作:

#pragma mark - ItemCellDelegate

- (void)itemCellDidClickSubmitButtonItemCell)cell
{
    UIViewController *controller = // Create the view controller to push
    [self.navigationController pushViewController:controller];
}

关于ios - 如何从 UITableViewCell 推送新 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22867010/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap