I have a table view with only one cell. When I call a method, I try to refresh it and the text label’s text should change. However it does not, it only changes if the view re appears.
I have added NSLog() in many places and the correct methods and conditions are called
I have tried a few things:
reloadData
setNeedsDisplay
setNeedsLayout
reloadCell
reloadRowsAtIndexPaths: withRowAnimation:
But nothing has worked, I know it is not a problem with my UITableViewDataSource code because it is properly getting called.
This is the most appropriate code I can give you:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tableView.scrollEnabled = NO;
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: @"cell"];
if (cell) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([[TravelLogViewController locationArray] count]) {
NSDictionary *flight = [NSDictionary dictionaryWithObjectsAndKeys:[[TravelLogViewController locationArray] lastObject], @"name", [[TravelLogViewController dateArray] lastObject], @"date", [[TravelLogViewController milesGainedIndex] lastObject], @"miles", nil];
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.textLabel.numberOfLines = 2;
cell.detailTextLabel.numberOfLines = 2;
cell.textLabel.font = [UIFont fontWithName:@"Avenir" size: 14.0];
cell.detailTextLabel.font = [UIFont fontWithName:@"Avenir" size: 12.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [flight objectForKey: @"name"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Flight Date: %@, Miles Gained: %.1f", [flight objectForKey: @"date"], [[flight objectForKey: @"miles"] doubleValue]];
cell.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
// cell.backgroundColor = [UIColor clearColor];
}
else {
// This gets called when I want it to, but the textLabel text or detailTextLabel text is not changed
cell.detailTextLabel.text = nil;
cell.textLabel.text = nil;
cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size: 18.0];
cell.textLabel.text = @"\n\n No Flights";
}
}
return cell;
}
UPDATE:
I am using ARC, and I have found that the table view is nil at the time I try to update the cell, but I have revised my cell and have no clue why!?
I am not nilling or releasing my table view anywhere, it should still be allocated
By the way I have a tab bar controller
UPDATE:
I have started a bounty now, its over a week later and I really feel this question needs some more attention and answers, I have checked my code multiple times and I cannot see why the table view is not updating / is nil.
UPDATE:
Ok, I have forgot to mention that the user can swipe to delete the cell. When he does, it does not actually delete the cell, it just changes its text (that is the part it is not updating). I put logs in viewDidLoad and viewDidAppear and table view appears to be valid, once the user swipes to delete… here is my code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
TravelLogViewController *travel = [[TravelLogViewController alloc] init];
[travel manuallyDeleteTopCellData]; // Deletes some data in other view controller and calls the method `proceedWithDeletion`
}
}
- (void)proceedWithDeletion {
// Method is called from another view controller
NSLog(@"%@", mostRecentFlight.description); // NIL HERE
[mostRecentFlight reloadData];
[mostRecentFlight setNeedsDisplay];
[mostRecentFlight setNeedsLayout];
}
TravelLogViewController
- (void)manuallyDeleteTopCell {
[TheMileIndexViewController deductDesiredMilesToIndex: [[milesGainedIndex lastObject] floatValue]];
[locationArray removeLastObject];
[milesGainedIndex removeLastObject];
[dateArray removeLastObject];
MenuMileIndexViewController *menu = [[MenuMileIndexViewController alloc] init];
[menu proceedWithDeletion];
}
So to sum up, one the view loads/appears, the table view is valid. When the user swipes to delete, it does not actually delete the row it just calls another view controller called TravelLogViewController method called manuallyDeleteTopCellData where it deletes some data, that method then calls another method in my original view controller called proceedWithDeletion where it is supposed to reload the table view, but doesn’t (the problem). The reason for this is because in proceedWithDeletion the table view is nil, but why!?
This code could be the source of your problem:
You’re creating a brand new
MenuMileIndexViewController, not accessing the already existing instance, which is what I presume you want to do. This new instance won’t have access to the original table view stored inmostRecentFlight. Inside-manuallyDeleteTopCell, you’ll need a reference to the callingMenuMileIndexViewControllerinstance.It might be as simple as passing
selfto-manuallyDeleteTopCellDataand using that variable to subsequently call-proceedWithDeletionon. For example: