9

問題のアプリには、ユーザーがアイテムをお気に入りとしてマークする機能があります。ユーザーがお気に入りを保存していない場合は、その事実を通知したいと思います (ほとんどの場合、空の tableView のアイデアは嫌いです)。

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

numberOfRowsInSection0 に遭遇したときに結果を返すようにテストし、次に 1 行をテストしてcellForRowAtIndexPathカスタム テキストを挿入することはできますが、お気に入りが 1 つしかない場合はどうでしょうか。

アップデート

上記のアイデアと以下で推奨するアイデアを実装しようとしましたが、変更が発生したときにテーブルを更新するデリゲート メソッドを備えた fetchedResultsController であるという事実のために、おそらく正しく実行していません。

テーブルにセルが 1 つしかないときにセルを削除すると、このエラーが発生します。

*** 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)

最初に表示するセルがない場合、次のようにクラッシュします。

*** 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];
}
}
4

3 に答える 3

31

1) 自分の iOS アプリで、「結果がありません」という UILabel を含むサブビューを作成します。 numberOfSectionsInTableView がゼロの場合、サブビューをテーブルビューに追加します。!= ゼロの場合は削除します。これにはもちろん、サブビューを削除できるように、保持オブジェクトとしてメモリ内に維持する必要があります。

2)あなたが言及したように1つのセルを作成したい場合:numberOfSectionsInTableViewがゼロの場合、1を返します。次に、numberOfRowsInSectionでも1を返します。cellForRowAtIndexPath で、numberOfSectionsInTableView と numberOfRowsInSection の両方を確認し、それらが 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];
    }
于 2011-01-30T01:34:47.327 に答える
9

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

次に、 cellForRowAtIndexPath でカウントを再度確認する必要があります。0 が返された場合は、テーブル ビューが「現在お気に入りがありません」を描画しようとしていることがわかり、テキストを設定できます。

于 2011-01-30T01:31:26.163 に答える
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;
}
于 2015-03-03T11:07:29.780 に答える