I have a tableview. I created it from storyboard it is a static tableview.(I am using it for profile settings).
But i have to change height of the first row of this table view dynamically and i don't want to change other cell heights. They should use their storyboard height values. How can i do this?
I tried following code:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
}
return (tableView.cellForRow(at: indexPath)?.bounds.height)!
}
But this is setting 100 for all cells.
My tableview:
Use the following override function..
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
}
return UITableView.automaticDimension
}
But you have to make sure that every item in the static tableview have (top/bottom/left/right) constraints. it only works when it have all these constraints on it.
Related
I have a tableview , it has two rows.
Tableview's height is 700. The first row height is 150 and the second row height is 100 but I'd like the second cell to cover the rest of the space in tableview.
I know I can use the below for tableview height. But I'd like to
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
Simply, just replace this method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let tblHeight = infoTable.bounds.size.height
return indexPath.row == 0 ? 150 : tblHeight - 150
}
just change infoTable with your table name.
So every time I scroll my tableView it reloads data which I find ridiculous since it makes no sense to reload data as it hasn't been changed.
So I setup my tableView as follows:
func numberOfSections(in tableView: UITableView) -> Int {
return self.numberOfElements
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 6
}
My cells are really custom and they require spacing between them. I couldn't add an extra View to my cell to fake that spacing because I have corner radius and it just ruins it. So I had to make each row = a section and set the spacing as a section height.
My cell has a dynamic height and can change it's height when I click "more" button, so the cell extends a little.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.segmentedControl.selectedSegmentIndex == 0 {
if self.isCellSelectedAt[indexPath.section] {
return self.fullCellHeight
} else {
return self.shortCellHeight
}
} else {
return 148
}
}
And here's how I setup my cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if self.segmentedControl.selectedSegmentIndex == 0 {
cell = tableView.dequeueReusableCell(withIdentifier: String.className(CurrentDocCell.self)) as! CurrentDocCell
(cell as! CurrentDocCell).delegate = self
(cell as! CurrentDocCell).ID = indexPath.section
} else {
cell = tableView.dequeueReusableCell(withIdentifier: String.className(PromissoryDocCell.self)) as! PromissoryDocCell
}
return cell
}
So I have a segmentedControl by switching which I can present either one cell of a certain height or the other one which is expandable.
In my viewDidLoad I have only these settings for tableView:
self.tableView.registerCellNib(CurrentDocCell.self)
self.tableView.registerCellNib(PromissoryDocCell.self)
And to expand the cell I have this delegate method:
func showDetails(at ID: Int) {
self.tableView.beginUpdates()
self.isCellSelectedAt[ID] = !self.isCellSelectedAt[ID]
self.tableView.endUpdates()
}
I set a breakpoint at cellForRowAt tableView method and it indeed gets called every time I scroll my tableView.
Any ideas? I feel like doing another approach to make cell spacing might fix this issue.
A UITableView only loads that part of its datasource which gets currently displayed. This dramatically increases the performance of the tableview, especially if the datasource contains thousands of records.
So it is the normal behaviour to reload the needed parts of the datasource when you scroll.
I have a uitableview controller with STATIC cells in two sections. I have adjusted the height of cells 0 & 1 in section 0 using storyboard. However, I have used code to expand a date picker after a UIswitch is toggled and a cell is tapped in section 1.
My problem is that the code is overriding cells 0 and 1 in section 0. How do I force those cells to take their height from storyboard? If that is not possible, how would I code their height so that the cell expansion code does not override it?
Notes: I am new and don't fully understand the code that I used to make the expansion happen, so if I need to change that, let me know. Code pasted below.
var pickerVisible = false
// MARK: - COLLAPSABLE CELL
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 1 && indexPath.row == 2 {
pickerVisible = !pickerVisible
tableView.reloadData()
}
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 1 && indexPath.row == 2 && toggle.isOn == false {
return 0.0
}
if indexPath.section == 1 && indexPath.row == 3 {
if toggle.isOn == false || pickerVisible == false {
return 0.0
}
return 165.0
}
return 44.0
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 44.0
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
// set height for rows in section 0
} else if indexPath.section == 1 {
// set height for rows in section 1
} else {
// default height for your tableview cell
return 44.0
}
}
How do I force those cells to take their height from storyboard?
-> I think you don't really need this.
How would I code their height so that the cell expansion code does not override it?
-> Its pretty easy to do what you want (sample code above)
I have found another answer that really gets to the root of what my problem was -- how to program one cell to be an exact height and have all the other cells take the height from what is put together in storyboard.
The answer is instead of returning a number in heightForRowAt, you can do the following:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 170.0
}
// OTHERWISE, RETURN WHATEVER THE TABLEVIEW WANTS.
return self.tableView.rowHeight
I am trying to have a UITableView with different sized prototype cells.
I set the row height in my Table View to 243. The first and second cells' individual row height are set to 243 and 465, respectively. When I run my app, both cells appear at 243 height.
Here is what I have tried:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 25
}
//does not change cell height
And:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 25
}
//Method does not override any method from its superclass
How can I configure my project to successfully resize the second cell?
The signature of the delegate method has changed in Swift 3. It needs to be:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 1 {
return 465.0
} else {
return 243.0
}
}
With the wrong signature the table view doesn't know you've implemented the method so it never gets called.
In my project, I have a static tableView with 3 sections. The cell in the second section holds a label that is filled dynamically and therefore has a dynamic height. The cell should adjust its height to the label's height. Here's what I tried, without success:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
return 44
} else if indexPath.section == 1 {
return UITableViewAutomaticDimension
} else if indexPath.section == 2 {
return 80
} else {
return 50
}
}
The heights of all sections are set properly except the automatic dimensions. Any help?
set this line in viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
then write this table view method
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
also make sure you have used the auto layout properly.
and you have set the number of lines for lable = 0
You also need to provide an estimated row height. You can do that by using the estimatedRowHeight property of your UITableView, or implementing the corresponding delegate method:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return XXXXX // Provide your estimation here, or pass UITableViewAutomaticDimension (not the best for performances)
}
Reference: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html