【问题标题】:In tableview Data is Overwriting在 tableview 数据被覆盖
【发布时间】:2016-09-02 13:40:34
【问题描述】:

我有两个collectionView。一是主项,二是子项 当我同时单击 collectionView 时,数据将添加到 tableView。

我的要求是,当我第一次点击collectionView 时,它将被添加到购物车中,tableViewCell 应该显示我从网络服务检索到的原始价格。

现在,当我点击第二个 collectionView 时,它也应该添加到 tableview 中,但是当我点击它时,它必须将价格显示为 0。 要添加到表视图中,我正在使用collectionView 的选择方法:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let  dictSub = arr[indexPath.row] as! [String : AnyObject]
    //to insert product to top of tableview
    arrTable(dictSub, atIndex: 0)
    self.tbl.reloadData()
}

在cellForRowAtIndexPath 方法中,我正在检查isMainProduct 是否等于一或零

代码:

if  isFromMain == "1" {
    let Price   = NSString(string: pPrice1).floatValue
    let total   = 1 * Price
    cell.lbl.text = String(format: "%.2f", total)
} else {
    let Price   = 0.00
    let total   = 1 * Price
    cell.lbl.text = String(format: "%.2f", total)
}

1 时添加原价,非主产品时显示 0。

问题是:

首先,当我点击主集合视图时显示原始价格,但当我点击子项目时,主产品的价格也变为零。

我想防止这些数据被覆盖。

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    在您的didSelectItemAtIndexPath中,您将Dictionary添加到Array,您可以使用此Dictionary 来区分它是否来自主collectionView,而不是将isFromMain 的实例值设置为1 .

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        var  dictSub = arr[indexPath.row] as! [String : AnyObject]  
        if collectionView == mainCollectionView {
            dictSub["isFromMain"] = "1"
        }
        else {
            dictSub["isFromMain"] = "0"
        }
        arrTable(dictSub, atIndex: 0)
        self.tbl.reloadData()
    }
    

    现在在cellForRowAtIndexPath 中使用这个键来检查它是来自主CollectionView 还是来自第二个。

    let dic = arrTable[indexPath.row] as! [String : AnyObject]
    let isFromMain = dic["isFromMain"] as! String
    if  isFromMain == "1" {
        let Price   = NSString(string: pPrice1).floatValue
        let total   = 1 * Price
        cell.lbl.text = String(format: "%.2f", total)
    } else {
        let Price   = 0.00
        let total   = 1 * Price
        cell.lbl.text = String(format: "%.2f", total)
    }
    

    【讨论】:

    • 真的帮了我兄弟。非常感谢您。我很感激你
    • 你会的,SO也可以帮助你:)
    • 你可以在collectionView的didSelectItemAtIndexPathmethod中做,获取该商品的价格,为totalPrice声明一个实例,并在collectionView的didSelect中添加它的值。
    • 我不是在谈论您在cellForRowAtIndexPath 中使用的总数,我是告诉您在类级别声明新实例,以增加您在didSelectItemAtIndexPath 中选择项目价格的总价值,如果有可能请将此作为新问题添加到 SO 上,并详细说明您想要的全部内容和新代码,如果我能给出答案,我会给出。
    • 蝙蝠添加新的,因为这个问题可能对遇到同样问题的人有所帮助。
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2011-06-25
    相关资源
    最近更新 更多