views:

346

answers:

1

Hi.

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.

+1  A: 

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)

ComSubVie
Using CGRect frame = cell.frame;frame.size.width = 123f;cell.frame = frame;and defining a background color for my cell, the cell remained the same size, despising of the values I used for its frame. Besides, resizing my cell like this would't prevent the cell separator to show it's ugly face 'till the right of the table, would it?I tried to resize the whole view, but it made the index "shrink", and I don't want that, only the cells (and the separator).The only way I'm seeing to do what I want is defining an image working as a separator for my cells, and define it as background.
camilo
You could also change the style of your tableview so that it uses no separators (`tableView.separatorStyle = UITableViewCellSeparatorStyleNone`) and add your own separator as UIImageView as subview to the cell (`[cell addSubview:...];`).
ComSubVie
Yes, that was what I did. And to set the cell's background of the size I want, I inserted a label in the cell, and modified it's background. It's not "clean" but it works...
camilo