2012-01-31 53 views
2

私はいくつかのTabBarControllersを持つTabBarで構成されるアプリケーションを持っています。 1つのコントローラには非常に単純なテーブルがあり、NSMutableDictionaryの内容を表示することになっています。適切なボタンを押すと、辞書は別のコントローラで更新され、ビューはUITableViewControllerに切り替わり、新しく更新されたテーブルが表示されます。UITableViewは更新されません

辞書が更新されているのがわかります。しかし、TableViewは変更を反映しません。実際、私はその画面に初めて入るときだけ変更を表示するようです。

私は[self table.reloadData]を試していますが、呼び出されても変更内容はUITableViewに反映されません。

誰にも提案はありますか?私はコードを投稿してうれしく思いますが、何を投稿すればよいかわかりません。

更新:テーブルは、最初に表示されたときにのみ更新され、適切に更新されます。後続のディスプレイには、元の内容が表示されます。

背景: テーブルビューは、辞書:appDelegate.currentFaveから入力されます。 ViewControllerがTabBarControllerによって呼び出されるたびに、テーブルビューが更新されます。あなたが我慢しているコードが正しいと仮定

- (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

+0

これはコメントであり、回答ではありません –

0

昨日私はこの同じ問題を抱えていましたが、私にとっては間違ったファイル所有者をインターフェイスビルダーに設定していて、データソースをセットアップせず、テーブルビューの代理人を適切に設定していませんでした。

インタフェースビルダーにアクセスし、ファイル所有者を右クリックしてみてください。正しく接続されていないものがあれば表示されます。

+0

私はストーリーボードを使用していますので、ファイル所有者はありません。しかし、View Controllerを右クリックすると、データソースとdatadelegateの両方がUITableViewに設定されていることが示されます。それは正しい、そうです。 – Bassman

0

あなたがInterface Builderの接続が正しく設定されていることを確認する必要がありますが、どのようにこの問題が本当にはのように聞こえることはあなたがあなたのif(cell == nil)文の中cellForRowAtIndexPath:であなたのUITableViewCellのセットアップコードを持っているということです。それはいけません。私に説明させてください。あなたはセルのリストを持っている、とあなたは、配列内の文字列に各セルにタイトルを設定したい場合は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; 
} 

あなたが問題を見ることができますその論理と?再利用可能なセルが見つからない場合、セルには更新されたタイトルしか表示されません。アップルは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

+0

はい、間違いなく私たちに問題のあるコードを表示する必要があります –

+0

私は議論できるように私のコードの一部を掲載しました。 – Bassman

関連する問題