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

Related searches

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)
    }

@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
}

Total the data in an Excel table - Office Support, Discover how to insert a Total row by using Table Tools in Excel. When you apply a total row for the first time, the cells will be empty. Note: If you want to copy a total row formula to an adjacent cell in the total row, drag the formula across� The header and footer options in Excel are very useful for printing information related to the workbook such as the page number, the saved workbooks filename and path or the date. However, what if you want to use the contents of a cell in a footer in Excel.


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).

Sum a column of numbers in a repeating table, When you insert a repeating table on a form template, Microsoft Office Footer rows are commonly used to display a running total beneath a column of numbers. cell in the first footer row that you inserted, point to Insert on the shortcut menu � The VBA code required to simulate cell references in headers and footers is listed at the end of this article. Once this code is in place, a cell reference can be added to a header or footer by inserting this syntax into the header or footer text: ^[Cell:A1] ^[Cell:Sheet1!A1] ^[Cell:NamedRange]


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
    }

Equivalent of =SUM(ABOVE) in numbers?, Add a footer row in the "Header & Footer" section in the Format > Table sidebar (Select footer cell, type "=SUM", press Enter, type header title or The sum is calculated over all cells in the column, even when adding new rows. You now have a new sum for all cells above the selected cell you want the� Put cell value in header or footer of all worksheets with VBA code. Sometimes, you want to insert header or footer with a selected cell's contents to all worksheets of your opening workbook, with the above code, you need to repeat them time and time again.


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

To add a range of cells using TOTAL, Enter the TOTAL calculation on the Equation bar. Notes. The TOTAL function relies on cell numbers. For example, if using the function TOTAL(c1..c9) and� Insert cell Content into Header or Footer in a Worksheet; Insert Cell Content into Header or Footer in All Worksheets; You can easily add the number of pages, the current date and time or the name of current file, including the File Full path into the header or footer in your Excel. And there is no built-in command or function to add a cell


Formula Basics – Exago Support Center, Calculating in the Report Footer icon on the toolbar. Select the target cell, enter an equal sign followed by the value(s) you wish to aggregate, and click the AutoSum button. Normally, we can insert the page numbers into header or footer quickly by using the Header & Footer function, please do as follows: 1. Go to the worksheet which you want to insert the page numbers into the header or footer. 2. Then click Insert > Header & Footer, and your worksheet will be in Page Layout view, see screenshot: 3.


On the Insert tab, in the Text group and click the Header & Footer button. On the Design tab, click Go to Footer or scroll down to the footer boxes at the bottom of the page. Depending on the desired location, click the left, center, or right footer box, and type some text or insert the element you want.


After you finish defining and formatting the codes and text for your custom header or footer, click a cell in the Worksheet area to deselect the header or footer area. Excel replaces the header/footer codes in the custom header or footer with the actual information, while at the same time removing the Header & Footer Tools contextual tab from