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.
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
}
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.
From the View tab, Windows Group, click the Freeze Panes drop down arrow. Select either Freeze Top Row or Freeze First Column.
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.
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 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....
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.
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.
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.
Simply return headerCell.contentView
instead of headerCell
and your problem will be resolved.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With