滚动

时间:2016-12-08 03:37:12

标签: ios swift firebase-realtime-database tableview tableviewcell

我目前正在使用表格视图来显示菜单,人们可以点击addsubtract按钮来增加/减少订单。

此表视图包含大约30个项目,因此您必须向下滚动才能访问所有菜单项。问题是,当您向下滚动时,滚动上方的表格视图(现在已隐藏)会丢失它们刚刚包含的数据。

例如,如果您有一个菜单项,您已经订购了2个项目,则标签上的2现已变回0。这是非常面向对象的,所以我不确定为什么会这样。

我的表视图类:

 @IBOutlet weak var appetizerTableView: UITableView!


var appetizerList = [OrderModel]()

let ref = FIRDatabase.database().reference()

override func viewDidLoad() {

    appetizerTableView.delegate = self
    appetizerTableView.dataSource = self


    ref.child("Broadway").child("AppetizerDishes").observeSingleEventOfType(.Value, withBlock: { snapshot in
        if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
            for snap in snapshot {
                print("WILL: \(snap)")
                if let postDict = snap.value as? Dictionary<String, AnyObject> {
                    let key = snap.key
                    let post = OrderModel(postkey: key, postData: postDict)
                    self.appetizerList.append(post)
                }
            }
        }

        self.appetizerTableView.reloadData()
    })


}

var data=[OrderModel]()

func addButtonAction(addedList:[String:Float]) {
    print("WILLCOHEN:\(addedList)")
}



func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return appetizerList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let post = appetizerList[indexPath.row]
    if let cell = appetizerTableView.dequeueReusableCellWithIdentifier("OrderCell") as? OrderCell{
        cell.configureCell(post)
        cell.delegate = self
        return cell
    } else {
        return OrderCell()
    }
  }

}

我的细胞模型类:

class OrderModel {
private var _dishName: String!
private var _dishDescription: String!
private var _numberOfOrders: Int!
private var _postKey: String!
private var _dishPrice: Float!

var dishName: String {
        return _dishName
}

var dishDescription: String {
    return _dishDescription
}

var numberOfOrders: Int {
    get {
        return _numberOfOrders
    }
    set (newVal) {
        _numberOfOrders = newVal
    }
}

var postKey: String {
    return _postKey
}

var dishPrice: Float {
    return _dishPrice
}

init(dishName: String, dishDescription: String, numberOfOrders: Int) {
    self._dishName = dishName
    self._dishDescription = dishDescription
}

init(postkey: String, postData: Dictionary<String, AnyObject>) {
    self._postKey = postkey

    if let dishName = postData["dishName"] as? String {
        self._dishName = dishName
    }

    if let dishDescription = postData["dishDescription"] as? String {
        self._dishDescription = dishDescription
    }

    if let numberOfOrders = postData["anumberOfOrders"] as? Int {
        self._numberOfOrders = numberOfOrders
    }

    if let dishPrice = postData["dishPrice"] as? Float32 {
        self._dishPrice = dishPrice
    }

}

}

我的细胞类:

protocol ClassNameDelegate:class {
func addButtonAction(addedList:[String:Float])
}

  var addedList: [String:Float] = [:]

 class OrderCell: UITableViewCell {

override func awakeFromNib() {
    super.awakeFromNib()

}

var post: OrderModel!
var link: Link!




@IBOutlet weak var dishName: UILabel!
@IBOutlet weak var dishDescriptionAndPrice: UILabel!
@IBOutlet weak var numberOfOrders: UILabel!
@IBOutlet weak var addOrderBtn: UIButton!
@IBOutlet weak var subtractOderBtn: UIButton!


weak var delegate: ClassNameDelegate?

@IBAction func addButtonPressed(sender: AnyObject) {
    if post.numberOfOrders == 9 {
        numberOfOrders.text = "9"
    } else {
        if addedList[post.dishName] != nil {
            addedList[post.dishName] = post.dishPrice
        } else {
            addedList["\(post.dishName) \(Int(numberOfOrders.text!)! + 1)"] = post.dishPrice
        }
        post.numberOfOrders = post.numberOfOrders - 1
        numberOfOrders.text = "\(post.numberOfOrders)"
    }

    if delegate != nil {
        delegate?.addButtonAction(addedList)
    }
}

@IBAction func subtractButtonPressed(sender: AnyObject) {
    if post.numberOfOrders == 0 {
        numberOfOrders.text = "0"
    } else {
        post.numberOfOrders = post.numberOfOrders + 1
        numberOfOrders.text = "\(post.numberOfOrders)"
    }
}

func getOrders() -> Dictionary<String, Float> {
    return addedList
}


func configureCell(post: OrderModel) {
        self.post = post
        self.dishName.text = post.dishName
        self.dishDescriptionAndPrice.text = post.dishDescription
        self.numberOfOrders.text = "0"

}

}

我应该提一下,我从Firebase中提取表格视图数据。

提前感谢您提供任何帮助或建议,非常感谢。

1 个答案:

答案 0 :(得分:0)

您在每个单元格上调用oauth2.war,并在configureCell(post:)函数中将configureCell(post:)标签的文本值设置为numbersOfOrders。您应该将文本设置为"0" / OrderModel参数中的值。

此外,您应该确保始终致电post(如果您必须手动创建configureCell(post:),则不会调用它),因此您的OrderCell功能应如下所示:

tableView(tableView:cellForRowAtIndexPath:)