UITableViewRowAction adjust dimensions (height) - swift

so i have a table in which I have made a little "hack" so to say. The "hack" was to create a UIView with a color and a smaller height than the actual (now transparent) cell, to make it look like there was spacing between all the cells.
My issue is now, that there is a 10 units gap from the "imaginary" cell to the actual buttom of the cell. Meaning that when i add my UITableViewRowAction, it will look like this:
Obviously i want it to have the same height.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
print("more button tapped")
}
more.backgroundColor = UIColor.gray
return [more]
}
This is what i have now.. but really i have no clue as to how i would adjust the height

Edit I misread the question. What you really need to know is do you provide a custom view for the editing actions for your tableViewCell. There is a detailed tutorial for this here: https://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views

Related

Swift tableviewcell height stays fixed

My tableviewcell height stays fixed even though I do the following:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 150
I have one imageview and one label inside the cell and I gave constraints to each side.
Also, I tried:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
I need a fixed height for my cell. It doesnt need to be dynamic. But no matter what i tried the height stays around 50px.
Any help?
Setting automaticDimension is based on using auto layout in your cells.
This means you must add a NSLayoutConstraint to all sides of a UILabel (or any other control you use in your cells) and the UITableViewCell container in interface designer or in code. In case of a label, configure setting 'Lines' to 0 and 'Line Break' to Word Wrap.
you can set the height you want with heightForRowAtIndexPath, hopefully that helps

Xcode11, TableView Cell Selection Style doesn't work

I uploaded the screenshot, I am setting the (static)tableview cell selection style to blue, but it doesn't work when I run the simulator, it is still the grey color. Then I tried the code cell.selectionStyle, but it also does not work. Am I doing wrong? or understanding wrong on this issue? I tried the code using ".contentView.backgroundColor" to set the background when I select or de-select a cell, it works,....but why the "cell.selectionStyle" does not work? AS I understand, it is right the background color when I select a cell.
Below is what(code) I tried to set the selection style. My focus is also on the interface as the picture showed.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("I select it")
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.selectionStyle = .blue
}
You can try this code
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.blue
cell.selectedBackgroundView = backgroundView

Change the section index background colour

Currently I am using table.sectionIndexBackgroundColor = .clear which makes the section index semi-transparent, causing what you can see in screenshot below.
I am trying to remove the semi-transparent overlay, which ends above the home button area on iPhone X. Another option is to extend the section index background throughout this area and set a non-transparent colour, because this design on rounded display looks really weird.
Any ideas how to do this?
EDIT
The point is, if I change the background colour to any value, the area near home button remains semi-transparent while scrolling.
FIXED
func tableView(_ tableView: UITableView, cellForRowAt path: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "identifier", for: path)
let data = ...
cell.textLabel!.text = ...
// here it comes
cell.contentView.superview?.backgroundColor = UIColor.clear
return cell
}

How to fill table cell with an image in Swift

I am replicating the effect in the image below. I created a custom UITableViewCell class with an UIImageView property. I set the prototype cell to my custom class. However, I cannot get the table cell's height to resize to the height of the image. There is always space above and below the image.
You can make the heigh of your UITableViewCell dynamic regarding the height of your image. The UITableViewDelegate has a function entitled tableView:estimatedHeightForRowAtIndexPath: that according to Apple:
Asks the delegate for the estimated height of a row in a specified location.
Then you can in your method ask for the height of the UIImageView and set the height for the row properly, see the following example:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.getEstimatedHeightForImageAtIndex(indexPath.row)
}
func getEstimatedHeightForImageAtIndex(row: Int) -> CGFloat {
// here you need to get the height of image for the selected row.
}
There is a nice tutorial you can read to know more about the topic :
UITableView Tutorial: Dynamic Table View Cell Height
I hope this help you.
What I did was to create a custom UITableViewCell, added an UIImageView to the cell and resized it in the storyboard to fill the whole cell.
If you do that though, make sure you add the constraints to have it horizontally and vertically centred, and add the spacing constraints, but change the spacing not to margin (uncheck the box).
See the image below:
And another thing to remember is the content mode of the imageview. I learned a lot from #EmptyStack and #erkanyildiz answers to this question.
I hope you found any of this useful, and I am sorry if none of it helped! :)
Create a custom UITableViewCell, set cell style to custom in storyboard. Add an UIImageView to the cell and resize image to fill the whole cell. Add image outlet to your custom UITableViewCell and use that outlet when seting an image in your TableViewController.
In your viewDidLoad() function add
tableView.rowHeight = UITableViewAutomaticDimension
Then you need your image aspect ratio (width / height) so you can use it when you set your row height.
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath
indexPath: NSIndexPath) -> CGFloat {
return view.frame.width / CGFloat(aspectRatio)
}

iOS8 Swift - Problems dynamically resizing tableview cells

Here's what my application looks like currently.
You'll see that when the subtitle text label is only one line is resizes correctly, but when there's multiple lines it gets all messed up. I think this has to do with the constraints possibly? Right now I'm using the auto-layout constraints. You can see them in the screenshot. Here's the code that creates my cells.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> DealCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Deal Cell", forIndexPath: indexPath) as DealCell
let currentBar = bars[indexPath.row] as BarAnnotation
cell.barName.text = currentBar.name
cell.deal.text = currentBar.deal
cell.distanceToBar.text = String(format: "%.3f mi", currentBar.distance)
// Set the height of the table view cells
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
return cell
}
Any ideas?
Use tableView:heightForRowAtIndexPath: function in the UITableViewDelegate.
Using this you can individually set the height of each row separately.
For more information : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
Or you can use auto layout settings like in this article.
http://useyourloaf.com/blog/2014/02/14/table-view-cells-with-varying-row-heights.html