UICollection视图单元格的自定义单元格无法清除旧数据

时间:2017-12-07 09:41:44

标签: ios swift uicollectionview uicollectionviewcell

我创建了一个自定义UICollection单元格,但数据有时会出现在错误的单元格中,无论我刷新多少UICollection视图都拒绝更改。我做了一个打印输出,看看从数组中得到的数据是错误的,但事实并非如此。有没有办法在输入下一个数据之前清除单元格中的旧数据。任何建议将不胜感激。

自定义单元格

class customAppointmentViewCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
}

let thumbnailImageView: UIImageView = {
    let tniv = UIImageView()
    tniv.translatesAutoresizingMaskIntoConstraints = false
    tniv.backgroundColor = UIColor.clear
    tniv.contentMode = .scaleAspectFit
    return tniv
}()

let seperatorView: UIView = {
    let sv = UIView()
    sv.translatesAutoresizingMaskIntoConstraints = false
    sv.backgroundColor = UIColor(r: 11, g: 49, b: 68)
    return sv
}()

let clientNamePlaceHolder: UILabel = {
    let fnhp = UILabel()
    fnhp.translatesAutoresizingMaskIntoConstraints = false
    fnhp.font = UIFont(name: "HelveticaNeue-Medium", size: 17)
    fnhp.textColor = UIColor.white
    fnhp.textAlignment = .left
    return fnhp
}()

let openingTimePlaceHolder: UILabel = {
    let fnhp = UILabel()
    fnhp.translatesAutoresizingMaskIntoConstraints = false
    fnhp.font = UIFont(name: "HelveticaNeue-Medium", size: 12)
    fnhp.textColor = UIColor.white
    fnhp.textAlignment = .left
    return fnhp
}()

let closingTimePlaceHolder: UILabel = {
    let fnhp = UILabel()
    fnhp.translatesAutoresizingMaskIntoConstraints = false
    fnhp.font = UIFont(name: "HelveticaNeue-Medium", size: 12)
    fnhp.textColor = UIColor.white
    fnhp.textAlignment = .left
    return fnhp
}()

let bookedBarberNamePlaceHolder: UILabel = {
    let fnhp = UILabel()
    fnhp.translatesAutoresizingMaskIntoConstraints = false
    fnhp.font = UIFont(name: "HelveticaNeue-Medium", size: 12)
    fnhp.textColor = UIColor.white
    fnhp.textAlignment = .left
    return fnhp
}()

let servicePricePlaceHolder: UILabel = {
    let fnhp = UILabel()
    fnhp.translatesAutoresizingMaskIntoConstraints = false
    fnhp.font = UIFont(name: "HelveticaNeue-Medium", size: 10)
    fnhp.textColor = UIColor.white
    fnhp.textAlignment = .right
    return fnhp
}()

func setupViews(){
    addSubview(thumbnailImageView)
    addSubview(clientNamePlaceHolder)
    addSubview(openingTimePlaceHolder)
    addSubview(closingTimePlaceHolder)
    addSubview(bookedBarberNamePlaceHolder)
    addSubview(servicePricePlaceHolder)
    addSubview(seperatorView)
    backgroundColor = UIColor(r: 23, g: 69, b: 90)
    addContraintsWithFormat(format: "H:|-16-[v0(90)]|", views: thumbnailImageView)
    addContraintsWithFormat(format: "H:|-116-[v0][v1(50)]-10-|", views: clientNamePlaceHolder, servicePricePlaceHolder)
    addContraintsWithFormat(format: "H:|-116-[v0]-60-|", views: openingTimePlaceHolder)
    addContraintsWithFormat(format: "H:|-116-[v0]-60-|", views: closingTimePlaceHolder)
    addContraintsWithFormat(format: "H:|-116-[v0]-60-|", views: bookedBarberNamePlaceHolder)
    addContraintsWithFormat(format: "V:|-10-[v0(20)][v1(20)][v2(20)][v3(20)]-10-|", views: clientNamePlaceHolder, openingTimePlaceHolder,closingTimePlaceHolder, bookedBarberNamePlaceHolder)
    addContraintsWithFormat(format: "V:|-10-[v0(20)]|", views: servicePricePlaceHolder)
    addContraintsWithFormat(format: "V:|-10-[v0]-10-[v1(5)]|", views: thumbnailImageView,seperatorView)
    addContraintsWithFormat(format: "H:|[v0]|", views: seperatorView)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
} 
}

