【发布时间】:2011-01-17 08:46:14
【问题描述】:
我想在表格视图的右侧添加加号按钮,以在其他视图中添加该单元格的内容。
如何在表格视图右侧添加加号按钮。
【问题讨论】:
标签: iphone objective-c
我想在表格视图的右侧添加加号按钮,以在其他视图中添加该单元格的内容。
如何在表格视图右侧添加加号按钮。
【问题讨论】:
标签: iphone objective-c
如果你有一个导航栏,你应该像这样添加一个 UIBarButtonItem:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
【讨论】:
转到 iPhone Interface Guidelines Page。
在“用于表格行和其他用户界面元素的标准按钮”下复制 ContactAdd 按钮(我在此处将其保存为 ContactAdd.png)。将其添加到您的项目中。
在cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中添加:
UIImage *image = [UIImage imageNamed:@"ContactAdd.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//You can also Use:
//UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
//match the button's size with the image size
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so you can open the next view
[button addTarget:self action:@selector(yourFunctionToNextView:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
【讨论】:
对于斯威夫特
let addButton = UIBarButtonItem.init(barButtonSystemItem: .Add,
target: self,
action: #selector(yourFunction))
self.navigationItem.rightBarButtonItem = addButton
【讨论】: