2012-01-31 94 views
2

我有一个应用程序组成TabBar与几个TabBarControllers。一个控制器包含一个非常简单的表格,它应该显示一个NSMutableDictionary的内容。当您点击相应的按钮时,字典将在单独的控制器中更新,并且视图切换到UITableViewController,显示新更新的表格。UITableView不刷新

我可以看到正在更新的字典。但是TableView从不反映这些变化。实际上,它似乎只在我第一次进入该屏幕时显示更改。

我已经尝试过[self table.reloadData],而它被调用时,更改不会反映到UITableView

有没有人有任何建议?我很高兴发布代码,但我不确定发布什么。

更新:仅在第一次显示表格时更新并刷新表格。随后的显示器只显示原始内容。

背景: tableview从字典中获取:appDelegate.currentFave。每次TabBarController调用ViewController时,tableview都会刷新。

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@"in viewWillAppear"); 

    [super viewWillAppear:animated]; 

    [self loadFavesFile]; 
    [self.tableView reloadData]; 
} 

// load the Favorites file from disk 
- (void) loadFavesFile 
{ 
// get location of file 
NSString *path = [self getFavesFilePath]; 

// The Favorites .plist data is different from the Affirmations in that it will never be stored in the bundle. Instead, 
// if it exists, then use it. If not, no problem. 
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { 

    // read Faves file and store it for later use... 
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 
    appDelegate.sharedData.dictFaves = tempDict;  

    // grab the latest quote. Append it to the list of existing favorites 
    NSString *key = [NSString stringWithFormat:@"%d", appDelegate.sharedData.dictFaves.count + 1]; 

    NSString *newFave = [NSString stringWithFormat:@"%@", appDelegate.currentFave]; 
    [appDelegate.sharedData.dictFaves setObject:newFave forKey:key]; 

} else { 
    NSLog(@"Favorites file doesn't exist"); 
    appDelegate.sharedData.dictFaves = nil;  
} 
} 


// this gets invoked the very first call. Only once per running of the App. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// reuse or create the cell 
static NSString *cellID = @"cellId"; 
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 
} 

// allow longer lines to wrap 
cell.textLabel.numberOfLines = 0; // Multiline 
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
cell.textLabel.font = [UIFont fontWithName:@"Chalkduster" size:(16)]; 
cell.textLabel.textColor = [UIColor yellowColor]; 

// NOTE: for reasons unknown, I cannot set either the cell- or table- background color. So it must be done using the Label. 

// set the text for the cell 
NSString *row = [NSString stringWithFormat:@"%d", indexPath.row + 1]; 
cell.textLabel.text = [appDelegate.sharedData.dictFaves objectForKey:row]; 
return cell;  
} 

回答

1

我发现了这个问题。我没有正确初始化并在我的视图控制器中分配TableView。看下面

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]  style:UITableViewStylePlain]; 

    tableView.dataSource = self; 
    tableView.delegate = self; 
    tableView.backgroundColor=[UIColor blackColor]; 
    self.view = tableView; 
} 
0

假设您提出的代码是正确的,您想要使用[self.table reloadData]。您的.位置错误。

+0

我的部分错别字。道歉。 – Bassman 2012-02-02 21:29:06

+0

这应该是一个评论,而不是答案 – 2012-02-11 23:06:44

0

昨天我也遇到了同样的问题,对我来说,事实证明我在界面构建器中设置了错误的文件所有者,并没有正确设置数据源和表视图的委托。

尝试进入界面生成器并右键单击文件所有者,这应该会告诉您是否有任何东西没有正确连接。

+0

我正在使用故事板,所以没有文件所有者。但是,右键单击视图控制器显示数据源和datadelegate都设置为UITableView。这是正确的,对。 – Bassman 2012-02-02 21:54:53

0

你应该确保你的界面生成器连接设置正确,但什么这个问题真的听起来就像是,你必须在你的cellForRowAtIndexPath: UITableViewCell中设置代码你if(cell == nil)语句中。它不应该是。让我解释。如果你有小区的名单,并要标题到每个单元格设置为所谓的myArray数组中的字符串,现在你的(不正确)的代码如下所示:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; 
    if (cell == nil) { 
     // No cell to reuse => create a new one 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"] autorelease]; 

     [[cell textLabel] setText:[myArray objectAtIndex:[indexPath row]]]; 
    } 
    return cell; 
} 

你能看到的问题用那个逻辑?如果没有找到可重复使用的单元格,单元格只会获得更新的标题,在您的情况下,这听起来就像情况一样。 Apple表示,每次调用cellForRowAtIndexPath:时应该创建一个“新”单元,这意味着您将所有设置代码放在if(cell == nil)检查的之外。

继续这个例子中,正确的代码应该是这样的:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; 
    if (cell == nil) { 
     // No cell to reuse => create a new one 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"] autorelease]; 
    } 

    [[cell textLabel] setText:[myArray objectAtIndex:[indexPath row]]]; 
    return cell; 
} 

这样,小区被分配正确的琴弦可重复使用的电池是否被发现,因此呼吁reloadData将有期望的效果。

+0

@ Rickay我检查了我的代码,并根据您的建议分配了单元格。我还有其他东西吗?我应该张贴一些代码吗? – Bassman 2012-02-02 21:44:36

+0

是的,你应该向我们展示有问题的代码 – 2012-02-02 22:56:27

+0

我发布了一些我的代码,所以我们可以讨论..... – Bassman 2012-02-03 00:52:16