问题描述
这就是困境:我想创建一个自定义 editingAccessoryView
,其中包含我的股票 UITableViewCell
的两个按钮。我想用故事板来实现这个目标。到目前为止,我已按照 和。我似乎无法让它工作。我得到的最接近的是当我创建一个类型为 UIView
的xib时,将该类设置为包含我的 UIViewController
的类 UITableView
并将其绑定到我的 IBOutlet
,但是在 cellForRowAtIndexPath
它是 nil
。
Here's the dilemma: I want to create a custom editingAccessoryView
that contains two buttons for my stock UITableViewCell
. I'd like to achieve this using a storyboard. So far I've followed the steps outlined here, here and here. I just can't seem to get it to work. The closest I got was when I created a xib of type UIView
, set the class to that of my UIViewController
that contains the UITableView
and bound it to my IBOutlet
, but on cellForRowAtIndexPath
it's nil
.
真相是,我想我只需要知道如何创建视图然后映射它到 editAccessoryView
;从那里我相信我可以弄清楚如何添加按钮并映射相应的 IBAction
。任何人都可以提供一些分步说明或教程链接吗?
Truth is, I think I just need to know how to create the view and then map it to the editAccessoryView
; from there I believe I can figure out how to add buttons and map the corresponding IBAction
. Can anyone provide some step-by-step instructions or links to tutorials?
推荐答案
我用以下代码自行解决了这个问题:
I solved this on my own with the following code:
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
。
As you can see I created a new UIView
programmatically and added two buttons via addSubview
and then assigned it to the editingAccessoryView
.
这篇关于自定义UITableViewCell editAccessoryView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!