【问题标题】:Custom UITableViewCell editingAccessoryView?自定义 UITableViewCell 编辑AccessoryView?
【发布时间】:2013-01-01 17:49:33
【问题描述】:

这是一个难题:我想创建一个自定义 editingAccessoryView,其中包含我的股票 UITableViewCell 的两个按钮。我想使用故事板来实现这一点。到目前为止,我已经按照hereherehere 列出的步骤进行了操作。我似乎无法让它工作。我得到的最接近的是当我创建一个类型为UIView 的xib 时,将类设置为包含UITableViewUIViewController 的类并将其绑定到我的IBOutlet,但在cellForRowAtIndexPath 上它是nil .

事实是,我想我只需要知道如何创建视图然后将其映射到editAccessoryView;从那里我相信我可以弄清楚如何添加按钮并映射相应的IBAction。谁能提供一些分步说明或教程链接?

【问题讨论】:

    标签: iphone ios xcode ios4


    【解决方案1】:

    我知道这可能为时已晚,但它比您找到的解决方案要好得多。 iOS 为您提供了一个名为tableView: editActionsForRowAtIndexPath indexPath: 的方法。这种方法基本上允许你添加自己的 UITableViewRowActions,这比使用 UIButtons 添加整个 UIView 更容易(也更简洁)。

    苹果说:

    当您想为您的表格行之一提供自定义操作时,请使用此方法。当用户在一行中水平滑动时,表格视图将行内容移到一边以显示您的操作。点击其中一个操作按钮会执行与操作对象一起存储的处理程序块。

    如果不实现此方法,表格视图会在用户滑动行时显示标准的附属按钮。

    如果需要,您可以自己查看Apple Documentation

    示例(斯威夫特)

    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
        let customAction = UITableViewRowAction(style: .Normal, title: "Your Custom Action", handler: { (action: UITableViewRowAction!, indexPath: NSIndexPath!) in
            println("Do whatever it is you want to do when they press your custom action button")
        })
        editAction.backgroundColor = UIColor.greenColor()
        
        let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete", handler: { (action: UITableViewRowAction!, indexPath: NSIndexPath!) in
            println("You can even implement a deletion action here")
        })
        deleteAction.backgroundColor = UIColor.redColor()
        
        return [deleteAction, editAction]
    }
    

    【讨论】:

      【解决方案2】:

      我自己用以下代码解决了这个问题:

          UIView *editingCategoryAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 35)];
      
          UIButton *addCategoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [addCategoryButton setTitle:@"Add" forState:UIControlStateNormal];
          [addCategoryButton setFrame:CGRectMake(0, 0, 50, 35)];
          [addCategoryButton addTarget:self action:@selector(addCategoryClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
      
          UIButton *removeCategoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [removeCategoryButton setTitle:@"Remove" forState:UIControlStateNormal];
          [removeCategoryButton setFrame:CGRectMake(55, 0, 65, 35)];
          [removeCategoryButton addTarget:self action:@selector(removeCategoryClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
      
          [editingCategoryAccessoryView addSubview:addCategoryButton];
          [editingCategoryAccessoryView addSubview:removeCategoryButton];
          cell.editingAccessoryView = editingCategoryAccessoryView;
      

      如您所见,我以编程方式创建了一个新的UIView,并通过addSubview 添加了两个按钮,然后将其分配给editingAccessoryView

      【讨论】:

      • 额外的好处:通过设置你自己的,你也可以覆盖默认的左侧位置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      相关资源
      最近更新 更多