UITableViewCellAccessoryCheckmark and AutoLayout constraints - ios

I have custom UITableViewCell and manage selection of my cells. When the cell is selected and the checkmark is presented, the cell Content View width shrinks and my labels with trailing constraints move to the left. How can I effectively avoid the content move?
I did not want to set the constraints to leading as I have flexible tableview width and they are right aligned. When I want to update label's constraints in code via its IBOutlets I went into trouble as I return multiple subclasses of UITableViewCell in cellForRow: and I got unrecognized selector error in didSelectRowAtIndexPath: when trying to set constraints in code. Any elegant solution? Thanks.

I had a similar (actually, the exact) problem, where the checkmark would push everything to the left.
- EDIT:
A few hours after my original answer, I realized it's a really simple issue.
This push to the left is a constraint pushing the contentView of the cell. So?
Connect the constraint from the item on the right (like the i button), to the cell itself, not its contentView.
- Original answer (the actual answer is on top, this is for the record):
I just realized there's a very simple solution. I had a button that was pushed left when checkmark was on and I had to keep a constraint to keep it in place (so that 'uncheck' won't move the button to the right). It was hard-coded 'width' from the right and overall just bad practice. Never mind that I could not 'get' that checkmark 'width' to do this dynamically.
Solution?
Hold a constraint from the left.
That's it. I have a label too, so in my case: Constraint from the button left to the label (so they won't overlap) and from the button-left to left content-view. Basically, whatever moves on the right does not really 'reaches' the left side (rather have everything 'sitting' on the right).

This behavior is by design.
The simplest way to get the behavior you want is to set an empty accessory view on every cell which will move all the content to the left the same distance as the checkmark.

How about putting the checkmark in there all the time, and just playing with the alpha / transparency?
if checked
{
cell.accessoryType = .checkmark
cell.tintColor = cell.tintColor.withAlphaComponent(1)
}
else
{
cell.accessoryType = .checkmark
cell.tintColor = cell.tintColor.withAlphaComponent(0)
}

Related

How to set Table view cell to occupy entire screen?

I am working on a project to get custom news feeds from Bing news. I created a custom table view cell and populated various UI items in it. But I am having a hard time to make it occupy the entire screen, it starts from extreme left and ends on the right, leaving large gap from the right margin as show in the image. Can anyone help me out, how to correctly fix it with a detailed explanation of how constraints work in Table views and Tableview cells.
Here' what it looks like in the simulator
Remove previous constraints and add constraint to your UITableView like as shown in below image.
So you UITableView will have 0 margin from edges.
Create a UITableViewController. Just drag drop a TableViewCell into it. Add identifier for that cell and use it. By default it will occupy the whole screen.
In your case I guess you might have added constraints to UITableView. Here what you should do is pin your UITableView to Top,Bottom,Right & Left or make UITableView's width equal to SuperView.

Trying to hide UITableViewCell separator by using insets but it only decreases in opacity

I am trying to hide a UITableViewCell's separator by pushing it all the way to the right as far as it goes. So I set both the cell's separator inset and layout margins all the way to the right as far as they go. But this only makes the cell slightly more transparent. As you see in the picture, the lower line is slightly more transparent than the one above. There seems to be another line for the cell below the one I hide that I can't remove for some reason. No idea what could be causing this.
You can hide the separators by calling table.separatorStyle = .None on your UITableView.
Edit 2: I couldn't tell by the question that the goal was to remove cell separators for just one type of cell: as mentioned in my comment below, I'd advise removing the separators with the line above and adding a small-height UIView as a custom separator where you want it.
Hide tableview seperator simply by setting its property in attribute inspector.
Simply select the Attributes inspector and set seperator = none if you want to hide then otherwise if you want to do any operation with seperator color then set color on there.
UITabieViewCell generally contain two subviews: content view and separator line. If a cell's height is 44.f, content view's height is 43.f and 1.f for the line. You just move the line away, but the height of it is still there.

TableViewCell with SegmentControl

I have tableview in which a particular cell has the UISegmentedControl in it.
My doubt is when a particular segment is selected I need to expand the cell and show the information below and need to hide it when the other segment is selected.
For Ex:
I have three segment A, B and C. When selecting the segment B, I need to show switch below the segment and when the other two segments are selected nothing must be displayed below the segment.
Your dataSource's objects should have an "extended" property.
Each time a segmentedControl is changed, update your dataSource accordingly and reload your cell.
When you configure your cell, change its size depending on the "extended" property of the object.
Theres a few things to do here. You need to make sure your tableview is using automatic dimension for its cell height. Dont use the -heightForRowAtIndexPath method (remove it). Instead set two properties on the tableview :
[self.tableview setEstimatedRowHeight:110];
[self.tableview setRowHeight:UITableViewAutomaticDimension];
This enables proper autolayout (in case you havent already) for each cell.
You will need to have a subclass of UITableViewCell in code, if you havent already (sorry for skipping over some quite big things!). Add the information label to your cell in Interface Builder. Give it a height constraint, and then drag this height constraint into your cell subclass so you can change it.
Now put your code for handling the segmented control in the subclass. When you detect that the central segment has been selected, then affect the height constraint of your information label, like this :
self.labelHeightConstraint.constant = 40;
You may also need a call to setNeedsLayout :
[self setNeedsLayout];
Apologies for the brevity - hopefully this will point you in the right direction. There are some other details - for eg on -prepareForReuse method (on the cell) you can set the height of the info label to 0 (and call setNeedsLayout again).

Disabling touch interaction of Spaces between Custom table view cell

I use custom cells of subclassing UITableViewCell in a table view. And There must be space between cells. I succesfully add the space with adding a subview that I called SpaceView into the content view. But When I touched the spaceView, it is perceived as I touched the cell. And didSelectRow method called. I tried to set spaceView's UserInteractionEnabled==NO. But It doesn't work.
So my question is; Is this the right way I used to add space between cells ? Should I try to add cells for making the space ? or If its the right way, How can I prevent calling "didSelectRowAtIndexPath" method when I touched spaceView ?
like Inder said below, u can use an UIButton to solve the issue.
1) in your custom cell: at the bottom of your custom cell add a blank UIButton with the height u actually need between cells, and customize its background color according to your needs.
2) in cellforrowatindexpath: disable button of each cell. (or you can also do that in Interface Builder of previous step)
result: u have a clear disabled button that will appear as required space between cells.
Make your spaceView as UIButton (I guess it's UIView right now)
When you add UIButton that touch will be consumed by the button thus touch won't be passed to it's parent.
OR
I'm not sure if it will work or not, you can add tap gesture to your spaceView

Why is my UITableViewCell imageView image disappearing?

I have an image in a cell's imageView, but If I scroll the uitableview up so that that particular cell goes off of the screen, then let it come back down, the imageView disappears.
How can I fix this?
The left column is the tableview untouched, the middle column is the tableview pulled up, the right column is the tableview released
As Kevin mentioned, your problem may be in the way you set the image. Are you creating an imageView and assigning it to the cell's imageView property or the image inside the property? without looking at the code, I would suggest checking what happens when the cell is created. in the cell == nil case. Hope it helps.

Resources