在日期之后显示两种不同类型的单元格 [英] Displaying two different types of cells after date

查看:45
本文介绍了在日期之后显示两种不同类型的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点棘手,但我将尽我所能解释.

This is kind of tricky but I will try to explain to the best of my abilities.

我有 3个数组,命名为:

  • allData
  • twitterData
  • feedData

allData 包含一个名为feedStruct的结构,该结构具有两个参数.类型和日期(两个字符串)

allData Contains a struct named feedStruct that has two parameters; type and date (both strings)

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

twitterData Contains JSON from twitter to display 3 tweets as cells in the TableViewController

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

feedData Contains JSON from my webpage and displays cells with that information. This JSON is not the same as the twitter JSON - they do not have the same parameters and therefore they are separated into two different arrays.

将JSON提取到twitterData和feedData中时,它们各自具有一个将其类型("twitter"或"web")和tweet/文章的日期作为unix戳添加到allData(feedStruct)数组中的函数.这样,我可以对allData数组进行排序,因此,单元格将显示为最新的,如下所示.

When the JSON is fetched into twitterData and feedData, they each have a function adding their type ("twitter" or "web") and the date of the tweet/article as a unix stamp into the allData (feedStruct) array. That way I can sort the allData array, so the cells are displayed newest first as seen below.

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

I then have this piece of code in my cellForRow function:

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单元比网络上最新添加的文章要新.但是,当文章比最近添加的tweet更新时, let tweet let item 的indexPath.row都将被弄乱,因为从两个数据中获取数据不同的数组.

The problem however is. At the moment, the three twitter cells are newer than the most recently added article from the web. But when the article is newer than the most recently added tweet, both the let tweet and the let item's indexPath.row will be messed up, as take data from two different arrays.

提要示例

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

希望它不会太混乱以至于无法理解.我根本不知道这是否是通过日期显示数据的方式(具有三个数组).感谢您抽出宝贵的时间阅读本文!

Hope it is not too messed up to understand. I do not at all know, if this is the way to go (with three arrays) in order to display the data via date. Thanks for taking your time to read this!

推荐答案

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

A pattern that I like to use is to define myself an enum that defines both the type of cell and the data to display. Instead of handling multiple collections, you have an heterogeneous data collection that lets you display your cells as defined per the enum type.

示例:

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

这篇关于在日期之后显示两种不同类型的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