UITableviewcell spacing between cells - iphone

I am creating UITableView cells dynamically and adding three labels on to it.I made my table as transparent by opaque no and clearcolor.
But the cells are being shown without spaces.I need to give space between cells.
Actually if i load a custom UITableViewCell from the xib file the space comes automatically. How to solve this issue?

UITableView with custom spacing
You can do this by cheating the tableView cell. By default you can only specify a single line, etched line or none so here is an alternative. You'll need to override the tableView delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Calculate your cell height with its contents, margin/padding/line width and return it. Set the background color of the cell to [UIColor clearColor] and make your table background the desired color for your separator. Subclass UITableViewCell and attach a backgroundContainerView and make it the desired background color for your cell. Attach all the subview to that backgroundContainerView.
Finally, in the UITableViewCell subclass implement
- (void)layoutSubviews
Here you should layout the subviews with the container view as if that is the whole cell.

In UITableView you have 2 properties : separatorStyle and separatorColor. Use them maybe your separator color is clearcolor so try and change it.

This problem is due to the difference in height in TableView and Custom cell. Set the cell height in heightForRowAtIndexPath delegate.

Related

UITableView weird padding at the bottom of cell

I have a UITableView in which I am displaying bunch of custom cells. The cell is configured to be 50 pixels in height:
I am retuning 50 in the heightForRow method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50.f
}
But in the end, there's a weird 10-15ish pixel padding/spacing between each cell:
What am i doing wrong?
Thanks!
Without seeing your custom cell's configuration code, I can only guess. The space you see is likely internal to the cell. Perhaps you've added a green subview, but it isn't sized to fit the cell. You probably need to set the subview to have a flexible height (or if you're using Auto Layout, pin the top and bottom to the parent).
Or maybe you have one cell per section and you're somehow creating a blank header or footer.

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.

Set Cell Subview Position After Height Set?

In my app I have a tableview, with a custom tableviewcell and that has a UIImageView in it.
However, my issue. - (void)configureCell:(MyCustomCell *)cell atIndexPath:(NSIndexPath *)indexPathwhere I set the frame of my image is called before - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath where I set the cell height.
Meaning, the image position isn't correct (its dynamic, changes depending on cell height).
How do you suggest I get around this?
Thanks.
When do you call your configureCell:atIndexPath: method? In your tableView:cellForRowAtIndexPath: implementation?
Anyway, try to set you image height in tableView:willDisplayCell: delegate method instead. This method is called just bore the cell is displayed on screen so that it will not be resized afterwards by other internal methods.
Besides, can't you use the AutoresizingMask property to make your image view resize automatically depending on its container view (the cell)?

UITableViewCell's detaiTextLabel overlaps with custom subviews

I want to customize UITableViewCell by adding UILabel and two UIButton as its subview.
The cell style will be UITableViewCellStyleSubtitle and these three items would have to
next (on the left) of detailTextLabel.But when i do this, detailTextLabel overlaps with these items and display clipped or partial subviews.
Any way to handle out this without subclassing UITableViewCell if possible.
Thanks.
do you want to display something with detailTextLabel? If not you could try to hide it.
cell.detailTextLabel.hidden = YES;
otherwise you could add another Label at a better position which displays your detailText

How to stop text in a UITableViewCell running into the area used by the disclosure indicator?

I am display some long text in a cell, and resizing the height of the cell using heightForRowAtIndexPath. However, when the text is displayed it is running into the area used by the (blank) disclosure indicator.
When such a row is selected, and the checkmark is displayed, the text reformats itself to not use the indicator area, causing a visual effect I do not want.
If there was a UITableViewCellAccessoryBlank accessory type (rather than UITableViewCellAccessoryNone), maybe the text wouldn't wrap into that area when displaying. Am I going to have to create a custom cell and layout my own label, or is there a simpler way?
First of all, I don't see a property call UITableViewCellAccessoryBlank in the UITableView Cell class reference so I don't think this will work.
I think you have 2 options :
Create a custom cell, like you suggest.
Configure the textLabel of your cell to change his contentMode.
I read this in UILabel class reference :
The default content mode of the
UILabel class is
UIViewContentModeRedraw. This mode
causes the view to redraw its contents
every time its bounding rectangle
changes. You can change this mode by
modifying the inherited contentMode
property of the class.
I suppose that the textLabel bounds change every time you change the accessory type, so by default it redraw himself.
You can try this in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method :
cell.textLabel.contentMode = UiViewSomeContentMode;
Content mode list can be found here. I'm not sure which one you should use so I let you try.
EDIT
It seems that contentMode is not working. So you should use a custom UITableViewCell to prevent any animation when adding an accessoryView.
Hope this helps !