我想在表格视图的右侧添加加号按钮,以在其他视图中添加该单元格的内容。
如何在表格视图的右侧添加加号按钮。
答案 0 :(得分:27)
如果您有导航栏,则应添加如下所示的UIBarButtonItem:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
答案 1 :(得分:9)
转到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;
答案 2 :(得分:2)
对于Swift
let addButton = UIBarButtonItem.init(barButtonSystemItem: .Add,
target: self,
action: #selector(yourFunction))
self.navigationItem.rightBarButtonItem = addButton