如何在没有其他视图控制器的情况下添加然后编辑UITableViewCell

时间:2014-01-27 03:10:59

标签: ios uitableview

我知道我无法直接编辑UITableViewCell,也就是说,我必须以某种方式在其中嵌入UITextField。我明白了。但如果可能的话,这就是我想做的事。

我有一个表格视图控制器,左侧是“编辑”,导航栏右侧是“+”。我想这样做......

  1. 当我点击“+”按钮时,我想要一个带有嵌入式UITextField的自定义UITableViewCell添加到表视图中。 (我知道如何创建自定义单元格。)
  2. 添加单元格后,我希望表格视图进入编辑模式,导航栏上的“编辑”变为“完成”。
  3. 现在我可以点击单元格内部,键盘出现,我输入我想要的内容,按“完成”,瞧,我的单元格带有输入的单元格。
  4. 我怎样才能完成#1和#2?可以这样做吗?网上有什么例子吗?

3 个答案:

答案 0 :(得分:1)

你走了。这有效。只需在Storyboards中创建适当的UITableViewController并进行适当的连接即可。如果你不能让它工作,我可以包括项目文件。

如果其中任何一项没有意义,请发表评论。快乐的编码!

//BGSMainTableViewController.h

#import <UIKit/UIKit.h>

@interface BGSMainTableViewController : UITableViewController

@end

//BGSMainTableViewController.m

#import "BGSMainTableViewController.h"
#import "BGSTextCell.h"

@interface BGSMainTableViewController () {
    NSArray *tableObjects;
}

@end

@implementation BGSMainTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set objects
    tableObjects = @[@"Object1", @"Object2", @"Object3"];

    // Set edit button
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    // Set add button
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                target:self 
                                action:@selector(addObject)];
    self.navigationItem.rightBarButtonItem = addButton;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableObjects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BGSTextCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textView.text = [tableObjects objectAtIndex:indexPath.row];

    // So the user can't edit the cell while not in "editing mode"
    [cell.textView setEditable:NO];

    return cell;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [super setEditing:editing animated:animated];

    if(editing)
    { 
        // Code here executed when "Edit" button pushed
        NSLog(@"editing");
        // You can go back here and turn on editing for each cell so the user can edit
        // all the cells. You just have to add more code below to update all the entries 
        // in the tableObjects array
    }
    else
    {
        // Code executed here when "Done" button is pushed

        // indexPath for recently added cell
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0] - 1 inSection:0];

        // This gets our newly added cell. We have to typecast it in order to avoid 
        // warnings and use the properties we've set up in the BGSTextCell .h/.m file 
        //In this case, the UITextView
        BGSTextCell *cell = (BGSTextCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        NSMutableArray *newArray = [NSMutableArray arrayWithArray:tableObjects];

        if ([cell.textView.text isEqualToString:@""])
        {
            // If cell is empty, delete it
            [newArray removeLastObject];
            tableObjects = [NSArray arrayWithArray:newArray];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        }
        else
        {
            // If cell contains text, add it to objects array, but replace the dummy 
            // string we added in addObject
            [newArray replaceObjectAtIndex:[newArray count] - 1 withObject:cell.textView.text];
            tableObjects = [NSArray arrayWithArray:newArray];

            // reset textview to not allow editing 
            cell.textView.editable = NO;
        }
    }
}

#pragma mark - Accessory Methods

- (void)addObject
{
    // 1 - Add a dummy string object to the array that the tableview receive's its data 
    // from. This has to be done first or there won't be data for the added 
    // UITableViewCell and an exception will be thrown.
    NSMutableArray *newArray = [NSMutableArray arrayWithArray:tableObjects];
    [newArray addObject:@""];
    tableObjects = [NSArray arrayWithArray:newArray];

    // 2 - Add cell to the last position in the tableView    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0] inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

    // 3 - Get the newly added cell and allow editing
    BGSTextCell *addedCell = (BGSTextCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    [addedCell.textView setEditable:YES];

    // 4 - This is what automatically pulls up the keyboard
    [addedCell.textView becomeFirstResponder];

    // 5 - Puts the tableView in "editing mode"
    self.editing = YES;
}

// BGSTextCell.h
#import <UIKit/UIKit.h>

@interface BGSTextCell : UITableViewCell

// I'm using a UITextView and NOT a UITextField because it's a lot easier to prevent 
// editing by just assigning NO to setEditable: . Someone can correct me on this, but I    
// personally haven't found an easy way to prevent editing on a UITextField
@property (nonatomic, strong) IBOutlet UITextView *textView;

@end


// BGSTextCell.m
#import "BGSTextCell.h"

@implementation BGSTextCell

@end

答案 1 :(得分:0)

这样做,


  - (void)viewDidLoad
    {
      [super viewDidLoad];

      array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3", nil];
      self.navigationItem.leftBarButtonItem = self.editButtonItem;
      UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(whenAddButtonTapped:)];
self.navigationItem.rightBarButtonItem = addItem;
  }

  - (void)whenAddButtonTapped:(id)sender
  {
    //logic to load datasource
    int count = [array count];
     count ++;
    NSString *str = [NSString stringWithFormat:@"%d",count];
    [array addObject:str];
    [self.aTableView reloadData];//add the cell

    //after change the edit to done
    self.aTableView.editing = YES;//make the tableview to editing mode
    [self setEditing:YES animated:YES];//set the button done

  }

 //u need to implement this to change the beheaviour of edit button
 - (void)setEditing:(BOOL)editing animated:(BOOL)animated
 {
     [super setEditing:editing animated:animated];
     if(editing)
    {
      self.aTableView.editing = editing;

    }
    else
    {
      self.aTableView.editing = NO;
   }

 }


答案 2 :(得分:-1)

您可以通过使用默认/模板项目 - 主 - 详细信息应用程序

来了解添加按钮的单元格

标识符可以设置为 - 选择连接 - 移动到属性检查器面板

if ([segue.identifier isEqualToString:@"Your first Controller"])
{

}
else if([segue.identifier isEqualToString:@"Your second Controller"])
{

}

有关详细信息,请参见下面的图片 How add and edit with one controller