UILabel in UITableView - ios

My question is how to add a gray edge to each label on (UITableViewCell that contains many 3 labels)?
So that my labels for each cell will be sepraeted by the edge.

You can have a UIView as content of UITableViewCell by adding subviews to contentView property of UITableViewCell.
Add your labels as subviews to the cell and to have separators in between labels you can use this kind of code
UIView* vertLineView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 1, 44)];
vertLineView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:vertLineView];
Reference:Draw vertical separator on UITableViewCell
Hope this solves your problem.

If you only want to separate the labels from each other, you could add a UIImageView between them with a small width (like 1 or 2 points) with a dark picture. If you want them to have border, set:
myLabel.layer.borderColor = [UIColor blackColor];
myLabel.layer.borderWidth = 1.0;
Don't forget to include QuarzCore.

Related

How to add subviews to a tableview cell dynamically

I created a custom tableview cell in IB. I add a scroll view as a subview of the cell's contentView, and create the IBOutlet in the tableview cell subclass, and make the connection. My problem is, I want to add subviews to the cell dynamically, but when I did this in code, nothing happens. The cell is rendered successfully, but the scrollView has nothing to show (no subviews on it).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// deque the cell
// create some label depending on the MODEL object
// (this is done by code, not in IB. because the label is content-based)
// we don't know how many labels in advance
[cell.scrollView addSubview: label]; // not working !!
...
return cell;
}
But if I add the subviews in IB (which means the subviews are pre-defined), it works.
Is there any ways to add the subviews to a cell dynamically ? Or maybe I put the code in the wrong place ?
Thanks for all of the responses.
This is really embarrassing. The problem is I mis-config the label property, make the label text color white, but somehow the scrollView background is also white. So I can't see the labels, but actually they are already there.
All the responses are helpful, but #Shebin's answer gave me the hint to check the color, so I think I should mark his answer as the best.
Try this
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, 130, 30)];
label1.text = #"any text";
label1.textColor = [UIColor redColor];
label1.backgroundColor = [UIColor greenColor];
[cell addSubview:label1];// first try adding to cell
if you need to add as subview of cell.scrollView
NSLog(#"scrollView %#",cell.scrollView);//it should not be nil
check scrollView content size and frame
For dynamic string this will also help you
NSString *string = #"This is the text";
CGSize stringsize = [string sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, stringsize.width+30/*you have to adjust 30 as u required*/, 30)];
label1.text = string;
label1.textColor = [UIColor redColor];
label1.backgroundColor = [UIColor greenColor];
cell.scrollView.contentSize = CGSizeMake(label1.frame.size.width+30/*you have to adjust 30 as u required*/, label1.frame.size.height);
[cell.scrollView addSubview:label1];
You need to add proper constraints on your sub views. The scrollable size of a UIScrollView is computed based on the constraints of its subviews. Please ensure that constraints on the content view of your cell is properly added.
And if you are not setting constraints on your subview like label etc. then its intrinsicContentSize is used.

Add Margin to a custom UI Table Cell

