In-place editing of UITableView cell

前端 未结 2 401
情书的邮戳
情书的邮戳 2021-02-05 22:50

Can someone please show me an example of in-place editing of UITableView cell...I am aware of UITableView delegate methods like cellForRowAtIndexPath,...etc

But I do not

相关标签:
2条回答
  • 2021-02-05 23:32

    Add a UITextField to your cell.

    Either way you choose to edit an entry, whether your using CoreData, or whatever to store the info, you need someway to save it. If you go the on-Table editing method, you can use the textField delegates to save the data as the user hits return.

    In your cellForRowAtIndexPath:

    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0,10,125,25)];
    myTextField.adjustsFontSizeToFitWidth = NO;
    myTextField.backgroundColor = [UIColor clearColor];
    myTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    myTextField.textAlignment = UITextAlignmentRight;
    myTextField.keyboardType = UIKeyboardTypeDefault;
    myTextField.returnKeyType = UIReturnKeyDone;
    myTextField.clearButtonMode = UITextFieldViewModeNever;
    myTextField.delegate = self;
    cell.accessoryView = myTextField;
    

    TextField Delegates:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        if(textField == myTextField){
            /*  do your saving here  */ 
        }
    }
    
    0 讨论(0)
  • 2021-02-05 23:36

    This is very old.. and probably needs a more recent answer.. I just had this problem and hacked together something.. maybe someone finds it useful..

    I created a Table View Controller that adds a message box add the end of an "item list" thats stored in NSUserDefaults... Item

        //
        //  MyItemListTVC.m
        //  Best10
        //
        //  Created by Francois Chaubard on 12/26/13.
        //  Copyright (c) 2013 Chaubard. All rights reserved.
        //
    
        #import "MyItemListTVC.h"
        #import "AppDelegate.h"
    
        @interface MyItemListTVC ()
    
        @property (strong, nonatomic) UIRefreshControl IBOutlet     *refreshControl;
        @property (strong,nonatomic) UIBarButtonItem *addButton;
        @property (strong,nonatomic) UITextView *messageBox;
    
        @end
    
        @implementation MyItemListTVC
    
    
        @synthesize refreshControl;
        @synthesize itemList;
    
        - (id)initWithStyle:(UITableViewStyle)style
        {
            self = [super initWithStyle:style];
            if (self) {
                // Custom initialization
            }
            return self;
        }
    
        - (void)viewDidLoad
        {
            [super viewDidLoad];
            self.itemList=[(AppDelegate *)[UIApplication sharedApplication].delegate getitems];
    
            // Uncomment the following line to preserve selection between presentations.
            // self.clearsSelectionOnViewWillAppear = NO;
    
            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem;
            // Do any additional setup after loading the view, typically from a nib.
            self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
            self.addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    
            self.addButton.enabled = false;
    
            //UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)];
            [self.navigationItem setRightBarButtonItems:@[self.addButton] animated:YES];
        }
    
    
    
        - (void)didReceiveMemoryWarning
        {
            [super didReceiveMemoryWarning];
            // Dispose of any resources that can be recreated.
        }
    
        - (void)insertNewObject:(id)__unused sender
        {
    
            NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.itemList];
    
            self.itemList = nil;
            [self.tableView reloadData];
    
            [temp addObject:self.messageBox.text];
            [[NSUserDefaults standardUserDefaults] setObject:temp  forKey:@"items"];
            self.itemList=[(AppDelegate *)[UIApplication sharedApplication].delegate getitems];
            self.messageBox = nil;
            self.addButton.enabled = NO;
            [self.tableView reloadData];
    
            [self.tableView setNeedsDisplay];
            [CATransaction flush];
        }
    
        - (void) save:(id)__unused sender {
    
    
        }
    
    
    
        #pragma mark - Table View
    
    
        - (NSInteger)tableView:(UITableView *)__unused tableView numberOfRowsInSection:(NSInteger)section
        {
    
            return [self.itemList count]+1;
        }
    
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            UITableViewCell *cell;
            if (indexPath.row ==[self.itemList count]  ) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MessageBox Cell"];
    
                UITextView *messageBox= [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 100)];
    
                cell.userInteractionEnabled=YES;
                messageBox.delegate=self;
                [messageBox setEditable:YES];
                [messageBox setUserInteractionEnabled:YES];
                messageBox.editable=YES;
                messageBox.font = cell.textLabel.font;
                messageBox.textAlignment = NSTextAlignmentCenter;
                messageBox.textColor = [UIColor grayColor];
                messageBox.text = @"insert new activity";
                self.messageBox = messageBox;
                [cell addSubview: messageBox];
            }else{
                cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
                [self configureCell:cell atIndexPath:indexPath];
            }
            return cell;
        }
    
        - (BOOL)tableView:(UITableView *)__unused tableView canEditRowAtIndexPath:(NSIndexPath *)__unused indexPath
        {
            // Return NO if you do not want the specified item to be editable.
            return YES;
        }
    
        - (void)tableView:(UITableView *)__unused tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if ((editingStyle == UITableViewCellEditingStyleDelete)&&([self.itemList count]>indexPath.row)) {
    
                NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.itemList];
                [temp removeObjectAtIndex:indexPath.row];
                [[NSUserDefaults standardUserDefaults] setObject:temp  forKey:@"items"];
                self.itemList=[(AppDelegate *)[UIApplication sharedApplication].delegate getitems];
                [self resignFirstResponder];
    
                [self.tableView reloadData];
            }
        }
    
        - (BOOL)tableView:(UITableView *)__unused tableView canMoveRowAtIndexPath:(NSIndexPath *)__unused indexPath
        {
            // The table view should not be re-orderable.
            return YES;
        }
    
    
    
    
        - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
        {
            if ([self.itemList count] > 0){
    
                cell.textLabel.text = [self.itemList objectAtIndex:indexPath.row];
    
            }
        }
    
        #pragma mark - UITextViewDelegate
        - (void)textViewDidBeginEditing:(UITextView *)textView {
    
            textView.text = @"-ing";
            [self.messageBox setSelectedTextRange:[self.messageBox textRangeFromPosition:[self.messageBox positionFromPosition:self.messageBox.beginningOfDocument offset:1] toPosition:self.messageBox.beginningOfDocument]];
    
    
        }
        -(void)textViewDidChange:(UITextView *)textView
        {
            if (textView.text.length > 4) {
                self.addButton.enabled=YES;
            }else{
                self.addButton.enabled=NO;
            }
        }
    
        - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if ((indexPath.row ==[self.itemList count] ) ) {
                return UITableViewCellEditingStyleNone;
            }else{
                return UITableViewCellEditingStyleDelete;
            }
        }
    
        - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
            if ([text isEqualToString:@"\n"]) {
    
                if (textView.text.length > 4) {
                    [self insertNewObject:textView.text];
                }
                [self resignFirstResponder];
                return NO; // or true, whetever you's like
    
            }else if((textView.text.length-range.location)<4){
                return NO;
            }
    
            if (textView.text.length > 4) {
                self.addButton.enabled=YES;
            }else{
                 self.addButton.enabled=NO;
            }
            return YES;
        }
    
        @end
    
    0 讨论(0)
提交回复
热议问题