2010-12-19 17 views
11

कोई मुझे UITableView सेल की यथा-स्थान संपादन का एक उदाहरण दिखाने कृपया ... मैं cellForRowAtIndexPath तरह UITableView प्रतिनिधि तरीकों के बारे में पता कर रहा हूँ, ... आदियथा-स्थान UITableView सेल के संपादन

लेकिन मुझे नहीं पता सेल के इन-प्लेस टेक्स्ट संपादन की अनुमति कैसे दें?
इसके अलावा इस मूल्य कोर डाटा यानी यह कायम किया जा सकता है के साथ इस्तेमाल किया जा सकता ..

बात यह है कि मैं सेटिंग्स के तहत देखा जा सकता है के लिए देख रहा हूँ -> वाई-फाई, जहां आप उन क्षेत्रों डोमेन, ग्राहक, आईपी देखना , आदि जहां मूल्य एक ही स्थान पर सेट किया जा सकता है।

क्या इन-प्लेस संपादन वी का उपयोग करने के किसी भी नकारात्मक पक्ष में फ़ील्ड मान को नियंत्रित करने के लिए एक अलग दृश्य नियंत्रक है?

उत्तर

18

अपने सेल में UITextField जोड़ें।

किसी भी तरह से आप एक प्रविष्टि को संपादित करना चुनते हैं, चाहे आप कोरडाटा का उपयोग कर रहे हों, या जानकारी को स्टोर करने के लिए जो भी हो, आपको इसे बचाने के लिए किसी भी तरह की आवश्यकता है। यदि आप ऑन-टेबल संपादन विधि पर जाते हैं, तो आप डेटा को सहेजने के लिए टेक्स्टफ़ील्ड प्रतिनिधियों का उपयोग कर सकते हैं क्योंकि उपयोगकर्ता वापसी करता है।

अपने 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, आदि जैसे)। सहायक है accessoryType के रूप में देखें? क्या यह विधि केवल इन-प्लेस टेक्स्ट संपादन के लिए उपयोग की जा सकती है या क्या मैं इसे अन्य प्रकारों को संपादित करने के लिए उपयोग कर सकता हूं जैसे दिनांक कहें? – hmthur

+0

डिफ़ॉल्ट शैली ठीक है, क्योंकि आपके पास कोई विवरण नहीं है पाठ। AccessoryView का उपयोग AcessoryType को दोहराने के लिए किया जा सकता है, इसलिए एक डिस्क्लेयर बटन के बजाय, आप इसे टेक्स्टफ़िल्ल्ड से बदल सकते हैं। तो आपको एक एक्सेसरी टाइप की आवश्यकता नहीं होगी। आप इस विधि का उपयोग दिनांक, NUmbers, टेक्स्ट, जिसे आप सोच सकते हैं संपादित करने के लिए कर सकते हैं। – WrightsCS

0

यह बहुत पुरानी है .. और शायद एक और अधिक हाल के जवाब की जरूरत है .. मैं सिर्फ इस समस्या थी और साथ में कुछ काट दिया .. शायद किसी को यह उपयोगी लगेगा ..

मैंने एक टेबल व्यू कंट्रोलर बनाया है जो एक संदेश बॉक्स जोड़ता है जो NSUserDefau में संग्रहीत "आइटम सूची" का अंत जोड़ता है लीटर ... मद

// 
    // 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 
संबंधित मुद्दे