I'm fairly new to the native app dev world and predominately a Front-End Dev/designer - I have built a IOS7 app in Xcode 5 - which contains a UITable with several custom cells. I would like to add margin of approx 8px to each of the cells (to look like the image below) - and change the colour of the link arrow which appears via a push segue - but have no idea how to do either despite a good web search / book read - theres doesnt seem to be the relevant options on the storyboard.
Can anyone advice if possible?
the code goes like this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for add margin of each cell (this code add margin only to top of each cell):
UIView *separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 8)];
separatorLineView.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:separatorLineView];
and for color of arrow you have to create an image and insert it with this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 7, 11)];
[label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourimage.png"]]];
cell.accessoryView = label;
You can include a margin to the cell by resizing the table itself. Instead of the standard 320 pixels width, change the width of the table to 312. That will give you an overall margin without having to meddle with the internals of the table view.
CGFloat margin = 8;
self.tableView.frame = CGRectMake(0, 0, self.view.frame.width-margin, self.view.frame.height);
In order to change the color of the arrow, you have to change what's called the accessoryView of the UITableViewCell. It can be any UIView.
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coloredArrow.png"]];

iOS UITableView unexpectedly adding margins

I have a UITableView, and currently it has a single cell in it. I have written a custom TableViewCell class which inherits from UITableViewCell in order to do some custom drawing. I have set the width of the table to the desired size, and am trying to set the width of the cell to the same size, so it will fill up the entire width of the table. The problem seems to be that I'm getting some margins on the left and right sides of the cell, and I don't know why.
Here's an example of the problem.
I made the TableView background black to be more clear. The TableView is the correct size. The background image is added to the cell, not the table, and it should be taking up the full width of the table.
I have tried making the TableView wider (as wide as the screen) to try to accommodate the size of the background image, but that doesn't quite do it. I would rather just figure out where these margins are coming from, and how I can get rid of them.
The TableView itself is initialized in Interface Builder. The style is set to Grouped, scrolling is disabled, and the view mode is set to Scale To Fill.
Here's the cell class' initWithStyle method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
_primaryLabel = [[UILabel alloc] init];
_primaryLabel.textAlignment = UITextAlignmentLeft;
_primaryLabel.font = [UIFont systemFontOfSize:18];
_primaryLabel.backgroundColor = [UIColor clearColor];
_detailLabel = [[UILabel alloc] init];
_detailLabel.textAlignment = UITextAlignmentLeft;
_detailLabel.font = [UIFont systemFontOfSize:12];
_detailLabel.backgroundColor = [UIColor clearColor];
_icon = [[UIImageView alloc] init];
[self.contentView addSubview:_primaryLabel];
[self.contentView addSubview:_detailLabel];
[self.contentView addSubview:_icon];
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImageView* whiteDisclosureView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 13)];
[whiteDisclosureView setImage:[UIImage imageNamed:#"white_disclosure.png"]];
self.accessoryView = whiteDisclosureView;
UIImageView * background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 305, 61)];
[background setImage:[UIImage imageNamed:#"button_silver.png"]];
[self setBackgroundView:background];
self.backgroundColor = [UIColor clearColor];
self.frame = self.contentView.frame = CGRectMake(0, 0, 305, 61);
}
return self;
}
Is your tableView using "grouped" style? With grouped style, iOS normally adds left and right margin for the table cells.
It may be possible to remedy this by adjusting the frame of the tableView to slightly outside its superview. See here for example in previous question
You shouldn't explicitly set your cell's frame (size), but declare its style. (If you don't do that already) The cells are designed to automatically take up the whole space. (Horizontally)
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
If not when allocating your cell, how do you set the cell's frame?
EDIT: Instead of using hardcoded frame sizes, use self.frame. Additionally, remove the last statement where you set the frame.
Another alternative solution I used.
#jonkroll's solution does work but it does not fulfil my need. I have a header section in the table view which I want to keep the margin left and right as is, but want to remove them on the normal cell.
The solution I did is to implement a layoutSubViews method in a custom table view cell. Within this method, set the contentView's width equal to table cell's width.
-(void)layoutSubviews {
self.contentView.frame.size.width = self.frame.size.width;
}
This may be very late, but I think some people will run into the same problem as well. Hope this solution works for you guys : )

Remove rounded corners UITableViewCell

I've been trying to do this for a while now. I just want to remove the rounded corners you can see on this picture :
Does anyone know how to do it ?
[EDIT 1 : CELL BUILDING]
I am subclassing UITableViewCell to make my own layout. And then I'm adding a background to these cells like below :
UIImageView* imageView = [ [ UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
imageView.image = cellBackground;
cell.backgroundView = imageView;
I you need to edit the backgroundview of the corresponding cell. E.g.
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor whiteColor]; // or any color
cell.backgroundView = bg;
[bg release];
The corners aren't rounded by default, just so you know.
Do you have a background image for the cell that makes the corners
seem rounded (most likely)?
Is each cell actually a section of the
tableview and there is no space between them?
Are you testing on a
jailbroken iPhone and you have some winterboard theme installed that
changes the style of tableviews?

Vertical space between UITableViewCells after setting the backgroundView on a cell

I am implementing UITableViewCells that have a custom background and expands when tapped.
I am setting a custom view as my UITableViewCells backgroundView: (in RowForIndexPath)
if (cell == nil) {
CGRect f = CGRectMake(0.0f, 0.0f, 300.0f, 50.0f);
cell = [[UITableViewCell alloc] initWithFrame:f reuseIdentifier:cellIdentifier];
UIView *back = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 300.0f, 50.0f)];
back.layer.cornerRadius = 8.0f;
[back setBackgroundColor:[UIColor blackColor]];
This works fine, by setting the backgroundView instead of the contentView, my backgroundView scales to accommodate the new size of the cell after it expands (changing the height in heightForRowAtIndexPath after a tap).
My problem is now that I would like a few pixels vertical space between my cells. Using the above approach will make the rounded black cells be displayed "back to back".
Is there a way to add the vertical space between the cells or is there a completely different approach I can take to obtain the desired look of my cells?
Thanks in advance.
In order to display a space between cells and avoid the "back to back" issue that you are having you can add a UIView with the the following frame CGRectMake(0, 0, 320, 1) and the background set to a light gray for the standard cell separator or if you just want some padding you can make the background clear.
Personally I like to use interface builder but you can of course do this programmatically.
UIView *cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320 ,1)];
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor:[UIColor lightGrayColor]];
[cell addSubview:cellSeparator];
[cellSeparator release];
The reason I set the cellSeparator at the top of the cell is because the effect is really for the cells that fall in between the first and last rows. Also it is important to set the autoresizingMask and the contentMode to make sure the cell separator adjusts properly when you make size changes to the UITableViewCell. If you have any elements in the cell that start at x=0 y=0 of you will need to move them down the height of the cell separator plus perhaps some additional pixels for padding so that the separator doesn't run through any elements with in the cell.

Resources