简体   繁体   English

UITableView编辑模式是否添加用户定义的单元格?

[英]UITableView Edit Mode Add the cell defined by user?

I wanted to use the table editing mode so that the users can choose which Cells to add. 我想使用表格编辑模式,以便用户可以选择要添加的单元格。

For eg I have 例如,我有

addArray = {@"Red",@"Blue",@"Green"};

Now , what I want is that when user enters the editing mode and presses the add button, he gets the options: Red , Blue , Green and selects the cell to be added. 现在,我想要的是当用户进入编辑模式并按下添加按钮时,他获得以下选项: RedBlueGreen并选择要添加的单元格。

-(IBAction)addButtonPressed
{
  [displayArray addObject:"User's Choice"];
  [self.tableview reloadData];
}

You probably want to use a UIActionSheet (see Apple docs ): 您可能要使用UIActionSheet (请参阅Apple文档 ):

UIActionSheet *actionSheet = [[UIActionSheet alloc]
                               initWithTitle:@"Select an option"
                               delegate:self
                               cancelButtonTitle:@"Cancel"
                               destructiveButtonTitle:nil
                               otherButtonTitles:@"Red", @"Green", @"Blue", nil];
[actionSheet showInView:self.view];

You will need to make your view controller conform to the UIActionSheetDelegate protocol: 您将需要使您的视图控制器符合UIActionSheetDelegate协议:

@interface MyViewController : UITableViewController <UIActionSheetDelegate>

Then, implement the actionSheet:clickedButtonAtIndex: method in your view controller: 然后,在您的视图控制器中实现actionSheet:clickedButtonAtIndex:方法:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.firstOtherButtonIndex) {
        // Red
        [displayArray addObject:"Red"];
        [self.tableview reloadData];
    }
    else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1) {
        // Green
        [displayArray addObject:"Green"];
        [self.tableview reloadData];
    }
    else if (buttonIndex == actionSheet.firstOtherButtonIndex + 2) {
        // Blue
        [displayArray addObject:"Blue"];
        [self.tableview reloadData];
    }
}

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

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