ios - Swift - Firebase getting all keys and store them into array -
i have firebase database structure:
main ----- name 1 --------value 1 --------value 2 --------value 3 ----- name 2 --------value 1 --------value 1 --------value 1
i'm trying key values , storing array of custom class.
my custom class has init:
init(name: string, photo: string) { self._name = name self._photo = photo }
so in view controller have:
var array = [customclass]() let ref = database.database().reference().child("main") func getinfo(){ ref.observesingleevent(of: .value) { (snapshot) in var arraytemp = [customclass]() child in snapshot.children { let snap = child as! datasnapshot let name = snap.key let custom = customclass(name: name, photo: "") arraytemp.append(custom) } self.array = arraytemp self.collectionview.reloaddata() } } func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell { if let cell = collectionview.dequeuereusablecell(withreuseidentifier: "cell", for: indexpath) as? cell { let custom: custom! custom = array[indexpath.row] cell.configurecell(custom) return cell } else { return uicollectionviewcell() } }
and here cell:
class cell: uicollectionviewcell { @iboutlet weak var thumb: uiimageview! @iboutlet weak var namelbl: uilabel! var custom: customclass! func configurecell(_ custom: customclass) { self.custom = custom namelbl.text = self.custom.customname.capitalized var url = url(string: self.custom.photourl) if url == nil { url = url(string: "") } thumb.sd_setimage(with: url) }
}
my issue if print name have values db when reload collection view see number of item correct name first element repeated x time... help?
to fix issue :
func numberofsections(in collectionview: uicollectionview) -> int { return 1 } func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return self.array.count }
Comments
Post a Comment