收藏视图

extension AppointmentsViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.appointments.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! customAppointmentViewCell
    //empty cell data
    //real data

    cell.openingTimePlaceHolder.text = ""
    if let opentime = self.appointments[safe: indexPath.row]?.bookingStartTimeString {
        cell.openingTimePlaceHolder.text = opentime
    } else {
        cell.openingTimePlaceHolder.text = ""
    }


    cell.closingTimePlaceHolder.text = ""
    if let closetime = self.appointments[safe: indexPath.row]?.bookingEndTimeString {
        cell.closingTimePlaceHolder.text = closetime
    } else {
        cell.closingTimePlaceHolder.text = ""
    }


    cell.bookedBarberNamePlaceHolder.text = ""
    if let barberName = self.appointments[safe: indexPath.row]?.bookedBarberName {
        cell.bookedBarberNamePlaceHolder.text = barberName
    } else {
        cell.bookedBarberNamePlaceHolder.text = ""
    }


    cell.servicePricePlaceHolder.text = ""
    if let servicepricee = self.appointments[safe: indexPath.row]?.bookedServicePrice {
        cell.servicePricePlaceHolder.text = servicepricee
    } else {
        cell.servicePricePlaceHolder.text = ""
    }


    cell.thumbnailImageView.image = UIImage()
    if let profileimagess = self.appointments[safe: indexPath.row]?.profileImage {
        cell.thumbnailImageView.image = profileimagess
    } else {
        cell.thumbnailImageView.image = UIImage()
    }


    cell.clientNamePlaceHolder.text = ""
    if let clientnamess = self.appointments[safe: indexPath.row]?.clientName {
        cell.clientNamePlaceHolder.text = clientnamess
    } else {
        cell.clientNamePlaceHolder.text = ""
    }


    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectView.frame.width, height: 100)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let uuidAvail = UserDefaults.standard.object(forKey: "theOriginalBarberShopUUID") as? String{
        if let bookIDDD = self.appointments[safe: indexPath.row]?.bookingUniqueID {
            let orderDetail = OrderDetailViewController()
            orderDetail.specificAppintment = self.appointments[indexPath.row]
            orderDetail.barberShopID = uuidAvail
            orderDetail.bookingUniqueIDDD = bookIDDD
            orderDetail.bookedBarberUUIDAX = self.appointments[indexPath.row].bookedBarberUUID
            orderDetail.appointmentsviewhold = self
            orderDetail.indexPathSelected = indexPath.row
            navigationController?.pushViewController(orderDetail, animated: true)
        }
    }
}
}

感谢

3 个答案:

答案 0 :(得分:2)

customAppointmentViewCell

中执行此操作
override func prepareForReuse() {
    self. thumbnailImageView.image = UIImage()
    //add here other variable and set default value for all.
}
当新单元格显示在屏幕上时,将调用

prepareForReuse

并在Nikhil Manapure的指导下从您的cellForRow中删除默认值设置代码。

希望这会对你有所帮助。

答案 1 :(得分:0)

试试这个 -

prepareForReuse中实施方法customAppointmentViewCell,并在此方法中删除所有数据并重置所有内容。如果要在cellForRow中向单元格添加一些子视图,请将其删除。

通过这样做,您将能够减少这一点 -

cell.clientNamePlaceHolder.text = ""
if let clientnamess = self.appointments[safe: indexPath.row]?.clientName {
    cell.clientNamePlaceHolder.text = clientnamess
} else {
    cell.clientNamePlaceHolder.text = ""
}

到此 -

if let clientnamess = self.appointments[safe: indexPath.row]?.clientName {
    cell.clientNamePlaceHolder.text = clientnamess
}

执行此操作后,请检查问题是否仍然存在。

希望这会有所帮助:)

答案 2 :(得分:-1)

尝试通过在单元格中指定属性来将模型对象传递给UICollectionViewCell类

示例:

UICollectionViewCell:

var object: Model? {
  didSet{
    // here do your assignment of text to various labels
  }
}

CellForRow:

let cell = ...
cell.object = self.appointments[indexPath.item]
return cell

我希望这有效。