ios - Dismiss keyboard for UITextfield in UITableView cell -
i have uitableview i've assigned uitextfield each cell. want able accept input each text field , dismiss keyboard when user taps anywhere on screen other keyboard. code have far, find keyboard gets dismissed when im on last cell in table.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [self.gradestableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; self.tf = [[uitextfield alloc] initwithframe:cgrectmake(225, (cell.contentview.bounds.size.height-30)/2, 50, 30)]; [self.tf setdelegate: self]; self.tf.tag = indexpath.row; self.tf.textalignment = nstextalignmentcenter; self.tf.placeholder = @"0"; self.tf.backgroundcolor = [uicolor graycolor]; self.tf.borderstyle = uitextborderstyleroundedrect; self.tf.keyboardtype = uikeyboardtypedecimalpad; [cell addsubview:self.tf]; cell.textlabel.text = [self.adderarraylabels objectatindex:indexpath.section]; return cell; } - (void)textfielddidbeginediting:(uitextfield *)textfield{ self.tapgr = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tap)]; [self.view addgesturerecognizer:self.tapgr]; nslog(@"started editing"); } ive tried both endediting: , resignfirstresponder both dismiss keyboard when im on textfield in last cell.
- (void)tap { [self.tf endediting:yes]; //[self.tf resignfirstresponder]; nslog(@"tap called"); self.tapgr.enabled = no; } with nslog statements in code can confirm method tap called every time appropriate tap gesture recognized still keyboard stays. how fix this?
the problem here:
self.tf your class has text field property, , every time create new text field, assign property. then, try endediting: or resignfirstresponder on property, text field on cell created.
you don't need property @ , can use local text field variable when creating cells.
then change tap method this:
- (void)tap { [self.view endediting:yes]; nslog(@"tap called"); self.tapgr.enabled = no; } and truly, method should be: - (void)tap:(id)sender;
also, commented, gesture recognizer should added in viewdidload. need add once, not each , every time text field begins editing. reason add every time text field begins editing if you're removing every time text field ends editing... method gesture calls gets rid of keyboard, see no reason that.
Comments
Post a Comment