Use setHighlighted in the UITableViewController - ios

Do I need to subclass UITableViewCell to use setHighlighted? I am trying to overlay an image on top of my cell between the moment that it is tapped and the moment the action happens. I used to do it by using setHighlighted in a custom UITableViewCell xib but I redid my app using custom cells straight from the sotryboard. It seems like over kill to subclass the whole cell only to be able to use setHighlighted.

Depending on your requirements, you could try using the UITableView delegate methods:
tableView:didHighlightRowAtIndexPath:
tableView:didUnhighlightRowAtIndexPath:
I would guess that subclassing the UITableViewCell and overriding setHighlighted might offer better performance though.

Related

Custom UITableViewCell not Showing Reorder Control

I have a UITableViewCell that is a subclass of CEWendell's SWTableViewCell, a type of cell that allows for custom buttons accessible by horizontal swipe. The problem is that even with tableView:moveRowAtIndexPath:toIndexPath: and tableView:canMoveRowAtIndexPath: implemented, as well as explicitly having [cell setShowsReorderControl:true], the reorder control does not show up when editing begins.
I have also tested this with a regular UITableViewCell with the same result.
This was a somewhat foolish mistake. Since I was using a UIViewController subclass, not a UITableViewController subclass, I needed to set [self.tableView setEditing:!self.tableView.editing animated:true], not [self setEditing:!self.editing animated:true] in order for the tableview to begin editing.

Adding UITableViewCell Image on button press

Is there a way to add an image to my UITableViewCell when I push a certain button?
According to the documentation, https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html, a UITableViewCell has a property called imageView which is a UIImageView meaning that all you need to do is create a UIImageView and set the UITableViewCell's imageView property to that of your UIImageView
Yes by creating a subclass of UITableViewCell, you can create tableviewcells in any format you want - Your subclass needs to know if the user pressed the button - many ways to do this - a plist, NSUserdefault or even a property in the TableViewCell subclass
Make sure your TableView delegate methods particularly CellForRowAtIndexpath calls your custom class.
Just [Tableview reloaddata] every time the button is toggled.
A good reference is the Elements sample app from apple.

UICollectionViewCells and Buttons

I've been trying to get my UICollectionView to respond differently to single and double taps but all the answers I have found seem to suggest this is not really feasible because the single taps get recognised first. It works on really slow taps, but anything faster always initiates the default gesture recogniser (if anybody has got this to work I would love to know)...
So anyway, I have now resorted to putting buttons in my UICollectionViewCell (which has it's own class and NIB file).
The question is this:
What is considered the best way to use the button in the UIViewController of the collectionView?
I currently have a protocol in the header of my subclass of UICollectionViewCell and have declared my viewController as the delegate and implemented the required protocol functions.
In collectionView:cellForItemAtIndexPath: I set the VC as the delegate of the cell.
It all works but it seems a bit long-winded, and maybe not a great way of doing this.
The other way I was thinking of was instead of using delegates to simply call addTarget:Action: on the property of the UICollectionViewCell in collectionView:cellForItemAtIndexPath:.
This seems simpler but the delegate pattern looks to me like the better fit.
Any and all advice on which would be better, why, and any more appropriate alternatives welcomed!
Thanks.
You're doing the right think using the delegation pattern. The ultimate responsible object for any action of your views is the viewController who's displaying those views. Therefore, using it as the delegate for you cell's protocol is just right.
create a custom subview of UICollectionViewCell and place your button in the initWithFrame method. Declare the button to be public so you can use it later in your uicollectionviewcontroller or uicollectionview if creating programmatically.

Why I'm getting gesture recognizers cannot be used on prototype objects?

I'm getting the above error from storyboard when I dropped a UITapGestureRecognizer inside a UIView which is inside a UITableViewCell in my scene.
Any idea why I'm getting this error ?
I'm not sure why the restriction is in place but I know why you are getting it the error.
When you design a UITableViewCell in StoryBoard you are only designing a prototype object. i.e. the object may never actually exist. It only gets ACTUALLY created in tableView:cellForRowAtIndexPath:
What might be a better approach is to create the gestureRecognizer when you configure the cell in code. This way you won't have this restriction.
I'd also possibly look at whether you actually need it? If it is just for a single tap with one finger then you may be better off coding the touchesEnded or just using a UIButton instead.
Found the reason myself. storyboard only allows that when we have a UITableViewController or it's subclass and the tableview content should be 'static cells' instead of default 'Dynamic Prototypes'. In that configures I can add a gesture recognizer inside the cell subview.
But that is a limitation and will not work in my case as I have a very customized view controller subclass instead of table view controller subclass. Need to find other way around it seems :(

Add right margin for detailTextLabel in UITableViewCell

What's the easiest way to add right margin to detailTextLabel inside an instance of UITableViewCell?
The detailTextLabel used by the standard UITableViewCellLabel is not a standard label but a special subclass, private, called UITableViewLabel. Customizing it it's not easy as you probably experienced as some methods are overridden by the subclass. You can take some control by de-queing the cell from a reusable pool (because in such case the cell is not recreated and so some of the overridden methods are not called again and in such case basic UILabel settings will apply) but the result couldn't be satisfactory.
So the best approach for custom views is not to try to tweak the existing on-the-shelf views but make a custom cell. This can be done programmatically in the tableView:cellForRow:atIndexPath: delegate method, or by creating a UITableView subclass or loading it from a nib file. Even for simple changes like this.

Resources