【问题标题】:Displaying two different types of cells after date在日期之后显示两种不同类型的单元格
【发布时间】:2018-07-03 19:50:22
【问题描述】:

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

我有 3 个数组 命名为:

  • 所有数据
  • 推特数据
  • feedData

所有数据 包含一个名为 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 都会被弄乱,因为从两个不同的数组。

Feed 示例

  • 文章 (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)

希望它不会太混乱而无法理解。我根本不知道,如果这是通过日期显示数据的方式(使用三个数组)。 感谢您抽出宝贵时间阅读本文!

【问题讨论】:

  • 添加另一个类:Meta,具有属性类型、属性日期和属性内容,其中内容是文章、推文或您想要的任何内容。你用一篇文章或一条推文初始化,然后相应地计算类型和日期。您只使用bigArray 中的 Meta 对象,它将被正确排序。在cellForRowAtIndexPath 中,执行 `let meta = bigArray[indexPath.row];让细胞;切换 meta.type case tweet: cell = tableView.dequeue... as TweetCell, etc.

标签: ios arrays json swift twitter


【解决方案1】:

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

示例:

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
    }
}

【讨论】:

  • 既是 UITableViewDataSource 又是 UITableViewDelegate 的类应该包含此代码。根据您的架构,它可能位于容器视图或视图控制器中。
  • 如何使用TableViewController中的代码?我假设上面的代码应该在一个 swift 文件中?
  • 如果我错了,请纠正我,TableViewController 应该已经是 TableViewDataSourceTableViewDelegate。否则,您只需实现协议。您可以将这些方法添加到您的TableViewController 实现中。您只需使用我在编辑中添加的配置方法传递模型项列表。
  • 抱歉,我无法完成这项工作。代码中几乎每一行都有问题,包括。 “YourArticleData”和“YourTweetData”:/
  • YourArticleData 是您为文章对象定义的数据模型。 YourTweetData 是您为推文对象定义的数据模型。您可以将它们替换为您拥有的课程。如果您没有它们的模型对象,我建议您为此制作一个简单的结构/类。 ArticleCellTweetCell 也是如此(您需要为每个实现 UITableViewCell 的专业化)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多