UITableView reloadData automatically calls resignFirstResponder

后端 未结 13 1395
天命终不由人
天命终不由人 2020-12-02 22:44

I have this UITableView with custom cells that can get only predefined values, therefore I use a UIPickerView as their inputView. All is jolly good until I edit a field and

相关标签:
13条回答
  • 2020-12-02 22:57

    This reads like expected behavior - the picker belongs to a particular cell, that cell gets reloaded and is not the first responder any more. I guess one had to select a specific element anyway for the picker to appear, i.e. to make it first responder.

    So you either need to make it become first responder again after reloading, or update the specific cell directly.

    0 讨论(0)
  • 2020-12-02 23:00

    I solved this by subclassing UITextView, overriding -(BOOL)resignFirstResponder and by adding a BOOL canResign. this variable is set before reloading the data and unset a short time after.

    0 讨论(0)
  • 2020-12-02 23:00

    I put my UISearchBar in its own section in a UITableView. When firing off the search, I made sure to only refresh the sections which do not contain the search bar.

    - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    
    0 讨论(0)
  • 2020-12-02 23:01

    adding:

    [yourSearchBar becomeFirstResponder];
    

    after your:

    [_tableView reloadData];
    

    did the trick

    0 讨论(0)
  • 2020-12-02 23:03

    Track which cell's keyboard is active and then get that particular cell by cellForRowAtIndexPath and make textView firstResponder

    self.tableView.reloadData()
    if let indexPath = self.activeIndexPath{
       if let cell = createFormTableView.cellForRow(at: indexPath) as? TextViewTableViewCell {
            cell.txtViewInput.becomeFirstResponder()
       }
    }
    
    0 讨论(0)
  • 2020-12-02 23:04
    customTextField.canResign = NO;
    [self.tableView reloadData];
    customTextField.canResign = YES;
    

    Custom text field is derived from UITextField.

    .h

    @interface CustomTextField : UITextField
    @property (nonatomic,assign) BOOL canResign;
    @end
    

    .m

    - (BOOL)canResignFirstResponder
    {
        return self.canResign;
    }
    

    Make sure that your custom text field is not recreated on table view reloading.

    0 讨论(0)
提交回复
热议问题