What is the difference between setHighlighted and setSelected in UITableViewCell in iOS? - ios

What is the difference between setHighlighted and setSelected in UITableViewCell ?
If I just want to not highlight a cell when a selection is made, Should I override setHighlighter or Just set the selectionStyle to NONE.

setHighlighted will mark the object with a highlighted colour (or glow, depending on your settings) when the finger is touching down. On touch up, the highlight disappears and the object state returns to normal.
setSelected on the other hand, while similar, will be set on touch down, and will remain in the highlighted state until the next touch down event occurs.
What I think you want is to override setHighlighted (just return inside the method and don't call super), but more simply, you can just set the cell's UITableViewCellSelectionStyle to UITableViewCellSelectionStyleNone.

To not let the UITableView highlight the cell, implement and return NO in the tableView delegate method - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath.

Related

cell properties set in viewcontroller do not reflect

I've got a subclass of UITableViewCell. I want to create a shadow on the labels dependent on the brightness of the image on the background. However, this image is set in the TableViewController. When awakeFromNib is called, self.backImage.iamge is nil. I tried implementing initWithStyle in the subclass itself but it logged nil as well.
When the cell is eventually loaded tho, the image is displayed.
Is there an event that happends when the cell is updated trough
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
?
So your table view controller is setting the background image for the cell? I assume it does this in cellForRowAtIndexPath after dequeueing the cell?
If so, try creating a method on your custom cell that sets the label shadow. Call the method after dequeueing the cell and setting the background image. If necessary, pass a parameter in the method that tells how dark to make the shadow.

UITableView Selected Row Changes Subview Background Color

I have a UItableview with custom cells in it. The height of the cell changes when you select it and gives an expanding effect. However, when you you select the cell the background of all the subviews become transparent it seems. I've tried setting the cell's SelectedBackgroundView but that doesn't really affect the cells subviews.
Here are some images:
Closed:
Open:
![enter image description here][2]
This is how its supposed to look or at least does in XCode - (Sorry for the bad graphic here)
Call [tableView deselectRowAtIndexPath:indexPath animated:YES]; at didSelectRowAtIndexPath. This should solve your issue.
Edit
If you don't want to see any grey selection at all, then, in your cellForRowAtIndexPath, set the cell.selectionStyle to UITableViewCellSelectionStyleNone, like so:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Presuming that you have subclassed UITableViewCell for your custom cells, you can modify a cell's appearance when selected/deselected by overriding the setSelected method in your custom subclass. For example:
- (void) setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
//Configure the selected state here
} else {
//Configure the deselected state here
}
}
UITableViewCell changes the backgroundColor of all subviews on selection for some reason.
This might help:
DVColorLockView
If you want the normal selection behavior but want to exclude specific cell subviews, you can do something like this to lock the background color by subclassing and checking if it is locked in the backgroundColor setter.

Make certain area of UITableViewCell not selectable

I have a custom UITableViewCell. I have a UIImageView in the Cell and when I select the Image it changes like it should but the cell is also selected which I do not want. So my question is how can I make a certain area of my cell not selectable?
I tried overriding the touchesBegan or touchesEnded but that did not work because then I cannot select the cells at all.
if you only want to not show the selection color, use:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if you have something triggered by a cell tap, it will still happen, so if you want to disabled that also you need to take care of that in:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
or if it triggers a segue, in:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
In cellForRowAtIndexPath you can set userInteractionEnabled to false:
cell.userInteractionEnabled = NO;

Adjust UITableViewCell height on delete swipe

I have a UITableViewCell with dynamic height based on textual content. In tableview:heightForRowAtIndexPath: the height gets calculated. This works well.
When the cell enters editing mode with editingStyle UITableViewCellEditingStyleDelete it indents the cell a little and can push the content out some causing the rowHeight to change. Again this works well as switching the tableview to editing causes the table to relayout and therefor the rowHeight gets recalculated.
When you click the red button or swipe the cell a Delete button appears on right hand side of the cell. This however does NOT trigger a relayout of the table, only the cell itself. The problem here is that if the content goes over the bottom edge the cell does not get resized.
Is there a way to trigger a re-layout of the table when the delete button appears?
In general, height of UITableViewCell can be only setup via tableview:heightForRowAtIndexPath:, and the latter method only be called once (per row per section) before tableView:cellForRowAtIndexPath:, so you have to [tableview reloadData] if want to adjust the UITableViewCell height.
Maybe your can do something in these delegate methods
// The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
To your question, you could reset the properties of the text control at - (void)layoutSubviews in your custom UITableViewCell.
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat theWidth = self.frame.size.height; //changed when entering the edit style
...
}
Hope these will help you.

Changing the contentView when an Accessory button is tapped

I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.

Resources