2010-12-19 7 views
11

誰かが私にのUITableView細胞のインプレース編集の一例を示してくださいすることができます...私はcellForRowAtIndexPathようのUITableViewのデリゲートメソッドを認識してい、...などインプレース編集

しかし、私はしないでくださいセルのインプレーステキスト編集を許可する方法を知っていますか?
また、この値をコアデータと一緒に使用することができます。つまり、永続化することができます。

私が探しているものは、[設定] - > [Wi-Fi]の[ドメイン、クライアント、IP 、等の値を同じ場所に設定することができます。

フィールド値を制御する別のビューコントローラを持つインプレース編集Vを使用することの欠点もありますか?

答えて

18

UITextFieldをセルに追加します。

どちらの方法でも、あなたがCoreDataを使用しているかどうかに関わらず、情報を保存するには、それを保存する必要があります。テーブル上の編集メソッドを使用すると、textFieldデリゲートを使用して、ユーザーヒットの戻り値としてデータを保存できます。あなたの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

私が探しているものにぴったりのような気がしますが、Val1、Val2などの表のセルスタイルが何であるか、まだいくつか質問がありました)。 accessoryViewはaccessoryTypeと同じですか? また、この方法はインプレーステキスト編集にのみ使用できますか、それを使用して他のタイプを編集することができますか? – hmthur

+0

デフォルトのスタイルは問題ありません。 AccessoryViewを使用してacessoryTypeを複製することができるので、Disclusreボタンの代わりに、TextFieldで置き換えることができます。だから、accessoryTypeは必要ありません。このメソッドを使用して、日付、NUmbers、テキスト、考えられるものを編集できます。 – WrightsCS

0

これは...非常に古く、おそらくより最近の答えを必要とする..私はちょうどこの問題を抱えていたし、一緒に何かをハッキング..誰かが役に立つと思うかもしれません。

NSUserDefauに保存されている「アイテムリスト」の最後にメッセージボックスを追加するテーブルビューコントローラを作成しましたlts ... 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