Common way to customize selection style of UITableViewCell - uitableview

I need customize view of selected/unselected cells in my UITableView, like this:
What is common way to do it?
UPDATE:
If I assign image (checked/unchecked) to cell.imageView.image I got such picture (which is ugly, because circle is too big):

Related

How I can hide button with size in UITableViewCell cell?

I have UITableView and UITableViewCell.
I get data from API. Some items have a link, others have not.
If the item has not to link I wand to hide button with a book icon.
When I use this method (look below) button is hidden right, but then when the tableview reuse this cell icon with a book does not come back. How I can fix it?
var addButtonTrailingConstraint = openPdfButton.widthAnchor.constraint(equalToConstant: 0)
if link == nil{
NSLayoutConstraint.activate([addButtonTrailingConstraint])
}else{
NSLayoutConstraint.deactivate([addButtonTrailingConstraint])
}
}
This is kinda hard to answer without more code / knowledge of your constraint setup.
But I can give you 2 tips how to solve this issue by taking another approach:
1. Approach: Use UIStackView to manage your buttons:
Remove your buttons and replace them with a UIStackView. Then in code, where you config your cell (set text, title, ...) you first remove all Buttons from the UIStackView (you can do this easily with stackView.removeAllArrangedSubviews(), this is needed because the cells are getting reused and you don't want to add more and more buttons every time the cell is getting displayed.
After that, add the buttons you need in this cell (e.g.: like this: stackView.addArrangedSubview(button)).
This approach has the benefit that it is very dynamic, you can add as many different buttons as you wish without having to modify your code.
But since you need to create new buttons all the time it is not the most performance efficient solution.
2. Approach: Use 2 different UITableViewCell classes:
Make 2 different UITableViewCells, one with one button and a second one with 2 buttons. You can also inherit one from another to reduce duplicate code.
Then in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) check which one of the 2 cell classes you need, create the right one and set its members (text, title, ...).
This approach is less flexible but more performance efficient in comparison to the 1. approach.
I use both approaches in production and they are working quite nicely :)
You need
if link == nil {
openPdfButton.widthAnchor.constraint(equalToConstant: 0).isActive = true
} else {
openPdfButton.constraints.forEach {
openPdfButton.removeConstraint($0)
}
}

Is it language appropriate to layer a UIButton over a UILabel in Swift 3.0?

My question is this: Is this following approach to making a UILabel tap sensitive stylistically acceptable in the Swift 3 language? I'm tempted to say "if it compiles it flies" but I don't want to get in the habit of using this "short cut" if it is going to bite me later on. Further, if it is acceptable, are there some drawbacks to using this method that aren't obvious to a newcomer like me?
Please note that I am not looking for a way to implement code, I have one. I am asking if the solution I have is acceptable from a language style perspective.
I've been trying to get a UILabel to accept a TapGesture when inside a table cell for 2 days now and whatever method I try, there is always some sort of error even if it will compile. On a hunch, I went to my storyboard for the table view and added a button on top (not stacked, or aligned, or anything like that – actually occupying the same 2D space on the story board) within the prototype cell. I deleted the button text, linked it to the table view cell code and implemented some basic functionality to change the text on the UILabel to red and back. All of this functions exactly like I expected. I click the button and the text changes from black to red, and back when clicked again. The UILabel text is static in the table cell on a white background and my real function isn't going to change the text, it is going to alter another view through delegation from that view.
Why do it this way? Even if I check the UILabel use interaction box and follow some of the other questions and answers here to make it tap-able, I cannot control+drag the UILabel to the table view cell and make an action, the option simply doesn't exist in the pull down menu. It is available if I control+drag the UILabel to the table view controller. This makes a kind of sense to me because it is the table view controller that senses touch (right?). But, on the other hand, I have a switch in the table view cell that works just fine when I follow the answer to this question. Simple functionality of the storyboard-code interation (control+drag) is preventing me from getting what I want. Maybe control+drag should be allowing me to make an action and doesn't? I don't know. I don't want to use a UIButton alone because the text scaling feature of UILabel is really handy.
If you just want to recognize taps on the table cell, make the class the delegate of the table view
tableView.delegate = self
and implement
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//your code here
}
I'd also appreciate you taking the time to read a bit more about UITableViews. Every UITableViewCell has a label by itself. Consider the following code:
tableView.dataSource = self // this code will ideally be in init
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "lovCell")
cell.textLabel?.text = dummyData[indexPath.row]
return cell
}

how to change the components in UITableVIewCell dynamicly

I am creating a UITabelViewCell looks like the the feed in Facebook which contains feed content, images, comments and so on.
To simplify the problem, lets consider only aUILabelon the top and aUIImageView` below the lable. All the constraints are set properly and the height of the cell is caculated dynamicly according to the constraints.
The challenge I face is that sometimes the feed only have content and sometimes it contains both content and images. I do not want to create two UITabelViewCell, cause it will make it harder to maintain.
The method I use now is remove the UIImageView if I find no image in the data, but I have to add it back when necessary, and all these can be done at "tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)". But I don`t think it is a good solution, can some one show me some good points, thanks!

How can I parse image from table cell inside table view to detail view in iOS?

I have an image inside my tableview cell.
What I want to do is when I click on that cell I want to parse both text & image to detail view.
I can do for text but I can't get the point about image.
So, simply my question is how can I parse image inside tableview cell to detail view in iOS.
Thanks
You can use it as the same way you're using it. Use indexPath to specify the row you wanna manipulate.
Here's how you do it. implement UITableViewDelegate in your ViewController, and implement the following method.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
The short answer, you access the image the same way that you set it. In the case of the standard UITableViewCell, it is the imageView property that you used.
For these API type questions though, there is no better place to start, than the documentation

iOS Table cell styling

In my iOS - swift project I need my table cells to look like this. I can get the multi select option by making the table editable. What I need to know is what are the ways that I can design a cell to look like this? How can I add that colored border at the bottom and how can seperate the cells to look like this?
Thanks
The cells you show in your question are custom cells. To make that, you would need to design those cells yourself with views, labels, buttons, etc. There is no default/boilerplate code that can do this for you; you will need to implement it yourself.
You must create and implement this custom cell.
Create a New file, Cocoa Touch class, on next page select "subclass of: UITableViewCell" and name "CustomCell"
Create a New file, select User Interface (under iOS) and select Storyboard, and Name something like "MyCustomCell". On this new storyboard drag a Table View Cell. Select this cell, and in identity inspector make the class = to the name of your "CustomCell" cocoa touch class file. Then go to Attributes Inspector and fill in "identifier" with "cell"
NOW
In your TableViewController file
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell
// Here's where you set your CustomCell properties like label title, images, backgrounds
return cell;
}
I don't know yet how to make the cells separated, but as to the border at the bottom just go to your "MyCustomCell" xib and add a view at bottom and change its background.
Maybe to get the space in-between you can use some sort of Layer (CAlayer).

Resources