Setting the height of just one static UITableViewCell - swift

Im trying to set the height of a static UITableViewCell IBOutlet in Swift. Is this possible? I have found answers to how to change all cells in a tableview, but I just want to change this one and keep the rest as they are sized in the IB.
I've tried this:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 1 && indexPath.row == 1 {
return CGFloat(screenSize.width)
}
else{
return UITableViewAutomaticDimension
}
}

To set the height dynamically on a static TableViewCell, you have to indeed set it in heightForRowAtIndexPath. Here is one implementation to change the second row in the first section:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 && indexPath.row == 1 {
// Here return a number for the height you desire
return CGFloat(100)
}
else {
return UITableViewAutomaticDimension
}
}
Here are a few pointers that might be relevant to your situation. Please disregard them if they where done on purpose in your code..
The height for the cell was set to the width of the screen, it might be worthwhile to check if that was intended. Also please remember that both sections and rows start at index 0 and not at index 1.

Related

Swift Changing the size of tableview cells

just like the title says I'm searching for a way to change the size of each cell in my tableview. I have 2 questions:
1) How can I change the size of all cells by the same amount in my tableview?
2) Is it possible to automatically adjust the size of the cell based on the length of the text in that particular cell?
I'm aware that there are already similar questions asked before but I couldn't manage to implement their answers correctly as they've been outdated for some time now. I'd appreciate any help, thank you in advance!
1) To change size of all cells with same type, first you need to create CellType, here is the explanation how to do this.
https://stackoverflow.com/a/51979506/8417137
or you can check for indexPath.row, but it's not cool :)
Than here:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
default:
return defaultCellHeight
}
}
2) Yes, its possible. You create your CustomCell. In your CustomCell you should
setup your textFields or labels inside ContentView. Your views with text will stretch your cell.
Important thing. In method above you must return special height like that.
return UITableViewAutomaticDimension
For example your code will looks like that.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
// Your stretching cell
case textCellType:
return UITableViewAutomaticDimension
}
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
// Your stretching cell
case textCellType:
return UITableViewAutomaticDimension
}
}

Dynamically Resize one Static Table View Cell's Height in Swift

I am trying to do as the title suggests dynamically allocate a certain height dependent on the height of the users device. I have, right now a table view of 8 static cells and in the last one it contains a container view that points to a another table view controller of dynamic cells, Image posted below. Everything was done through to story board so far, however, since there is no way for me to "constrain" a single cell like I can with other elements I was hoping for a solution to my problem here.. Basically in the last cell, the static cell has to dynamically change its height based of the coordinates of "Height of users phone" - "Y coordinate of 7th static cell" is what I would imagine.
For what I understand, if all the cells have the same high except the last, what you have to do is implementing the method heightForRowAtIndexPath. For the last cell you make the calculation that you need. I give you an example
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 8 {
//Returns UITableViewAutomaticDimension or the size you have calculated
return UITableViewAutomaticDimension
} else {
return x.x
}
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 8 {
//Returns UITableViewAutomaticDimension or the size you have calculated
return UITableViewAutomaticDimension
} else {
return x.x
}
}

Hide a specific cell in my table

I'm trying to hide a specific cell in my table view?
private var yourDataSource = [AccountData.AllTasksDB] //A Json Array originally
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
var rowHeight:CGFloat = 72
//AccountData.AllTasksDB[indexPath.row]["importantvariable"]
if (self.yourDataSource[indexPath.row]["importantvarible"] == "0")
{
print("Cell is mine")
rowHeight = 0
}
else
{
print("Cell is not mine")
rowHeight = 72.0
}
return rowHeight
}
Can anyone tell me what I'm doing wrong? I thought it was looping through each cell and checking a value i've setup before hand. But it doesn't seem to want to access it.
I figured this would be more legible in an answer
fileprivate var yourDataSource = [YourObject]()
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if self.yourDataSource[indexPath].isMine == true {
return 0
}
else
{
return 72.0 //or whatever you like
}
}
You are accessing cell in the tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) method.
At this point the cell is not yet visible (or dequeued). If the cell is not visible, it will be nil, hence your error.
Try putting a break point at let cell = tableView.cellForRowAtIndexPath(indexPath) as! TableViewCellMyTasks and you'll see that it is nil.

How to set uitableviewcellselectionstyle.none to static cells?

Sorry I really cant find this anywhere.
I need to set my selection style to none so that the rows dont highlight when i click on it. Also, i need rows to be selectable as I have some rows which needs expanding and collapsing. I know the piece of code is UITableViewCellSelectionStyle.None but I have no idea where I can implement it. Thanks!
EDIT ADDED IN CODES
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 7
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// Set height for date picker
if indexPath.section == 0 && indexPath.row == 2 {
let height:CGFloat = datePicker.hidden ? 0.0 : 216.0
return height
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Expanding and collapsing date picker
UITableViewCellSelectionStyle.None
let datePickerIndexPath = NSIndexPath(forRow: 1, inSection: 0)
if datePickerIndexPath == indexPath {
datePicker.hidden = !datePicker.hidden
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.tableView.beginUpdates()
// apple bug fix - some TV lines hide after animation
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
self.tableView.endUpdates()
})
}
}
The codes are mainly for the datepicker that i have implemented. everything works fine but clicking on the cell highlights the whole row in the default selection color.
Hard to know where to best implement it without seeing your code, but you can definitely put it in your cellForRowAtIndexPath when you dequeue/initialize your cell. Just call yourCell.selectionStyle = .None before return yourCell
cell.selectionStyle = .None
When you write the code after equal(=) , just press dot(.) so that many type of functionality will be pop up like this-
And for your second issue just put some value in array to check that is working correctly or not.
I have had the same issue, the solution for me was:
self.tableView.allowsSelection = true
in storyboard, manually select all the static cells and change selection from "default" to "none"
for those cells are allowed to be selected, change the selection to "Default".
It would appear as if only some cells can be selected.
You can still handle selection for those exclusive cells in didSelectRowAt by checking indexPath.
Hope that helps.
Just found a way to programmatically insert the selection style setting for static cells, instead of using storyboard:
In viewWillAppear, use:
tableView.cellForRow(at: yourIndexPath1)?.selectionStyle = .none
tableView.cellForRow(at: yourIndexPath1)?.selectionStyle = .none
...

Can't change row height in Xcode 6 dynamic table

I have a UItableViewController and want the row height to adapt to the text in the UITextView. If I use this code in viewDidLoad it makes the height of the rows too small to hold the text:
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 50
The only way I can find to adjust the size is by changing the Table View Row Height in the StoryBoard. This changes every cell in the table view which is not what I want.
Does anyone have a suggestion about what I am missing?
This is my working code:
override func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var text:String
if indexPath.section > 0 {
text = switchNameArray [indexPath.row]
} else {
text = viewDescription
}
return calculateHeight(text)
}
func calculateHeight (text:String) -> CGFloat {
if UIDevice.currentDevice().userInterfaceIdiom == .Pad
{
return CGFloat(count(text)+50) // iPad
} else {
return CGFloat(count(text)+30) // iPhone
}
}
There is a override method to change the height for each row.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
You can also create and If-Else condition if you want to make it bigger or smaller. Just return a number
You could try using:
func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100 //Whatever fits your need for that cell
}
You can get the cell with tableView.cellForRowAtIndexPath(indexPath) and make the height whatever you need it to be.
If you want it to adapt to the size of your content
get the current tableView frame then adjust the height
depending on the height of the content.
var frame = CGRect(origin: tableView.frame.origin, size: tableView.frame.size)
frame.size.height = tableView.contentSize.height
tableView.frame = frame