Когда я обновляю свои структуры данных, которые подпитывают UITableView, я вызываю relaydData. Однако, когда я представляю свой UITableView, новые значения не отображаются. Только в первый раз значения отображаются. Впоследствии, когда новые значения обновляются, UITableView показывает старые значения еще. Я не вижу, как вызывается cellForRowAtIndexPath. Как мне показать мои новые данные?
Вот код:
@implementation MyViewController
@synthesize tableView;
@synthesize maxEntries, selectedIndex;
@dynamic names, quotes;
// used pressed the "Done" button
- (IBAction)done
{
selectedIndex = -1;
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
selectedIndex = -1;
[tableView reloadData];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return maxEntries;
}
- (int)getSelectedIndex
{
return selectedIndex;
}
- (void)resetSelectedIndex
{
selectedIndex = -1;
}
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
selectedIndex = indexPath.row;
[self dismissModalViewControllerAnimated:YES];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kPlacemarkCellID = @"GrapeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kPlacemarkCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kPlacemarkCellID] autorelease];
}
CGRect frame = cell.textLabel.frame;
frame.size.width = 200;
cell.textLabel.frame = frame;
if ((int)indexPath.row < maxEntries)
{
if (names[indexPath.row] != nil)
{
cell.detailTextLabel.text = names[indexPath.row];
}
else
{
cell.textLabel.text = @"...";
}
if (quotes[indexPath.row] != nil)
{
cell.textLabel.text = quotes[indexPath.row];
}
else
{
cell.detailTextLabel.text = @"...";
}
}
else
{
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"";
}
return cell;
}
- (void)addEntry: (NSString *)name withQuote:(NSString *)quote
{
if (maxEntries < MAX_ENTRIES)
{
names[maxEntries] = [name copy];
quotes[maxEntries] = [quote copy];
maxEntries++;
[tableView reloadData];
}
}
- (void) clearEntries
{
maxEntries = 0;
selectedIndex = -1;
[tableView reloadData];
}
- (void)viewDidUnload
{
self.tableView = nil;
}
- (void)dealloc
{
[tableView release];
[super dealloc];
}
@end