0

IB にプロトタイプのテーブルビューがあります。これには、完全なセルと基本セルの 2 種類のセルがあります (記事を表示するために、記事の種類に応じてそれぞれを使用します)。

FetchedResultsController をアプリに統合して、CoreData を使用してテーブルビューを設定できるようにしたいのですが、以前は (FetchedResultsController の代わりに NSArray を使用して) セルのセットアップを次のように処理しました。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    static NSString *CellIdentifier = nil;
    Article *article = self.articles[row];

    // Checks if user simply added a body of text (not from a source or URL)
    if (article.isUserAddedText) {
        CellIdentifier = @"BasicArticleCell";
    }
    else {
        CellIdentifier = @"FullArticleCell";
    }

    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // If the user simply added a body of text, only articlePreview and progress has to be set
    cell.articlePreview.text = article.preview;
    cell.articleProgress.text = [self calculateTimeLeft:article];

    // If it's from a URL or a source, set title and URL
    if (!article.isUserAddedText) {
        cell.articleTitle.text = article.title;
        cell.articleURL.text = article.URL;
    }

    return cell;
}

しかし、基本的な記事かどうかを確認する方法がわかりません (前に NSArray の Article オブジェクトのプロパティを確認したように)。私が見たある記事はこれをしました:

- (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Set up the cell...
    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

しかし、セル識別子が何であるかを決定する前に、それがどのような種類の記事であるかを知る必要があるため、ここでそれを行う方法がわかりません。FetchedResultController オブジェクトを早期に取得し、クエリを実行して article プロパティの値 (基本的かどうか) を確認し、それに応じて CellIdentifier を設定することはできますか? それとも他にやるべきことがありますか?

TL;DR: FetchedResultsController を使用するときに、セルに表示されるオブジェクトの種類に応じて CellIdentifier を決定するにはどうすればよいですか。

4

2 に答える 2

1

fetchedResultsController からオブジェクトを取得するときに、型を確認し、返されたものに基づいて作成するセルの型を決定できます。例えば:

id result = [fetchedResultsController objectAtIndexPath:indexPath];
if ([result isKindOfClass:[MyObject class]]) {
   // It's a MyObject, so create and configure an appropriate cell
} else ...
于 2013-04-05T20:20:43.050 に答える
1

Articleルックスの検索が少し異なる前に行っていたのとまったく同じロジックを使用できます

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  Article *article = [self.fetchedResultsController objectAtIndexPath:indexPath];

  NSString *CellIdentifier = [article isUserAddedText] ? @"BasicArticleCell" : @"FullArticleCell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  // Set up the cell...
  [self configureCell:cell atIndexPath:indexPath];

  return cell;
}
于 2013-04-05T20:23:58.397 に答える