iphone - UITableView 未刷新

标签 iphone ios uitableview reloaddata

我有一个应用程序,它由一个 TabBar 和几个 TabBarController 组成。一个 Controller 包含一个非常简单的表,它应该显示 NSMutableDictionary 的内容。当您点击适当的按钮时,词典会在单独的 Controller 中更新,并且 View 会切换到 UITableViewController,显示新更新的表格。

我可以看到词典正在更新。但是 TableView 从不反射(reflect)更改。事实上,它似乎只在我第一次进入该屏幕时显示更改。

我已尝试 [self table.reloadData],当它被调用时,更改不会反射(reflect)到 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;    
}

最佳答案

我发现了问题。我没有在我的 View Controller 中正确初始化和分配 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;
}

关于iphone - UITableView 未刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9072768/

相关文章:

iphone - 如何像 DJ 一样抓取音频?

iphone - 问在xcode4中使用json

android - Cordova 推送插件 : onNotificationGMC is not fired and cannot obtain regID

ios - 快速的 tableview 到 webview

ios - performSegueWithIdentifier 根本不工作

iphone - 如何设置导航栏中的事件指示器?

iphone - 删除 UIView

ios - MapKit 缩放到用户当前位置

iphone - 如何在 UIWebView 上显示垂直或水平滚动条?

ios - 动态单元出队导致应用程序崩溃