TableViewCells width in iPhone - 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)

Related

How to set the height of subviews of UItableViewCell when cell is created programaticalliy

I have custom cell created with nib. In the table view I am using the method -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath to set the height of cell
Everything works fine.
But the problem is that I also want to change the size of UIlabel which is added as subview in nib of cell.
How do I do that?
Which metod to override in customcell class ?
The method you are looking for is:
-(void)layoutSubviews
{
[super layoutSubview];
//Do your magic
}
layoutSubviews is call after the cell is created and whenever the device orientation changes, to allow you to resize and/or move the subviews (plus make any other minor adjustments) to compensate for differences between the orientations, but in this case you can also use it to redraw your subviews.
As you have UILabel in your custom cell class, make a function in that class that take frame you want to set as parameter. Set frame of label in that function. You need to call this function from your cellForRowAtIndexPath method before returning the cell.
If you are reusing your custom cell you should call method only when the (cell == nil)
Also if you can add some code in your question that would be helpful and you can get more precise answer.
You need to treat your custom cell in the same way you would treat a normal view or view controller class with a xib. i.e. You need to create IBOutlets for the controls in you custom cell and during creation of the cell you can access the controls quite easily.
myCell.myCustomUILabel.text = #"blah"
There are some gotchas when using custom cells in a xib and onnecting up the IBOutlets. This SO answer (of mine) explains how to create and link up IBOutlets of a custom cell.

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.

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

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.

Accessory View out of visible range of UITableViewCell

I create custom UITableViewCell and just put the Accessory Type to Disclosure Button
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
But the half Accessory is out of the View on the right border of the screen.
Sadly, but I can not post a screenshot because I need 10 posts to do that.
I searched everywhere but I did not found anything about this problem.
Since you are not allowed to change the width if UITableViewCell the problem here may be with the width of UITableView itself.
Try adjusting the tableView.frame.size.width to fit the main window/screen width.
You can also check the width of parent views if there are any.

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