animation issue with UITableView footer View when performing "beginUpdates" "endUpdates" - ios

When I'm changing a cell height with animation (using beginUpdates(), endUpdates()) I'm having a very weird animation for my section footer : it's moving at the bottom of the tableview.
here is the simplest code i could write
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "footer"
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return cellHeight
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.beginUpdates()
cellHeight += 20;
tableView.endUpdates()
}
How could I avoid this animation issue ?

I'm having the exact same problem (the footer moves to the bottom of the tableview and stays there).
It looks like it's an iOS8 bug (it doesn't happen on iOS7).
In my case, a workaround is to return the footer as the next section header...

Related

Tabelview Cell won't show correctly

My tableview cell won't show correctly. I have no idea what the problem is. When I launch the simulator it will show this: Simulator. I have used Contraints and I know for sure that the Contraints are correct. This is how it needs to be: How it must look like. Please help!
Add these in your code
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80.0
}
If you have multiple heights then put condition like this
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if(indexPath.section == 0){
return 50
}else{
retrn 120
}
}
Or use automatic dimension, with resizes every cell automatically according to its content:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Swift 3: How can I remove the extra space from the TableView?

I have a webview on the tableview, and on each cell I have some extra blank space after the phone numbers and email/ website addresses. So how can I remove/ delete the extra blank space from the cell to make it look bit better.
the code what I have is this:
self.heightOfTheCell = CGFloat(((heightMul)*32)+4)
I am not too sure how to play up with these numbers.
Please find the screenshot attached.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0
{
if heightOfTheCell != nil
{
return heightOfTheCell
}else{
return UITableViewAutomaticDimension
}
}
else{
return UITableViewAutomaticDimension
}
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Here's how I do it.
First setup properly the constraints for the cells.
then on your viewController
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

How to freeze a tableView cell in swift?

I want to freeze a tableView cell on top of screen, so that while scrolling, other cells move but the top cell stays on its place.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
cell. //I think it must be here
return cell
Thank you.
Use these methods to create tableview header and add any subviews in it.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView.init()
header.backgroundColor = UIColor.gray
//Add subviews here
return header
}
try this:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 65
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.black
header.textLabel?.text="adadafaf"//ur data
}

UITableView Section Title Glitch?

I have a view controller with a tableview inside that is grouped. This produces a very weird glitch that causes the sectionTitles to show at the side. I've attached top, trailing, bottom, and leading constraints to the superview for the tableview and I've tested returning UITableViewCell() in cellForRowAtIndexPath to see if it was the cell that was causing the problem, but it still shows it like this.
var sectionTitles = ["Customer Orders", "Unprocessed Orders", "Processed Orders"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionTitles.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Perform segue to order list in future
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 117.0
}
After hours I figured out that I didn't implement this method
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
If you have only one section you don't put many titles.
If instead you want to give a title for each cell you have to add a label in your customized tableViewCell and in your cellForRowAtIndexPath you have to put something like this (after you have created a NSObject class):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
let order = Order[indexPath.row]
cell.yourTitleLabel.text = order.title
return cell
}

How to Change UITableViewCell's Height on Tap?

I'm trying to have a UITableViewCell reveal more content about a particular cell whenever that specific cell is tapped. Once the cell has expanded and is tapped again, the cell should shrink back to its original size.
I'm pretty sure something has to be done with the delegates heightForRowAtIndexPath and didSelectRowAtIndexPath, but I don't know how to select a specific table cell row using didSelectRowAtIndexPath.
// Height of table cell rows
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 45
}
//On cell tap, expand
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.rowHeight = 75;
}
Also, is it possible to hide any content that's overflowing from the parent cell? Kind of like overflow: hidden; in CSS.
Declare a global variable NSInteger type and store row on tableview selected in didSelectRowAtIndexPath and also reload here . After check row in heightForRowAtIndexPath and increase height there .
Try Like this
var selectedIndex : NSInteger! = -1 //Delecre this global
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == selectedIndex{
selectedIndex = -1
}else{
selectedIndex = indexPath.row
}
tableView.reloadData()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == selectedIndex
{
return 75
}else{
return 45
}
}
What you have to do is to save the index of the selected Cell in didSelectRow method. and also have to begin/end updates on table view. This will reload some part of tableview. and will call heightForRow method. In that method you can check that if your row is selected one then return expandedHeight, otherwise return the normal height
In height for row:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if self.selectedSortingRow == indexPath.row {
return ExpandedRowHeight
}
else{
return normalHeight
}
}
In didSelect Row:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
selectedSortingRow = (int) indexPath.row
self.tableView.endUpdates()
}
Swift 4 answer with recommended comments. No need to make a new variable, tableView holds a value for selectedRow indexPath.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.tableView.indexPathForSelectedRow?.row == indexPath.row {
return 75;
} else {
return 45;
}
}
Swift 4. Animated resize with multiple selection. Based on Ryderpro answer.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let selectedRows = tableView.indexPathsForSelectedRows, selectedRows.contains(indexPath) {
return 75
} else {
return 45
}
}

Resources