UITableView reloadData不会导致表格单元格显示新数据

时间:2011-07-16 18:11:42

标签: cocoa-touch uitableview ios4

当我更新为UITableView提供动力的数据结构时,我调用了reladData。但是,当我呈现我的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

0 个答案:

没有答案