gpt4 book ai didi

ios - 在日期后显示两种不同类型的单元格

转载 作者:行者123 更新时间:2023-11-30 11:50:03 25 4
gpt4 key购买 nike

这有点棘手,但我会尽力解释。

我有 3 个数组,名称为:

  • 所有数据
  • 推特数据
  • 提要数据

所有数据包含一个名为 feedStruct 的结构,它有两个参数;类型和日期(均为字符串)

twitterData包含来自 Twitter 的 JSON,用于在 TableViewController 中将 3 条推文显示为单元格

feedData包含我网页中的 JSON 并显示包含该信息的单元格。此 JSON 与 twitter JSON 不同 - 它们没有相同的参数,因此它们被分成两个不同的数组。

当 JSON 被提取到 twitterData 和 feedData 中时,它们每个都有一个函数将其类型(“twitter”或“web”)和推文/文章的日期作为 unix 标记添加到 allData (feedStruct) 数组中。这样我就可以对 allData 数组进行排序,因此首先显示最新的单元格,如下所示。

然后我的 cellForRow 函数中有这段代码:

let sortedData = allData.sorted{ $0.date! > $1.date! }

if (sortedData[indexPath.row].type == "twitter") {
// Displaying twitter cell
let tweet = tweetData[indexPath.row]

return twitterCell



} else {

// Displaying web cell

let item: LocationModel = feedItems[indexPath.row - tweetData.count] as! LocationModel


return cell
}

但是问题是。目前,这三个 Twitter 单元比网络上最近添加的文章更新。但是,当文章比最近添加的推文更新时,let tweetlet item 的 indexPath.row 都会困惑,因为从两个中获取数据不同的数组。

提要示例

  • 文章 (indexPath.row = 0)
  • 推文 (indexPath.row = 1)
  • 推文 (indexPath.row = 2)
  • Tweet (indexPath.row = 3)(只有3条数据在数组中,这里要求的是第 4 个位置)
  • 文章 (indexPath.row = 4)(由于推文的原因,跳转到第 5 篇文章,而不是第 2 篇文章)
  • 文章 (indexPath.row = 5)
  • 文章 (indexPath.row = 6)

希望它不会太困惑而难以理解。我根本不知道这是否是通过日期显示数据的方法(使用三个数组)。感谢您花时间阅读本文!

最佳答案

我喜欢使用的一种模式是为自己定义一个枚举,它定义单元格的类型和要显示的数据。您无需处理多个集合,而是拥有一个异构数据集合,可让您按照枚举类型的定义显示单元格。

示例:

class YourViewClass {
enum CollectionViewItem {
case Article(data: YourArticleData)
case Tweet(data: YourTweetData)
}

private let tableView = UITableView()

fileprivate var allData: [CollectionViewItem] = []
private var tweetData: [YourTweetData]
private var articleData: [YourArticleData]

init() {
super.init(frame: .zero)

tableView.delegate = self
tableView.dataSource = self
tableView.register(ArticleCell.self, reuseIdentifier: ArticleCell.reuseIdentifier)
tableView.register(TweetCell.self, reuseIdentifier: TweetCell.reuseIdentifier)
addSubview(tableView)
}

func configure(tweetData: YourTweetData) {
self.tweetData = tweetData
mergeAndSort()
}

func configure(articleData: YourArticleData) {
self.articleData = articleData
mergeAndSort()
}

private func mergeAndSort() {
allData = tweetData.map({ tweet in return CollectionViewItem.Tweet(data: tweet) }) + articleData.map({ article in return CollectionViewItem.Article(data: article) })
allData.sort() // You will have to figure out how you sort your elements
}
}

extension YourViewClass: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch allData[indexPath.row] {
case .Article(let articleData):
let cell = tableView.dequeueReusableCell(withIdentifier: ArticleCell.reuseIdentifier, for: indexPath) as! ArticleCell
cell.configure(data: articleData)
return cell
case .Tweet(let tweetData):
let cell = tableView.dequeueReusableCell(withIdentifier: TweetCell.reuseIdentifier, for: indexPath) as! TweetCell
cell.configure(data: tweetData)
return cell
}
}

// Assuming theres only 1 section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allData.count
}
}

关于ios - 在日期后显示两种不同类型的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48428042/

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