How to get total in footer cell of cells in sections?

若如初见. 提交于 2020-01-04 05:18:07

问题


Currently Im able to get cells to add up their total in the sections Footer Cell. But it calculates the total for every cell no matter what section its in, inside the all the sections footer.

I still can't get it to add up the different prices(Price1 - 3) for the cells that have a different prices selected passed into it the Section

code im using to add up total in the CartFooter for the Cells in the sections cartFooter.cartTotal.text = "\(String(cart.map{$0.cartItems.price1}.reduce(0.0, +)))"

PREVIOUSLY:

im trying to get the Cells in each section to add up their total in footer cell for each section that they're in.

The data in the CartVC is populated from another a VC(HomeVC). Which is why there is 3 different price options in the CartCell for when the data populates the cells.

Just kind of stuck on how I would be able to get the total in the footer for the cells in the section

Adding specific data for each section in UITableView - Swift

Thanks in advance, Your help is much appreciated

extension CartViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return brands
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let brand = brands[section]
        return groupedCartItems[brand]!.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cartCell = tableView.dequeueReusableCell(withIdentifier: "CartCell") as! CartCell

        let brand = brands[indexPath.section]
        let cartItemsToDisplay = groupedCartItems[brand]![indexPath.row]
        cartCell.configure(withCartItems: cartItemsToDisplay.cartItems)

        return cartCell
    }

     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cartHeader = tableView.dequeueReusableCell(withIdentifier: "CartHeader") as! CartHeader

        let headerTitle = brands[section]
        cartHeader.brandName.text = "Brand: \(headerTitle)"

    return cartHeader
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
         let cartFooter = tableView.dequeueReusableCell(withIdentifier: "FooterCell") as! FooterCell
        let sectionTotal = cart[section].getSectionTotal()

        let calculate = String(cart.map{$0.cartItems.price1}.reduce(0.0, +))
        cartFooter.cartTotal.text = "$\(calculate)"

        return cartFooter
    }

}

Update: these are the results that I am getting using this in the CartFooter

let calculate = String(cart.map{$0.cart.price1}.reduce(0.0, +))
cartFooter.cartTotal.text = "$\(calculate)"

which calculates the overall total (OT) for all the sections and places the OT in all all Footer Cells(as seen below ▼) when im trying to get the total for each section in their footers (as seen in image above ▲ on the right side)

Update2:

this what ive added in my cellForRowAt to get the totals to add up in the section footer. it adds up the data for the cells but it doesn't give an accurate total in the footer

    var totalSum: Float = 0

    for eachProduct in cartItems{
        productPricesArray.append(eachProduct.cartItem.price1)
        productPricesArray.append(eachProduct.cartItem.price2)
        productPricesArray.append(eachProduct.cartItem.price3)
        totalSum = productPricesArray.reduce(0, +)

        cartFooter.cartTotal.text = String(totalSum)
    }

回答1:


@Evelyn Try calculation directly in footer. Try below code.

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let cartFooter = tableView.dequeueReusableCell(withIdentifier: "FooterCell") as! FooterCell
    let brand = brands[indexPath.section]
    let arrAllItems = groupedCartItems[brand]!
    var total: Float = 0
    for item in arrAllItems {
        if item.selectedOption == 1 {
             total = total + Float(item.price1)
        } else item cartItems.selectedOption == 2 {
             total = total + Float(item.price2)
        } else if item.selectedOption == 3 {
             total = total + Float(item.price3)
        }
    }
    cartFooter.cartTotal.text = "$\(total)"
    return cartFooter
}



回答2:


There's a lot of code, and I'm not too sure where your coding error lies. With that said:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
     let cartFooter = tableView.dequeueReusableCell(withIdentifier: "FooterCell") as! FooterCell
    let sectionTotal = cart[section].getSectionTotal()

    let calculate = String(cart.map{$0.cart.price1}.reduce(0.0, +))
    cartFooter.cartTotal.text = "$\(calculate)"

    return cartFooter
}

Your code seems to say let sectionTotal = cart[section].getSectionTotal() is the total you are looking for (i.e. the total within a section), while you are displaying the OT in a section, by summing up String(cart.map{$0.cart.price1}.reduce(0.0, +)).

In other words, if cartFooter is the container that will display the total within a section, one should read cartFooter.cartTotal.text = "$\(sectionTotal)" instead, no?

If that's not the answer, I suggest that you set a breakpoint each time the footerView is instantiated, and figure out why it output what it outputs (i.e. the OT, instead of the section total).




回答3:


If you want to correct calculate total price in each section you need filter items for every section. now i think you sum all of your section. For correct section you need yous snipper from your cellForRow method:

let brand = brands[indexPath.section]
        let cartItemsToDisplay = groupedCartItems[brand]![indexPath.row]
        cartCell.configure(withCartItems: cartItemsToDisplay.cart)

inside this:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
         let cartFooter = tableView.dequeueReusableCell(withIdentifier: "FooterCell") as! FooterCell
        let sectionTotal = cart[section].getSectionTotal()
        let brand = brands[section]
        let catrItemsToDisplay = // now you need to get associated items with this brand (cart)
    // I think it is can looks like this  =  cartItemsToDisplay[brand]
        let calculate = String(cartItemsToDisplay.cart.reduce(0.0, +))
        cartFooter.cartTotal.text = "$\(calculate)"

        return cartFooter
    }



回答4:


Modify your Model structure and the way values set to through TableViewDelegate. Giving a short hint. Please see if it helps:

class Cart {
   var brandWiseProducts: [BrandWiseProductDetails]!

   init() {
       brandWiseProducts = [BrandWiseProductDetails]()
   }

   initWIthProducts(productList : [BrandWiseProductDetails]) {
       self.brandWiseProducts = productList 
   }    
}



class BrandWiseProductDetails {
      var brandName: String!
      var selectedItems: [Items] 
      var totalAmount: Double or anything
}

class SelectedItem {
     var image
     var name
     var price
}


  Sections:

  Cart.brandWiseProducts.count

  SectionTitle:

  Cart.brandWiseProducts[indexPath.section].brandName

  Rows in section

  Cart.brandWiseProduct[indexPath.section].selectedItem[IndexPath.row]

  Footer:

  Cart.brandWiseProduct.totalAmount


来源:https://stackoverflow.com/questions/59039423/how-to-get-total-in-footer-cell-of-cells-in-sections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!