Reload the UITable on the fly when editing a UITextField inside one of the cells

I have a UITable containing some UITableCells. These cells contain several UILabels and UITextField. The table data source comes from the main controller property. (This controller is also the delegate and data source for the table).

Here's a simplified screenshot of the user interface:

schreenshot

Now I need to update the contents of all UILabels on the fly when the user edits one of the UITextFields. To do this, I listen to the "Edited Changed" event at the UITextField level. This causes the following action:

- (IBAction) editChangeHandler: (id) sender { MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; [[delegate.viewController.myDataSourceArray objectAtIndex:self.rowIndex] setANumber: [theTextField.text intValue]]; [delegate.viewController reloadRows]; } 

The reloadRows method in viewController is as follows:

 - (void) reloadRows { NSLog(@"called reloadRows"); //perform some calculations on the data source objects here... [theUITable reloadData]; } 

My problem is that whenever the user changes the value in the field, the reloadRows method is called successfully, so apparently this is reloadData, but also causes the keyboard to be rejected.

Thus, at the end, the user can touch only one key when editing the TextField before the keyboard is fired and the table reloads.

Does anyone know about this solution or have experienced the same problem?

+6
source share
2 answers

You can selectively change rows as needed, instead of updating the entire table (this update is an update to the cell in which you are currently working, resetting its state).

Take a look:

 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

In addition, you can edit these cells directly, getting the cell through:

 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 

if you do not want to rebuild these cells.

+3
source

I ran into the same problem. In my case, I tried to create a common controller, so writing a specific method for manually rebuilding cells was not a good option.

Here is the solution I came up with:

  • Temporarily make a text box in some other cell in the table will become the first responder.
  • Call reloadRowsAtIndexPaths to reload the cell that your user edited.
  • Make the field the user edits again the first responder.

It works like a charm. The keyboard remains visible without blinking at all, the cell is updated properly, and the user can continue to type in the same field that they originally edited.

+3
source

Source: https://habr.com/ru/post/902066/


All Articles