简体   繁体   中英

UITableView doesn't reload

I have a problem with updating the UITableView . I call the reloadData method on it, but cellForRowAtIndexPath doesn't get called. Code:

//.h
@interface MasterViewController : UIViewController<ELCTextFieldDelegate, UITableViewDelegate, UITableViewDataSource> {
  //some variables
}
@property (nonatomic, retain) IBOutlet UITableView *theTableView;
//other properties

//.m
- (void)viewDidLoad {
  //some code
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillHide:(NSNotification *)notification {
  self.theTableView.frame = CGRectMake(0, 0, 320, 480 - 64);
  [self.theTableView reloadData];
}

I connected the theTableView with UITableView in the xib and I also set it's delegate and dataSource to File's Owner .

I use ELCTextFieldCell which provides me with a UITableViewCell with a UILabel and a UITextField . I insert some values in the cells and when the keyboard hides that tableView should reload it's data to show the correct result in one of the cells. I did some debugging and calculations work as it should, but that cell doesn't refresh(as I mentioned the cellForRowAtIndexPath method doesn't get called after [self.theTableView reloadData] .

Thank you.

Any help would be appreciated

EDIT

Found an "ugly" solution for that. Instead of calling

[self.theTableView reloadData];

I called

[self.theTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:7 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

It's ugly and hard-coded which may lead to problems later if I will add more cells or remove some, but it works.

Still looking for a solution that would use reloadData method

reloadData redisplays only visible rows (for efficiency). The fact that you set your tableview's frame just before reloadData makes me think that the tableview for some reason doesn't see that the particular cell is visible at that time, so it doesn't ask for a new one (and this explains why after scrolling down/up it refreshes correctly). Try to poll your tableview to see what reports as visible cells prior calling reloadData ( [theTableView visibleCells]; ). I hope that this will help you track the problem.

仅当您对表进行某些更改时,表才会重新加载.....

check if tableView is not nil... if so allocate memory

what is returning in the

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

and

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

both returning non zero value than it should be called

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM