Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do headers and footers follow movement of row when deleting it?

I'm building an app in Swift that relies heavily upon Table Views. It uses both headers and footers, as well as row deletion. For some strange reason when I perform a row deletion, the header and footer follows the sliding movement of the row to be deleted. The below screenshots explain what I mean.

enter image description here

How do you avoid this?

I'm implementing the delete function like this:

// Deleting
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {

        // Handling data source updating, cell row deletion, transition...

    }
}

And the header and footer like this:

// ## Header ##
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = tableView.dequeueReusableCellWithIdentifier("shoppingHeader") as! ShoppingTableViewHeaderCell

    // Clear up startup background
    headerCell.textLabel?.backgroundColor = UIColor.clearColor()

    // Add covering background color
    headerCell.backgroundColor = UIColorFromHex(0x22b8a3, alpha: 1)

    // Set store image, or store title if image is unavailable
    let url = NSURL(string: dataSource[sortedStores[section]]["meta_data"]["logo"].string!)
    let data = NSData(contentsOfURL: url!)
    if data != nil { // Store image
        let image = UIImage(data: data!)
        headerCell.imageView?.image = imageResize(image: image!, cellWidth: 90, cellHeight: 30)
    } else { // Store name label
        headerCell.textLabel?.text = dataSource[sortedStores[section]]["meta_data"]["nameStore"].string
        headerCell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 16)
        headerCell.textLabel?.textColor = UIColor.whiteColor()
    }

    // Right hand side details: define strings
    let address = dataSource[sortedStores[section]]["meta_data"]["street"].string
    let distanceMeasure = dataSource[sortedStores[section]]["meta_data"]["distance"].int!
    let numberOfOffers = dataSource[sortedStores[section]]["offers"].count

    // Right hand side details: assign strings to variables
    headerCell.rightLabel0.text = address
    headerCell.rightLabel1.text = "\(distanceMeasure) m away"
    if numberOfOffers == 1 {
        headerCell.rightLabel2.text = "1 offer available"
    } else {
        headerCell.rightLabel2.text = "\(numberOfOffers) offers available"
    }

    // Right hand side details: set text color
    headerCell.rightLabel0.textColor = UIColor.whiteColor()
    headerCell.rightLabel1.textColor = UIColor.whiteColor()
    headerCell.rightLabel2.textColor = UIColor.whiteColor()

    return headerCell
}


// ## Footer ##
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerCell = tableView.dequeueReusableCellWithIdentifier("shoppingFooter") as! UITableViewCell

    // Set black color to show that footer moves too.
    footerCell.backgroundColor = UIColor.blackColor() //clearColor()

    return footerCell
}
like image 841
Ulf Aslak Avatar asked Apr 29 '15 11:04

Ulf Aslak


People also ask

How do I stop my header and footer from repeating?

Go to Layout > Breaks > Next Page to create a section break. Double-click the header or footer area to open the Header & Footer tab. Select Link to Previous to turn off the link between the sections.

How do I lock headers and footers in Excel?

From the View tab, Windows Group, click the Freeze Panes drop down arrow. Select either Freeze Top Row or Freeze First Column.

Why is my header going on every page?

If you see the header/footer on every page, then you are putting it in the primary Header/Footer. If you're seeing it only on odd pages, then you have enabled "Different odd and even" which makes the primary header/footer the Odd Page Header/Footer.

How do I keep the same header section in Word?

Select Layout > Breaks > Next Page. Double-click the header or footer on the first page of the new section. Click Link to Previous to turn it off and unlink the header or footer from the previous section. Note: Headers and footers are linked separately.

How to remove header and footer in Excel?

How to Remove Headers in Excel 1 Select the worksheets from which you want to remove a header or footer. 2 Open the Page Setup dialog box ( Page Layout tab > Page Setup group > Dialog Box Launcher ). 3 In the Page Setup dialog box, click the drop-down arrow to open the list of preset headers or footers, and select (none). See More....

How to edit the header and footer of a chart?

Or, click the Page Layout button on the status bar in the bottom-right corner of the worksheet: Now, you select the header or footer text box and make the desired changes. Another way to modify an Excel footer or header is by using the Page Setup dialog box. Please remember that a header and footer of chart sheets can only be edited in this way.

How to display header and footer in worksheet view?

Headers and footers are displayed only on printed pages, in Print Preview and Page Layout view. In the normal worksheet view, they are not visible. Inserting a header in an Excel worksheet is quite easy.

How to add odd and even footers and headers?

For odd and even headers, don't do that manually in a one-by-one fashion. Go to Format > Document, click on the Layout tab at the top and enable "Different odd and even." This will give you one set of odd/even footers and headers for all the linked sections.


2 Answers

Simply return headerCell.contentView instead of headerCell and your problem will be resolved.

like image 176
Wizkid Avatar answered Oct 21 '22 22:10

Wizkid


I think using reusable cells for headers and footers is your problem. Try to use nibs instead. Create a separate .xib files for header and footer cells. And get your cell with:

let headerCell = NSBundle.mainBundle().loadNibNamed("YourNibName", owner: self, options: nil).first as! ShoppingTableViewHeaderCell

Or implement a UITableViewHeaderFooterView's subclass, create a nib for it, register this nib with:

 tableView .registerNib(UINib(nibName:"YourNibName", bundle: nil), forHeaderFooterViewReuseIdentifier: "TableHeader") 

And get your header's view with:

let header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("TableHeader")
like image 38
Vlad Avatar answered Oct 21 '22 22:10

Vlad