How to Draw a line in UITableViewCell - iphone

I need to draw a line in UITableviewcell.
There is any possible way to do this one?
If anybody having any idea about to draw a line in UITableview cell please reply me.
Thanks.

If the line is horizontal or vertical you could add a black UIView. Like this.
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0, 25, cell.contentView.bounds.size.width, 1)] autorelease];
lineView.backgroundColor = [UIColor blackColor];
lineView.autoresizingMask = 0x3f;
[cell.contentView addSubview:lineView];
The one above is for one horizontal line. The following code works if you need a vertical line in the middle of the cell
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake((cell.contentView.bounds.size.width/2), 0, 1, cell.contentView.bounds.size.height)] autorelease];

The easiest way is to take the image of the line and put it in UIImageView and add the image view to cell's content view... don't go all the way to actually 'drawing' it.. it'll be an overkill..

Related

ios 7 - uitableviewCell Separator line

I need to display separator line in iOS 7 same like iOS 6 (as my image 2). I am using below code in CellForRowAtIndexPath:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0];
[cell.contentView addSubview:separatorLineView];
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_highlighted.png"]];
cell.selectedBackgroundView = customColorView;
cell.backgroundColor=[UIColor clearColor];
//Separation style for iOS7
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
return cell;
I am getting output as below image:
But my output should like below image for both IOS 6 and 7:
Can anyone please help me how to achieve this? Thanks..
contentView doesn't always have the same size as the cell itself, add separator view on top of the cell view above the content view:
separatorLineView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell insertSubview:separatorLineView aboveSubview:cell.contentView];
UITableView has a property separatorInset. Use that to set the insets of the table view separators to zero to let them span the full width of the screen.
[tableView setSeparatorInset:UIEdgeInsetsZero];
OR
You can set separator inset property in xib also. Change it to custom. And make both left and right values to zero.

Adding separator in UICollectionViewCell like one in UITableViewCell

I am developing an app in which I need " a separator line " after each row of UICollectionViewCell...(like in UITableView after each row).
Anyone having idea how to get this?
Create a UIView with a black background that is 1 pixel high and 320 pixels wide?
For Example
For Horizontal Line
UIView *horizontalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,1,linelenth)];
horizontalLine.backgroundColor = [UIColor blackColor];
[self.view addSubview:horizontalLine];
[horizontalLine release];
For Vertical Line
UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(x cordinate,y cordinate,linelenth,1)];
verticalLine.backgroundColor = [UIColor blackColor];
[self.view addSubview:verticalLine];
[verticalLine release];

Having issue with reusing UITableViewCell

I have the following code which draws a separator line and text for a UITableViewCell. It looks fine but when I scroll off screen then back, the separator line is gone but the text is still fine. Any ideas?
static NSString *aProgressIdentifier = #"CustomerCell";
UITableViewCell *aCustomerCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
aCustomerCell.contentView.backgroundColor = [UIColor whiteColor];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell addSubview:aLine];
[aLine release];
}
CMACustomer *aCustomerObject = aCellObject;
aCustomerCell.textLabel.text = aCustomerObject.customerFullName;
aCustomerCell.detailTextLabel.text = nil;
aCell = aCustomerCell;
Try to add the "aLine" image view as subview of the contentView and not the whole table itself. Probably when the cell is reused and then layoutSubviews is called again, the contentView overlaps (white background) your aLine. Infact consider that iOS default cells have their subviews dynamically redrawn and resized each time they are displayed on screen.
So I would try this:
[aCustomerCell.contentView addSubview:aLine];
If this doesn't work, what can you do is to remove the contentView completely and add your own custom subviews (do this inside the if(!aCustomerCell) and not outside unless you will not get the benefits of the cell re-use):
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
[cell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell.contentView addSubview:aLine];
[aLine release];
}
Finally another check is verify that the cell height is > 72 (it seems a trivial check but its often source of headaches!).
The table view is using a pool of cells, so you can't be sure which one you're getting for any given index path. You can use the cell or the content view, but be sure to add only one of your custom lines per cell.
UIImageView *aLine = (UIImageView *)[cell viewWithTag:64];
if (!aLine) {
// etc.
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.tag = 64;
[cell addSubview:aLine];
//
}
// other formatting logic here, you can also hide/show aLine based on biz logic
try adding it to the contentView
[aCustomerCell.contentView addSubview:aLine]

ios uitextfield like notes

I wish that my uitextfield seems like the notes of the iPhone, yellow with the lines..
what's the best way to do this?
thanks in advance!
You mean UItextview right? ... its not a UITextField.
However, clear your background on the UITextView.
self.textView.backgroundColor = [UIColor clearColor];
Then create ImageView that u put as a subview to your textview.
backgroundimage = [[UIImageView alloc] initWithFrame: CGRectMake(0, -14,textView.frame.size.width, textView.fram.size.height)];
set a pattern image as backgroundColor. This will be your lines. Just create on or two lines . Then it should repeat itself.
backgroundimage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lines.png"];
then add your subview to the textview.
[self.textView addSubview:backgroundimage];
Good luck!

iPhone: Removing rounded corners from table view cell

i have a table view in my application which shows rounded corners by default.
Is there any way to remove the rounded corners.
I have #import <QuartzCore/QuartzCore.h> in place and included the frame work as well.
eve the following code does not work
self.clipsToBounds = NO;
self.table.layer.cornerRadius = 0.0;
can any one suggest a solution?
Thanks in advance
For a UITableViewStyleGrouped cell, you'll need to change the backgroundView on your cell.
UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
bg.backgroundColor = [UIColor whiteColor];
cell.backgroundView = bg;
[bg release]; // if you're not using ARC
Are you using UITableViewStyleGrouped on your table view? If so change the style to UITableViewStylePlain and the cells will be rectangulars without rounded corners.