Home UITableView reloadData() using lots of power calling from cellForRowAt
Reply: 0

UITableView reloadData() using lots of power calling from cellForRowAt

Nate
1#
Nate Published in 2018-01-13 08:11:46Z

My UITableView will only show all of the fully loaded contents in the cells if I follow the advice of this post and call reloadData() in the main thread with DispatchQueue. I'm doing this in tableView cellForRowAt because it wasn't fully loading the values from my array into all of the cells otherwise. Now it's constantly reloading and is obviously very energy intensive. This makes it harder to scroll in the Table View, which makes sense.

How can I prevent this ridiculous use of energy while still loading the data into the cells as I need?

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell:UITableViewCell? =
        charitiesSupportedTable?.dequeueReusableCell(withIdentifier: "SupportedCell")
    if (cell == nil)
    {
        cell = UITableViewCell(style: UITableViewCellStyle.subtitle,
                               reuseIdentifier: "abc")
    }


        if(indexPath.row <= moneyRaisedOfPrevPostsArray.count-1){
            cell?.textLabel?.text = charOfPrevPostsArray[indexPath.row]
            cell?.detailTextLabel?.text = moneyRaisedOfPrevPostsArray[indexPath.row]
        }

        DispatchQueue.main.async {
            self.charitiesSupportedTable.reloadData()
        }

    return cell!
}

Here's my viewDidLoad where I create the array

override func viewDidLoad() {
    super.viewDidLoad()


    let userEmail = Auth.auth().currentUser!.email
    var email1 = userEmail?.replacingOccurrences(of: "@", with: "", options: NSString.CompareOptions.literal, range:nil)
    email1 = email1?.replacingOccurrences(of: ".", with: "", options: NSString.CompareOptions.literal, range:nil)



    charitiesSupportedTable.delegate = self
    charitiesSupportedTable.dataSource = self

    charitiesSupportedTable.isScrollEnabled = false

    reff = Database.database().reference().child("Posts")

    let userRef1 =  reff?.queryOrdered(byChild: "user").queryEqual(toValue : email1)

    userRef1?.observe(.value, with: { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let key = snap.key
            let value = snap.value
            let valueAsDict = value as! NSDictionary


            charityOfPrevPosts = valueAsDict.value(forKey: "charity") as! String
            viewCountOfPrevPosts = valueAsDict.value(forKey: "viewCount") as! String // times .006
            moneyRaisedPrevPosts  = Double(viewCountOfPrevPosts)! * 0.006

            charOfPrevPostsArray.append(charityOfPrevPosts)
            moneyRaisedOfPrevPostsArray.append(String(moneyRaisedPrevPosts))



        }

        charOfPrevPostsArray.reverse()
        moneyRaisedOfPrevPostsArray.reverse()



    }
    )
    moneyRaisedOfPrevPostsArray.removeAll()
    charOfPrevPostsArray.removeAll()




    reff = Database.database().reference().child("UsersViews").child(email1!).child("totalMoneyRaised")
    reff?.observe(DataEventType.value, with: { (snapshot) in
        var totalMoney = snapshot.value as! Double

        self.totalMoneyLabel.text = String(format: "$%.02f", totalMoney)
    })

    // don't know if this part is relevant but I do return charitiesCount + 2 in numberOfRowsInSection
    reff = Database.database().reference()
    databaseHandle = reff?.child("UsersViews").child(email1!).child("charitiesSupported").observe(.value, with: { (snapshot) in

        let myDictionary11 = snapshot.value as? NSDictionary

        // Adds brands and charities to myData array
        for value in (myDictionary11?.allValues)! {
            self.myCharities.append(value as! String)
        }
        charitiesCount = self.myCharities.count

    })


}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298544 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO