Custom UITableViewCell not Showing Reorder Control - ios

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.

Related

UITableView within UITableViewCell does not get updated

I have tried to create a custom cell that would display a list of items. To achieve this I tried to create a custom cell with UITableView inside it with scroll disabled.
The problem I have with this is when i try to apply changes to the cells in the inner UITableView data just does not get updated and the cell stays as it was. I have tried calling [tableView reloadData], [cell setNeedsDispaly], [cell setNeedsLayout] to no avail.
It seems like the data that has been applied when the cell was initialised persists through any attempts to change it. Though, when I create a breakpoint in cellForRowAtIndexPath: the data does get updated but is not rendered.(e.g. text property of UILabel has new value, but text is old on the screen.)
You need to nil the cell and then reload the table view. For some reason iOS caches table view data and stuff doesn't get updated correctly.
You could try overriding UITableViewCell's prepareForReuse(): in that method you can set all the cell's outlets/properties to nil. You would do this for the cells in the main cell's table view. I am assuming those are custom cells as well and that you have a custom UITableViewCell subclass for them.
Refer [I have two views on one cell, when I click on a cell it will be hidden and one edit form will be expanded on that. How to resolve that? ..check in accepted answer methods ...
[tableView reloadRowsAtIndexPaths:ll withRowAnimation:UITableViewRowAnimationAutomatic] in didSelectRowAtIndexPath

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.

How to access parent object in Objective C

I am creating a Custom UITableViewCell with a UITextfield in it, I would like to know how to access the the custom UItableViewCell from the UITextField delegate didend?
I am trying this
- (void)textFieldDidEndEditing:(UITextField *)textField {
CustomFinishingCell *updateCell = (CustomFinishingCell *)textField.superview;
but that's not returning a UITableViewCell, I think it's returning the UItableScrollView or something like that.
The textField's superview will actually be the cell's contentView. You could probably get the cell by doing:
CustomFinishingCell *updateCell = (CustomFinishingCell *)textField.superview.superview;
That's a little clunky though.
I also wouldn't recommend using tags as that's even more clunky.
What I would do, is create a subclass of UITextField with a property for your CustomFinishingCell and set that when creating the cell (where you add the UITextField). That will ensure everything keeps working, even if the user resorts the cells, adds/removes cells etc, and will work well with cell reuse.
Update:
In a lot of cases, it will be better to actually use #TimReddy's answer and set the delegate of the UITextField to the CustomFinishingCell and move the - (void)textFieldDidEndEditing:(UITextField *)textField etc code to that subclass. That will save you having to create another subclass for UITextField.
You need to have your custom UITableViewCell be that UITextView's delegate. That way you know for certain when the delegate fires it is that UITableViewCell's text view.
You may need to so some cleanup work before the UITableViewCell gets recycled tho.
You could give each cell view a tag, then call UIView's viewWithTag to get the view with the specified tag.
https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html
This is how I do it:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[textField convertPoint:textField.frame.origin toView:self.tableView]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
I prefer this approach because it doesn't rely on tags or maintaining a reference to the container view from the UITextField, or traversing the view hierarchy. All those approaches are brittle for one reason or another.
Tags approach: You need to set the tag in tableView:cellForRowAtIndexPath: and if you have multiple sections, you need to encode the section.
Adding a property to UITextField: This either requires a UITextField category and associated objects, or subclassing UITextField. Both work, but are overkill.
Traversing the view hierarchy is brittle because it depends on implementation details that could change at any time.

Use setHighlighted in the UITableViewController

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.

Want to fire IBAction on Touch up Inside of Static TableViewCells

I have a Static TableViewCells on a storyboard. There are just labels in those cells. I would like to fire IBAction event upon touch. What I am doing right now is create a full-size white button and linked to IBAction. But it doesn't show highlighted color when the cell is selected because cell is behind the button.
UITableViewCells have a delegate method specifically for handling row touches -tableView:didSelectRowAtIndexPath:
If you need a custom button over each cell, subclass UITableViewCell instead of going through a storyboard. As much as they may be a timesaver, storyboards are really a limiting factor when it comes to nitty gritty iOS programming.
Try adding this in your cellForRowAtIndexPath (when you initialize each cell):
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
Does this fix it?

Resources