2011-01-30 6 views
9

問題のアプリにはユーザーがアイテムをお気に入りとしてマークする機能があります。ユーザーにお気に入りが保存されていないときは、その旨を通知したいと思います(空白のtableViewのアイデアはほとんどありません)。tableViewのデータがないときにカスタムラベルを表示

私のnumberOfRowsInSectionは、ユーザーがお気に入りとしてマークしたアイテムがない場合はゼロです。私はcell.textLabel.text = @ "あなたはお気に入りがありません"と設定したいのですが、テーブルの項目がないときはcellForRowAtIndexPathが呼び出されません。

0に遭遇した後、cellForRowAtIndexPathで1行をテストしてから、カスタムテキストを挿入すると、結果が出るかもしれませんが、お気に入りが1つしかない場合はどうなりますか?私は上記の持っていた、と下のお勧めしますが、おそらく私はおそらく、それはときに、テーブルを更新するために、デリゲートメソッドでfetchedResultsControllerだという事実のために、右のそれをやっていないよなアイデアを実装しようとした

UPDATE

変更が発生します。

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976 Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted). with userInfo (null)

、それはでクラッシュ最初の場所に表示すべきセルが存在しない:

私はしかし、1個のテーブル内のセルがある場合、私は、セルを削除すると、このエラーが出ますここで

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFBatchFaultingArray objectAtIndex:]: index (0) beyond bounds (0)'

関連するコードです:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; 

    if ([sectionInfo numberOfObjects]==0) { 
     return 1; 
    } else { 
     return [sectionInfo numberOfObjects]; 
    } 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

if ([[_fetchedResultsController sections] objectAtIndex:0]==0) { 
    cell.textLabel.text = @"You have no favorites"; 
} else { 
    Shows *show = [_fetchedResultsController objectAtIndexPath:indexPath]; 

    cell.textLabel.text = show.ShowsToSerials.serialName; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", show.showTitle]; 
} 
} 

答えて

30

1)私自身のiOSアプリケーションでは、「結果なし」と言って、UILabelでサブビューを作成します。 numberOfSectionsInTableViewがゼロの場合、サブビューをtableviewに追加します。それが!=ゼロのとき、私はそれを削除します。 これはもちろん、サブビューを保持オブジェクトとしてメモリ内に保持する必要があるため、削除することができます。

numberOfSectionsInTableViewがゼロの場合、1を返します。次に、numberOfRowsInSectionで1を返します。 cellForRowAtIndexPathでは、numberOfSectionsInTableViewとnumberOfRowsInSectionの両方をチェックしてください.SHOULDが0(1を返した後)であれば、何らかのメッセージを表示してください。

ソリューション1では、サブビューを保持する変数を維持する必要がありますが、コードはより短くクリーンです.CPUの使用量が少なくてすみます(重要なパフォーマンス上の問題を引き起こすことはありません)。

コードを組み込むように編集: 実際には思ったより少し違っていましたが、基本的に同じです。 ここには、コードと味に合わせて変更する必要があることは明らかです。

のUIView * nomatchesViewを宣言することを確認します。.hの中

- (void)viewDidLoad{ 

     nomatchesView = [[UIView alloc] initWithFrame:self.view.frame]; 
     nomatchesView.backgroundColor = [UIColor clearColor]; 

     UILabel *matchesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,320)]; 
     matchesLabel.font = [UIFont boldSystemFontOfSize:18]; 
     matchesLabel.minimumFontSize = 12.0f; 
     matchesLabel.numberOfLines = 1; 
     matchesLabel.lineBreakMode = UILineBreakModeWordWrap; 
     matchesLabel.shadowColor = [UIColor lightTextColor]; 
     matchesLabel.textColor = [UIColor darkGrayColor]; 
     matchesLabel.shadowOffset = CGSizeMake(0, 1); 
     matchesLabel.backgroundColor = [UIColor clearColor]; 
     matchesLabel.textAlignment = UITextAlignmentCenter; 

     //Here is the text for when there are no results 
     matchesLabel.text = @"No Matches"; 


     nomatchesView.hidden = YES; 
     [nomatchesView addSubview:matchesLabel]; 
     [self.tableView insertSubview:nomatchesView belowSubview:self.tableView]; 
    } 



    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     //If there is no table data, unhide the "No matches" view 
     if([data count] == 0){ 
     nomatchesView.hidden = NO; 
     } else { 
     nomatchesView.hidden = YES; 
     } 

     return [data count]; 
    } 
+1

ソリューション1のコードを投稿してもよろしいですか? –

+0

ダニエル。コードにもう一度感謝します。メモリ管理に関する単なる質問です。私はdeallocでそのビューを解放します(それは最善でしょうか?)しかし、どこでラベルをリリースすべきですか? –

+0

私はビューを持っており、すべてがdeallocでリリースされています。私は何かが私のために漏れているとは思わない。 –

9

私が個人的にすることは次のとおりです。 あなたはnumberOfRowsInSectionメソッドで自分の言ったように、1つのセルを表示するために0を返す必要がある場合、tableViewデータソースの数をチェックする必要があります。

次に、cellForRowAtIndexPathでカウントをもう一度チェックする必要があります。0を返した場合は、テーブルビューが "あなたは現在お気に入りがありません"を描画しようとしていて、テキストを設定できます。

+1

これはまた、優れた提案です。 –

5

我々はラベルを作成してのUITableView等の背景ビューとしてあることを設定した場合、それは簡単で、以下のリンクで述べたように以下。 http://www.appcoda.com/pull-to-refresh-uitableview-empty/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    if (data) { 

     self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     return 1; 

    } else { 

     // Display a message when the table is empty 
     UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 

     messageLabel.text = @"No data is currently available. Please pull down to refresh."; 
     messageLabel.textColor = [UIColor blackColor]; 
     messageLabel.numberOfLines = 0; 
     messageLabel.textAlignment = NSTextAlignmentCenter; 
     messageLabel.font = [UIFont fontWithName:@"Palatino-Italic" size:20]; 
     [messageLabel sizeToFit]; 

     self.tableView.backgroundView = messageLabel; 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    } 

    return 0; 
} 
+0

ありがとうございます。大丈夫だよ。 – Raja

関連する問題