Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:22:12+00:00 2026-06-15T16:22:12+00:00

Background I have a class with a UITableView ivar named _tableView. The class implements

  • 0

Background

I have a class with a UITableView ivar named _tableView. The class implements UITableViewDatasource and UITableViewDelegate _tableView. The UITableView cells are instances of FormTableCell. FormTableCell is a subclass of UITableViewCell, with a UILabel and a UITextField. On the class I have a property called textFieldDelegate. When tableView:cellForIndexPath: is called the current textFieldDelegate is assigned to the textField property of the returned cell. Also when the classes textFieldDelegate is modified I want to update the cells textfield’s delegate. I have come up with three ways of doing this, but don’t know the best way.

  1. Update the delegate of each visible cell with the new textFieldDelegate.
  2. Iterate through all the cells and assign the new textFieldDelegate
  3. reload the tableViews data.

Option 1 is good, but it assumes tableView:cellForIndexPath will be called before displaying any cells not in the tableView’s visibleCells array. If this does not happen then different cells could have different delegates and that’s not what I want.

Option 2 is the worst because it only works well for small numbers of cells, I would hope FormTables would always have small numbers of cell, but I never know.

Option 3 trumps Option 1 if my assumption that tableView:cellForIndexPath: will be called before displaying non-visible cells is invalid and it trumps Options 2 because it handles tables with lots of cells.

Question

Which is the best option and is my assumption that tableView:cellForIndexPath: will be called before a non-visible cell becomes visible or is interacted with incorrect?

Code

- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)textFieldDelegate
{
  if(_textFieldDelegate != textFieldDelegate) {
    _textFieldDelegate = textFieldDelegate;


    //Update _textFieldDelegate for all visible cells. 
    //!DEV: Assuming non-visible cells will be recreated with tableView:cellForIndexPath:
    for (FormTableCell *cell in [_tableView visibleCells]) {
      [[cell textField] setDelegate:_textFieldDelegate];
    }

    //!DEV: This will potentially generate memory issues for a large number of cells
    for(NSUInteger i = 0; i < kNumberOfFields; ++i) {
      NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(NSInteger)i inSection:0];
      FormTableCell *cell = (FormTableCell*)[_tableView cellForRowAtIndexPath:indexPath];
      [[cell textField] setDelegate:_textFieldDelegate];
    }

    //!DEV: Maybe this is the best method, but it requires the recreation of visibleCells.
    [_tableView reloadData];

  }
}

Final Status

So this is what I ended up doing. I implemented a Decorated Delegate(?)

- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)textFieldDelegate
{
  if(_textFieldDelegate != textFieldDelegate) {
    _textFieldDelegate = textFieldDelegate;
  }
}

#pragma mark - UITextFieldDelegate Implementation

//This class forwards UITextFieldDelegate methods on to _textFieldDelegate, if implemented.
//Provides default functionality is not implemented.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
  if([_textFieldDelegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
    return [_textFieldDelegate textFieldShouldBeginEditing:textField];
  } //implicit else
  return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {

  //Setup _closeKeyboardButton behind _tableView
  //view::closeKeyboardButton (_closeKeyboardButton)
  NSAssert(_closeKeyboardButton == nil, @"!DEV: Thought _closeKeyboardButton would always be nil here.");
  if(_closeKeyboardButton != nil) {
    [_closeKeyboardButton removeFromSuperview];
    _closeKeyboardButton == nil;
  }

  CGRect closeKeyboardFrame = {CGPointZero, [self frame].size};

  UIButton *closeKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [closeKeyboardButton setFrame:closeKeyboardFrame];

  [closeKeyboardButton addTarget:self
                          action:@selector(_closeKeyboardButtonTapped:)
                forControlEvents:UIControlEventTouchUpInside];

  [self insertSubview:closeKeyboardButton belowSubview:_tableView];
  _closeKeyboardButton = closeKeyboardButton;


  if([_textFieldDelegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
    [_textFieldDelegate textFieldDidBeginEditing:textField];
  }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
  if([_textFieldDelegate respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
    return [_textFieldDelegate textFieldShouldEndEditing:textField];
  } //implicit else
  return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  if([_textFieldDelegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
    [_textFieldDelegate textFieldDidBeginEditing:textField];
  }

  //Tear down _closeKeyboardButton
  NSAssert(_closeKeyboardButton != nil, @"!DEV: Thought _closeKeyboardButton would always exist here.");
  [_closeKeyboardButton removeFromSuperview];
  _closeKeyboardButton = nil;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  if([_textFieldDelegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
    return [_textFieldDelegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
  } //implicit else
  return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
  if([_textFieldDelegate respondsToSelector:@selector(textFieldShouldClear:)]) {
    return [_textFieldDelegate textFieldShouldClear:textField];
  } //implicit else
  return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  if([_textFieldDelegate respondsToSelector:@selector(textFieldShouldReturn:)]) {
    return [_textFieldDelegate textFieldShouldReturn:textField];
  } //implicit else
  return NO;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:22:13+00:00Added an answer on June 15, 2026 at 4:22 pm

    In my experience, there’s never any need to access the visibleCells property of the tableView.

    Do it all in your tableView:cellForRowAtIndexPath: method. It will be called each time a cell is created, or a recycled one is pulled out from a call to dequeReusableCell…
    That method will get called before any visible cells are on presented on screen.

    For educational purposes, you can throw in some NSLogs in all of your tableview methods to see in exactly what order they are called in, and what actions cause them to be invoked.

    Unrelated note — if you need to do any frame maniuplation to subviews in the cell, you can override the layoutSubviews method in your FormTableCell, or you can implement the tableView:willDisplayCell:forRowAtIndexPath: method and do it there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a class named testThing: .testThing { background-color:#000000; float:left; height:50px; width:50px; }
Background: I have enclosed (parent) class E with nested class N with several instances
I have a grouped UITableView with a custom UITableViewCell class and I am adding
I have a class named as @interface Appointment : UIViewController <UITableViewDelegate, UITextFieldDelegate, UIActionSheetDelegate> {
I have a background worker which calls a function within a separate class. This
I have an IValueConverter class that is used to change the background color of
I have a helper class that should only ever run in a background thread.
I basically have the following class: .sf-sub-indicator { background: url(/abcprod/images/arrows-ffffff.png) no-repeat -10px -100px; }
lets say i have a background worker in a class that perform db query
I have created a button CSS class that uses background images with different positions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.