【问题标题】:Displaying a Json on a CollectionViewCell inside a TableViewCell. Swift 3在 UITableViewCell 内的 uiCollectionViewCell 中显示 Json。斯威夫特 3
【发布时间】:2017-12-05 12:46:27
【问题描述】:

我正在尝试解析 Json 并将其显示为表视图单元格内的集合视图项。所以层次结构是这样的:

MainTableVIew -> TableViewCell -> CollectionView -> CollectionViewCell

所以我有 json

"payments":[{
    "type":"card",
    "title":"Card"
}, {
    "type":"sms",
    "title":"SMS",
    "max_amount":24
},{
    "type":"account",
    "title":"Account",
    "disabled":true,
    "max_amount":-1
}]

我解析了它。 我做的下一件事是在

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "NewPaymentCell") as! NewPaymentTableViewCell
            cell.package?.title = payments[indexPath.row].title
            return cell}

是哪个包

var package : PromoPayment? {
        didSet {
            collectionView.reloadData()
        }
    }

和 PromoPayment 我获取字典的类。

在 NewPaymentCollectionView 类中,我还像这样填充单元格:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewPaymentCollectionCell", for: indexPath) as! NewPaymentCollectionViewCell
        cell.paymentIcon.image = UIImage(named: iconImage[indexPath.row])
        cell.kidPayment?.title = (package?.title)!
        return cell
    }

kidPayment 在哪里

var kidPayment : PromoPayment? {
    didSet{
        namePaymentTypeLabel.text = kidPayment?.title
        }
    }

它在NewPaymentCollectionViewCell: UICollectionViewCell类中建模

当我调试它返回我零..

感谢您的帮助。

【问题讨论】:

    标签: ios json swift uitableview


    【解决方案1】:

    你可以试试这个,设计一个自定义的tableview单元格并在里面添加collection view。为集合视图创建一个出口并尝试按照给定的代码重新加载集合视图。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "NewPaymentCell") as! NewPaymentTableViewCell
                cell.package?.title = payments[indexPath.row].title
                **cell.collectionView.reloadData()**
                return cell
    }
    

    不要使用 didset 方法,而是使用 this。

    【讨论】:

    • 您好,感谢您的帮助。问题是我有一个 costum 单元和一个 collectionView 类,它是 NewPaymentCollectionView。我试过你说的,但没有。我在 CollectionViewCell 中进行了调试,它甚至没有进入它...
    • 以编程方式设置集合视图委托和数据源,就在重新加载集合视图之前。
    猜你喜欢
    • 1970-01-01
    • 2017-02-15
    • 2017-03-24
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多