tableview scrolls when adding/deleting data in swift 2.3 - swift

Re-wording this question as I understand the problem better:
I have a UITableview and I am reloading data from Firebase when theres any activity. I use
tableView.estimatedRowHeight = 330
tableView.rowHeight = UITableViewAutomaticDimension
So anytime my tableview source gets updated - a new post gets added or deleted, my UI tableview will scroll one cell up or down to accomodate for this new activity. How do I make sure my cell stays in the same position when tableview gets updated ?
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 1000
}

Related

How to make Custom Xib Cell Resize Correctly

I'm trying to make a custom xib cell resize based on really how much information the user enters.
I have tried setting the constraints of the xib cell, like this Constraints
I only have top and bottom constraints on the Label and the Date.
In the viewDidLoad() of the viewcontroller containing the cell and tableview i have
self.tableView.rowHeight = UITableView.automaticDimension
and I also have set
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
GIF of Resize Not Working

Tableview creates five rows on one row?

I have a tableView with a custom cell that I have told should give me 5 cells in return of this custom cell. The question now is that I am running the app and getting five rows on one row. I have changed from default size of cell to custom but that is nearly the only thing I have done. So now I wonder what can create this kind of problem?
The code of my tableView looks like this.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theoryCell") as! theoryTVC
return cell
}
So the problem is you are using custom cell with custom height but you are not setting height for row in table view method.
Try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0 //Choose your custom row height
}

Tableview set row height dynamically based on content in swift?

I am struck in setting tableview cell row height in swift , On scrolling the row height is changing, please check the screen shot.
For the first time its scrolling perfect on scrolling multiple times the height is not proper. I am using too many custom tableview cells
In viewDidLoad
I added this
self.tableView.rowHeight = UITableViewAutomaticDimension
and also added these delegate methods.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
Can anyone guide me to achieve this in proper way.

How to set dynamic height of custom UITabelview cell in swit

I have a UITableView that is populated with custom cells which had created by Xib(autolayout).The cell consist of multiple labels images etc and size of size of changes according to response we get from api.I have been trying hard set height dynamically. I am using following methods.
func createTabelView ()
{
let nib = UINib(nibName: "DBEventCell", bundle: nil)
self.eventTabelVew.registerNib(nib, forCellReuseIdentifier: Constants.CELL_IDENTIFIER)
self.eventTabelVew.estimatedRowHeight = 200
self.eventTabelVew.rowHeight = UITableViewAutomaticDimension
self.eventTabelVew.separatorStyle = .None //UITableViewCellSeparatorStyle.SingleLine;
self.eventTabelVew.separatorColor = UIColor.clearColor();
self.eventTabelVew.backgroundColor = UIColor.whiteColor();
self.eventTabelVew.setNeedsLayout()
self.eventTabelVew.layoutIfNeeded()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tleView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
but this not working.Please do help as I am struck last from last few days.
Thanks in advance
I think you don't need this function
func tableView(tleView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
also you should check your cell autolayout constrains and adjusting properties of UI components to make it contract or expand with content also check this tutorial
https://www.appcoda.com/self-sizing-cells/

How to make the height of tableviewcell self-adaption to the text-label in it?

I am creating a TableViewController, now there's some cells in it, the longth of texts in these cells are not the same, so I wanna make the height of the cells self-adaption to the text-label.
I was thinking to creat the label first, then set the height of cell to fix it, but it seems not work, here is the code.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! FuckGFWQATableViewCell
return cell.question.bounds.height
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("fuckGFWQATableViewCellId", forIndexPath: indexPath) as! FuckGFWQATableViewCell
cell.question.numberOfLines = 0
cell.question.text = fuckGFWQuestions[indexPath.section]
return cell
}
But when the heightForRowAtIndexPath called, the label was not exist. What should I do?
With iOS8 you can use self sizing cells in UITableView.Checkout here. You do not need to calculate your UILabel's height and set your cell. You just need to :
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension