繁体   English   中英

如何在objective-c中将加号按钮添加到表格视图的右侧?

[英]how to add the plus button to the right side of the table view in objective-c?

我想在表视图的右侧添加加号按钮,以在其他视图中添加该单元格的内容。

如何在表视图的右侧添加加号按钮。

如果你有一个导航栏,你应该像这样添加一个UIBarButtonItem:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]      
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
    target:self action:@selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
  1. 转到iPhone 界面指南页面

  2. 在“表格行和其他用户界面元素中使用的标准按钮”下,复制ContactAdd按钮(我将其另存为ContactAdd.png)。 将其添加到您的项目中。

  3. 在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM