Vishnu
Vishnu

Reputation: 208

Cannot Reload Tableview from custom cell

I have a UITableView with custom tableview cells. One of my Cell has a UITextField, I'm handling the textfield delegate methods in the custom tableviewCell class. I need to reload the tableview once the user entered a text, I had done the below thing but not worked , Any idea please help me.

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    UITableView *parentTable = (UITableView *)self.superview;
    [parentTable reloadData];
}

Upvotes: 0

Views: 1646

Answers (10)

Prabhu Anbu
Prabhu Anbu

Reputation: 51

Try this,

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
   [self.yourTableName reloadData];
   [self.yourTableName reloadInputViews];
}

Upvotes: 0

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableReload++;
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d%d",indexPath.row,tableReload];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        cell.textLabel.text = @"Hello";


    }
    // Configure the cell.
    return cell;
}

Upvotes: 0

Maulik
Maulik

Reputation: 19418

By (UITableView *)self.superview; you will not get the table view. Try below code :

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
      YourCell *cell = (YourCell *)[[textField superview] superview]; // you will get cell
      UITableView *table = cell.superview; // you will get table view
      [table reloadData];
}

Upvotes: 0

Ashim
Ashim

Reputation: 286

Don't reload whole table view for that small operation. Just reload the Cell in which the text field is. Also didnot forget to update your data source as table view cell fetch the data from the data source while the cell is loading.

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
     [tableViewDataSource addObject:textField.text];//this will fill the data source with data of textfield
    CustomCell *cell = (CustomCell *)textField.superview.superview;
    NSIndexPath *indexPath = [parentTable indexPathForCell:cell];
    [parentTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

Upvotes: 0

Gaurav Patel
Gaurav Patel

Reputation: 957

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
UITableView *parentTable = (UITableView *)textField.superview.superview;
[parentTable reloadData];
}

Upvotes: 0

iCoder
iCoder

Reputation: 1645

CustomCell.h

@property (nonatomic, copy) void(^tapHandler)(NSUInteger tag);

CustomCell.m

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSUInteger tag = 10; //Need to change the tag
    self.tapHandler(10);
}

Controller.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
 {
    // Whatever content was previously there. Add the below line in addition

     cell.tapHandler = ^(NSUInteger tag){
        [tableView reloadData];
    };

    return cell
 }

Hope it helps!

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 40008

It's better to keep a reference of table view in CustomCell

 // interface
 UITableView *tableView;

 // propery 
 @property (nonatomic)UITableView * tableView;

Then

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //your stuff
  cell.tableView= tableView;
}

Upvotes: 0

sagarcool89
sagarcool89

Reputation: 1376

Register the view controller to receive notifications that the data has been changed, and have it refresh the table when it receives one. Then have the parser send it out.

Registering for it is easy:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(reloadTableView:)
                                             name:@"reloadTableView"
                                           object:nil];

Your refresh method needs to be set up to receive these notifications, along these lines:

- (void)reloadTableView:(NSNotification *)notif {
    [self.yourTableName reloadData];
}

And it's important to stop observing in your ViewDidUnload:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Then in the parser you need to simply add this when it's complete:

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTableView" 
                                                    object:nil];

The view controller (and anyone else observing the notification with that name) will get the message and perform its task.

Thanks..

Upvotes: 3

Krunal
Krunal

Reputation: 6490

Try this,

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [self.parentTable reloadData];
}

Upvotes: 0

adali
adali

Reputation: 5977

first , the superview of the textfiled is not UITableview, it is tableviewcell or tableviewcell.contentview (depends on your code)

then, you just need to set the tableview as your view controller's member ,or property,

then

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [self.tableview reloadData];
}

Upvotes: 0

Related Questions