问题
I put a textfield in every cell of the table. And after editing the textfield, an EditingDidEnd
Event will trigger. In the method of hanlding this event I try to use
[XXXUITableViewController.tableView reloadData];
but it doesn't work(the delegate method is not called).
If I try to reloadData
in someway like hanlding Tapgesture, it works just fine. I can use anthoer way to make my app work, but it's better to know why is reloadData not working. Any ideas, thanks a lot.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ParCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
for(UIView * old in cell.contentView.subviews){
[old removeFromSuperview];
}
//add a textfield in Table Cell View
ParticleConfigCellView * parCell=[[[NSBundle mainBundle] loadNibNamed:@"ParticleConfigCellView" owner:self options:nil]objectAtIndex:0];
[parCell refreshFromDataSource:[self.dataContainer.data_particleConifig objectAtIndex:indexPath.row]];
[cell.contentView addSubview:parCell];
return cell;
}
- (IBAction)nameChange:(id)sender {
[self savedata];
[self.tableView reloadData];//(not working. Table view delegate methods are not called.)
}
回答1:
Here in this method also reload your array which is showing in table view because after saving new data you are not reloading new data so that its showing old data only.
- (IBAction)nameChange:(id)sender {
[self savedata];
[yourArray removeAllObjects];
//then here put new that in yourArray
[self.tableView reloadData];//(not working)
}
Edit:
Also call this method [self.tableView reloadData];
in the last line of your savedata
method.
回答2:
Each time you create a cell, you must assign a delegate text field that is in it. If you are using a dynamic table, add this code cell.yourTextField.delegate = self
in this method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
.
Or maybe you forgot to subscribe your class to the protocol?
In any case, the method reloadData
updates the data in the table from its data source, and I do not understand why you need to call this method after you enter text in a field that is in the table. If you don't save this text, it will disappear after call reloadData
method.
来源:https://stackoverflow.com/questions/21953009/uitableview-reloaddata-not-working-when-put-it-in-editing-did-end-ibaction-btte