gpt4 book ai didi

ios - 如果我有两种不同类型的单元格,我该如何使用 FetchedResultsController?

转载 作者:行者123 更新时间:2023-11-28 22:36:27 25 4
gpt4 key购买 nike

我在 IB 中有一个原型(prototype) TableView ,它有两种不同类型的单元格:完整单元格和基本单元格(用于显示文章,我根据文章类型使用每一种单元格)。

我想将 FetchedResultsController 集成到我的应用程序中,以便我可以使用 CoreData 来填充表格 View ,但之前(使用 NSArray 而不是 FetchedResultsController)我按如下方式处理单元格设置:

- (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 对象,查询它以查看文章属性的值(是否为基本值)并相应地设置 CellIdentifier?或者还有其他我应该做的事吗?

TL;DR:在使用 FetchedResultsController 时,如何根据要在单元格中显示的对象类型来决定 CellIdentifier。

最佳答案

您可以使用与之前完全相同的逻辑,只是检索 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;
}

关于ios - 如果我有两种不同类型的单元格,我该如何使用 FetchedResultsController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15842729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com