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

utt*_*tam 8 iphone objective-c

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

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

Tho*_*itz 27

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

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]      
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
    target:self action:@selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
Run Code Online (Sandbox Code Playgroud)


era*_*uki 9

  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;
    
    Run Code Online (Sandbox Code Playgroud)

  • *注意:您也可以使用:[UIButton buttonWithType:UIButtonTypeContactAdd],而不是复制和添加; 而不是UIButtonTypeCustom. (12认同)