Determine whether UITableViewCell is editing from "swipe" or "self.editButton" - iphone

I'm trying to determine whether a UITableViewCell subclass is in edit mode from a user's swipe (in which case I don't need to indent my subviews) or from the user pressing the "Edit" button associated with the UITableViewController. (In which case I do.)
I know it's possible from a cell's perspective, since the self.textLabel view automatically indents properly. I have tried:
-(void)layoutSubviews {
[super layoutSubviews];
CGRect labelFrame = self.textLabel.frame;
labelFrame.origin.x += 5;
myCustomUILabel.frame = labelFrame;
}
But my custom label does not properly indent. (Though the self.textLabel view does?)
I would like to avoid the following:
Providing the cells with a reference to the parent table.
Overriding methods in the UITableViewController class to let the cells know whether they are being edited individually or the entire table is editing.

You can override willTransitionToState: in your UITableViewCell subclass. When the "Edit" button is pressed the state will be UITableViewCellStateShowingEditControlMask(=1) and when swiping it will be UITableViewCellStateShowingDeleteConfirmationMask(=2).

You should not be doing the indentation manually. The UITableViewCell will do it for you!
All you have to do is make sure that you add your subviews to 'contentView' of the UITableViewCell. This is the reason why self.textLabel indents properly as you have identified.
Look at the documentation of contentView property for a UITableViewCell:
The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

Related

Custom UITableViewCell with Storyboard

My goal is to create a custom UITableViewCell which contains 2 UILabels. One is the title and one is the detail. The detail label should allow for 3 rows of text.
So I went on and created a custom table view cell in storyboard. I also created a subclass of UITableViewCell and linked the two together.
I the added two UILabel to the cell in storyboard and placed them where i wanted them to be and linked them to their coresponding outlets in teh subclass. Since the content of the labels varies I wanted to align the text vertically to the top. As I understand the only way to do this is by calling the sizeToFit method on the label. I execute this under in the sub class of UITableViewCell:
-(void)layoutSubviews {
[super layoutSubviews];
[self.detailTextLabel sizeToFit];
}
So far everything seems fine and the text in the detailTextLabel is aligned as it should. Although when i satrt interacting with the cell, for example slide my finger over it so the delete button appears, the detailTextLabel will change size to the size that was set in storyboard. This causes the text to be misaligned. Similar things happen when i select the cell and change to another view and the return to the table view via a tab bar
My question is: Is there any way of creating this custom cell differently using storyboard or is my only alterative to create everything programtically?
Regard, Christian
Maybe you should take a look at this if you still want to vertically align your text in your UILabel without sizeToFit (who will resize it when you will interact with your cell).
About your question, I think you can create your custom cell from a xib file like this.

UITableViewCell subview (left side)

Is it possible to implement a subview of a UITableViewCell on the left side of the cell that crossfades when the table enters the editing mode?
Another problem I face is that the bounds of the cell.textLabel are read-only. Is it possible to use some sort of inset for that label? (Because I, like mentioned above, want to use a View on the left side)
Edit: How do I perform an action (in this case fade a subview) when the whole table enters editingmode? (Not through a swipe over a cell). The reason why I want to implement this is because the tableview shows the "-" button on the left side of the cells if it is in editingmode. (I want to show my own button on the left side of the cells if editing=NO, fade it out if editing=YES and show it again if the tableView leaves editingmode (editing=NO))
A first thought directs me to something like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if(editing) {
//fade out my own button
} else {
//show my own button
}
}
But I would have to loop through all the cells and I think this leads to a substantial performance worsening. What do you think?
You want to set the Editing Accessory View of a UITableViewCell. See the editingAccessoryView property for information on this. More information can be found in the Customizing Cells section of the TableView Programming Guide.
The label within the content view of a table view cell is read-only so you can not reassign it but its properties are mutable. You can actually move the label around within the bounds of the table view cell. It's probably confusing to grasp, but have a look at the Characteristics of Cell Objects section of the Table View Programming Guide. In it, the bounding boxes for each of the nested views are shown (not all of them are always visible). For your purposes, a good exercise would be to set the background color of each subview of a UITableViewCell to a different color and then try to adjust the sizes of them. Doing so and understanding what's going on will probably let you achieve the end-result that you desire.
In the end I created a custom UILabel that I used instead of the default UITableView textLabel and added a custom button on the left side. In the setEditing method I fade this button in/out.

Another Button at UITableView Editing Mode

I need an additional button next to the Delete button when in editing mode of UITableView Cells. Any suggestions for the purpose is appreciated. Thank You.
I would suggest you first subclass UITableViewCell. In the respective init method you create the button and add it to the contentView as a subview. Make the button hidden. After that you overwrite layoutSubviews and position the button on your content view by setting the frame property of the button. Then also subclass willTransitionToState: and check if the state is UITableViewCellStateShowingEditControlMask. If that's the case make the button visible. If not hide it.
Note: If you add an additional button to the UITableViewCell you also need to adjust the textLabel frame and other stuff to not overlap the button's rectangle.
cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
or some other value than "check"
or
cell.editingAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoDark];
or with some other value than the info button, with it's target set to something appropriate
an accessory view trumps a type

TableViewCells width in iPhone

I'm trying to create a cell on a tableview which doesn't go "all the way" from left-to-right of the screen.
Basically, what I need is a cell which (as usual) starts on the left side of the screen, but with a given width, creating a free space on the right.
Is there any way of doing it? I wasn't able to find any example.
Sorry for the eventually noob question, and many thanks in advance.
In older versions of the SDK this was possible using the initWithFrame:reuseIdentifier:method, however this is deprecated since 3.0, so that you should create the UITableViewCell using the initWithStyle:reuseIdentifier: method.
You can access the frame of the UITableViewCell using the frame property and change it's size:
CGRect frame = cell.frame;
frame.size.width = 123f;
cell.frame = frame;
For indentation on the left side you can simply use the UITableViewDelegate method tableView:indentationLevelForRowAtIndexPath:
You can also add a specific subview to the UITableViewCell on the right side (an accessory view) using the accessoryView property of the cell:
UIView *view = ...
cell.accessoryView = view;
(If the value of this property is not nil, the UITableViewCell class uses the given view for the accessory view in the table view’s normal (default) state; it ignores the value of the accessoryType property. The provided accessory view can be a framework-provided control or label or a custom view. The accessory view appears in the the right side of the cell. UITableViewCell Class Reference)

Resizing UITableViewCell when entering edit mode

I have a cell that I have designed in Interface Builder, and when I enter edit mode, it moves the whole cell to the right to make space for the delete icon, but it pushes all items to the right, not shrink them down. It pushes a UILabel off of the edge of the cell. Also, this is over the move handles. How do I shrink these items down, instead of just moving them.
I've never made a custom tableview cell in Interface Builder. So, I don't know how to solve your problem on Interface Builder.
But if you programmatically add all items into the contentView property of UITableViewCell instead of adding these into UITableViewCell itself, these items would automatically shrink when entering edit mode.
I have a partial solution.
On the custom cell object I've created a IBOutlet to the view that holds all subviews in the cell (myContentView).
This way I can run this code to shrink it:
OrderReviewCell * cell = (OrderReviewCell * )[reviewTable cellForRowAtIndexPath:indexPath];
CGRect frame = cell.myContentView.frame;
frame.size.width = 200;
cell.myContentView.frame = frame;
I've figured that the best place to put this is in shouldIndentWhileEditingRowAtIndexPath but my problem is Where to put the code that returns the cell to its normal state?
Hope it helps and DO let me know when you find the right answer!
Gonso