UITableCellView visible lines on top - ios

I have an cell class that is inherited from UITableViewCell. It has next grey lines:
However if I don't set cell texts I don't see these lines:
So what can be the cause for this lines?
UPDATE
Here is code for my custom cell:
#implementation MDItemTableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
NSString *fontName = selected ? self.selectedFontName : self.fontName;
self.textLabel.font = [UIFont fontWithName:fontName size:self.fontSize];
}
#end
UPDATE 2
Here is code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *source = [(MDChoiceField *)self.field source];
MDChoiceItem *item = source[(NSUInteger)indexPath.row];
MDItemTableViewCell *cell = [(MDItemTableView *)self.fieldView cellForRowAtIndexPath:indexPath];
cell.textLabel.numberOfLines = 0;
NSString *string = NSLocalizedString(item.title, nil);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString:string];
[attributedString addAttribute:NSKernAttributeName
value:#(1.2)
range:NSMakeRange(0, [string length])];
cell.textLabel.attributedText = attributedString;
return cell;
}
And view one:
- (MDItemTableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MDItemTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.font = [UIFont fontWithName:self.fontName size:self.fontSize];
cell.fontSize = self.fontSize;
cell.fontName = self.fontName;
cell.selectedFontName = #"HelveticaNeue-Light";
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor colorWithHexString:BLUE_COLOR];
if ([cell respondsToSelector:#selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:LIGHT_BLUE_COLOR];
return cell;
}
Sorry for so many lines.

Note that the added lines are shorter than the table cell itself. If you turn on "Color Blended Layers" in the simulator Debug menu you can see that they seem to be the same length as the label. This suggests it's the label, not the cell, which is producing the oddity.
This question seems related: How can I remove UILabel's gray border on the right side?
And that suggests the following fix, added to cellForRowAtIndexPath::
[cell.textLabel setBackgroundColor:[UIColor clearColor]];

A few settings will help you,
As Richa's answer, self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; It'll hide seperator (default one) from the table.
As user3781721 answer, tableName.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; will remove any empty cell. So there'll be a plain look of table.
In tableview's cellForRow datasource, cell.clipsToBound = YES; before return the cell.
If still not solve then increase your cell height little, may be by 5 and check.

Put this code in ViewDidLoad
tableName.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

Using a 'Grouped' style UITableView will keep the table from showing these 'blank' cells if they are in fact empty. You can either declare this when you init the UITableView like this,
UITableView *myTable = [[UITableView alloc] initWithStyle:UITableViewStyleGrouped];
Or if you're using interface builder there's a drop down to change the style from 'Plain' to 'Grouped'

Those lines are the default separator lines of tableView. Just set the separator style as NONE. write or use this line wherever you have declared your UItableView. Either in Xib file or code.
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Update:1
- (void)viewDidLoad { [super viewDidLoad];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}
Use this, and let me know if it works or not.

Related

Separator line issues - uitableview cells with different heights

I have a UITableView with different cell heights. I have added a custom separator line also using the following code
UIView* separatorLineView = [[UIView alloc]initWithFrame:CGRectMake(1, cellReport.frame.size.height-1, table.frame.size.width-1, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cellReport.contentView addSubview:separatorLineView];
When i scroll my table extra separators appear between the rows and i dont understand why? Am i missing something? help me out.
In short UITableView displays separator at wrong position for some cells. Have attached an example image for reference.
P.S: I AM NOT USING AUTOLAYOUT
Please follow these steps
And do not need to every time your separator view. And set your frame every time.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *myIdentifier=#"Cell";
UITableViewCell *Cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
UIView* separatorLineView;
if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
separatorLineView = [[UIView alloc] initWithFrame:CGRectZero];
separatorLineView.backgroundColor = [UIColor grayColor];
separatorLineView.tag = 100;
[Cell.contentView addSubview:separatorLineView];
}
separatorLineView = (UIView *)[Cell.contentView viewWithTag:100];
separatorLineView.frame = CGRectMake(0, Cell.frame.size.height-1, Cell.frame.size.width, 1);
}

Wrong UITableView separator color on iOS7

I'm setting UITableView separator color this way:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.1f];
}
And SOMETIMES on iOS7 my table view separators look different (cells with data and empty cells separators have different alpha I guess), look at attached image:
How can I solve this issue?
If you want to remove all cell's separator which has empty content then use following line of code.
self.tableView.tableFooterView = [[UIView alloc] init];
in viewDidLoad method.
Other wise set self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; and set your custom UIImagefor separator.
EX:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"cell"; //[NSString stringWithFormat:#"S%1dR%1d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
.
. // your other code.
.
UIImageView *imgLine = [[UIImageView alloc] init];
imgLine.tag = 101;
[cell.contentView addSubview: imgLine];
}
.
. // your other code.
.
UIImageView *imgLine = (UIImageView *) [cell.contentView viewWithTag:101];
imgLine.frame = CGRectMake(5, 69, 310, 1);
imgLine.image = [UIImage imageNamed:#"queTblLine.png"];
return cell;
}
And don't forget to set self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
EDITE :
Not sure but might be helpful in your case:
set self.tableView.separatorInset = UIEdgeInsetsZero;
Get From question/answer.
Swap the order of the two statements. Set the color first, then the inset:
self.tableView.separatorColor = [UIColor redColor];
self.tableView.separatorInset = UIEdgeInsetsZero;
You can achieve this visually in storyboard or nib/xib
Put UIImageView at bottom edge of cell and you can set image as per your requirement
STEP 1: Set divider as per image
STEP 2: Set Separator style to none
You can set it programatically:
- (void)viewDidLoad {
[super viewDidLoad];
[[UITableView appearance] setSeparatorColor:[UIColor redColor]];
}

How to Implement DataSheet

I want to implement data sheet to display history of user. I want to implement that design like this:
But I dont know how to do that...Can anyone please help me
Edit:
Add the horizontal line at the specific position and the label at the position and it will look like this.
Create a tableview and than in cellForRowAtIndexPath method add this code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UILabel * numLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,5,33,30)];
numLbl.text = #"1";
[numLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
numLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:numLbl];
UILabel * nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30,5,50,30)];
nameLbl.text = #"john%Lakeview";
[nameLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
nameLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:nameLbl];
//create a hoizontal separator in cell to display it like column
UIView* hSeparatorview1 = [[UIView alloc] initWithFrame:CGRectMake(25, 0, 1, 30)];
hSeparatorview1.backgroundColor = [UIColor blackColor];
hSeparatorview1.tag = 1;
[cell addSubview:hSeparatorview1];
UIView* hSeparatorview2 = [[UIView alloc] initWithFrame:CGRectMake(85, 0, 1, 30)];
hSeparatorview2.backgroundColor = [UIColor blackColor];
hSeparatorview2.tag = 2;
[cell addSubview:hSeparatorview2];
}
return cell;
}
//this method is used to set the hight of the tableview cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 30;
}
I have created it for only two label and two horizontal view but you can create as many as you like.
And yes dot forget to place this code in didSelectRowAtIndexPath otherwise horizontal view will disappear when user click the cell.
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get the cell which is selected
UITableViewCell *selectedCell = [atableView cellForRowAtIndexPath:indexPath];
//set cell horizontal saparator view color of selected cell bcoz when cell selected all view color is gone
UIView *hSeparatorview1=[selectedCell viewWithTag:1];
hSeparatorview1.backgroundColor = [UIColor blackColor];
UIView *hSeparatorview2=[selectedCell viewWithTag:2];
hSeparatorview2.backgroundColor = [UIColor blackColor];
}
Better to try with Custom tableview Cell for text,and UIViews for lines.I'm also done the same in my app.

Hiding of label in Xcode is not working properly in my tableviewcell of iOS

I am having two labels created manually for displaying it in the tableviewcell named title and detail, code for displaying it are,
dealarray = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",nil];
detailarray = [[NSMutableArray alloc]initWithObjects:#"oneoneoneoneone oneoneoneoneoneoneoooooooo",#"two",#"three",#"fouronefouronefouronefouronefouronefouronefouron",nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
for(UILabel *lbl in [cell.contentView subviews])
{
[lbl removeFromSuperview];
}
cell.accessoryType= UITableViewCellAccessoryNone;
UILabel* title;
title= [[UILabel alloc] initWithFrame:CGRectMake(5,5,300,20)];
[cell.contentView addSubview:title];
[cell.contentView bringSubviewToFront:title];
[title setFont:[UIFont boldSystemFontOfSize:14]];
title.tag = 1001;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor blackColor];
title.text =[dealarray objectAtIndex:indexPath.row];
UILabel* detail;
detail= [[UILabel alloc] initWithFrame:CGRectMake(5,30,300,10)];
[cell.contentView addSubview:detail];
[cell.contentView bringSubviewToFront:detail];
[detail setFont:[UIFont systemFontOfSize:12]];
detail.tag = 1002;
detail.backgroundColor = [UIColor clearColor];
detail.textColor = [UIColor blackColor];
detail.text = [detailarray objectAtIndex:indexPath.row];
return cell
}
No problem in displaying those 2 labels and no problem in hiding all the 'detail' label and displaying the 'title' alone, the problem arises when I try to display the 'detail' label of the resp selective of cells.
Code tried:
// conti of cellforrowatindexpath
detail.numberOfLines = 3;
detail.lineBreakMode = NSLineBreakByWordWrapping;
if (a==-1)// declared 'a' in viewdidload as -1
{
((UILabel*)detail).hidden = YES;
}
else if(a==indexPath.row)
{
((UILabel*)detail).hidden = NO;
}
((UILabel*)detail).hidden = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
a=indexPath.row;
[tableview reloadData];
}
Sorry for posting large amount of codes, it may help any one who is searching for the wholesome data of my doubt.
Whats the mistake am doing, I can't hide the detail label for resp selecting of cells. Can anybody help in this regard?
May I suggest you to use xcode functionnality to design your cell content? (as easy as drag and drop of UILabel on your cell for example) Then you will be able to access them from your code using their respective "tag" id.
A correct way of doing is detail here : http://www.appcoda.com/customize-table-view-cells-for-uitableview/
I think it's a better way than programmatically creating content for your cell, because using xcode interface you will be able to flawlessly define your interface.
Anyway to respond precisely to your question you should try to hide the detail label from "didSelectRowAtIndexPath" :
// Retrieve the corresponding cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// Get the detail label (using its tag) you set it to 1002
UILabel *detail = (UILabel *)[cell viewWithTag:1002];
// Hide it
detail.hidden = true;
Hope this help.
There is an error in your logic. You code as written will always set detail.hidden to YES, the 2 preceding if are ignored, thus this (BTW you don't need the type coercion and extra brackets):
if(a==-1) detail.hidden = YES;
else if (a==indexPath.row) detail.hidden = NO;
else detail.hidden = YES;

Conditionally display image in UITableViewCell

I'm trying to display an image in only certain cells in my UITableView. Here's a my configureCell method:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
UIImageView *ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[ribbonView setImage:ribbon];
[cell addSubview:ribbonView];
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
And here's cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
This doesn't quite work because at first, all of the cells draw them ribbonView, regardless of the value of info.visited. I've stepped through the if/else and I see that the hiding code it being hit, though. But, if I navigate away from the list, and then come back, the correct ribbon state is visible. Scrolling the table view breaks it again.
The font colors are always correct, though.
If you are reusing cells, then it could likely be that you are adding multiple ribbonView subviews to the cell, so even if the info.visited for the current indexPath is NO, there is another ribbonView leftover on the cell that you can still see.
The thing to do is make certain you only ever have one ribbonView subview, which can be done either by removing old ribbonViews in your configuration method, or better by subclassing UITableViewCell and adding a ribbonView property to the cell, which gets set once and added to the cell's view hierarchy once, and which you can then access and set hidden to NO or YES in the configuration method.
EDIT: The cell text color will always be correct since you are changing the color on one instance of UILabel that is in the cell's view hierarchy. I expect you'd see the same buggy behavior if instead your configuration method added a new UILabel subview to the cell each time it was configured.
EDIT: Code to try
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
static NSInteger ribbonTag = 12345;
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
// re-use a ribbonView if one's already been added to this cell
UIImageView *ribbonView = [cell.contentView viewWithTag:ribbonTag];
if (!ribbonView){
ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
ribbonView.tag = ribbonTag;
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
[ribbonView setImage:ribbon];
// add subviews to contentView
[cell.contentView addSubview:ribbonView];
}
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
If you are using dequeueReusableCellWithIdentifier:forIndexPath: in the tableView:cellForRowAtIndexPath: method. Every time you reutilize a cell, you will create a new UIImageView and put it over the last one.
But to solve this you dont need to subclass. Not yet, because your cells are simple still. If you want to add more subviews, then subclassing is the only option.
One solution I can think of would be this:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
StoryInfo *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImageView *ribbonView = nil;
//My code:
for ( UIView *childView in cell.subviews ) {
if([childView isKindOfClass:[UIImageView class]] {
ribbonView = childView;
break;
}
}
//Note: this doesnt work if you have more than one UIImageView in your cell.
if(ribbonView == nil) {
UIImage *ribbon = [UIImage imageNamed:#"ribbon.png"];
ribbonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[ribbonView setImage:ribbon];
[cell addSubview:ribbonView];
}
//Ends here.
if([[NSNumber numberWithBool:NO] isEqualToNumber:info.visited]) {
cell.textLabel.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:52/255.0 alpha:1];
ribbonView.hidden = NO;
}
else {
cell.textLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
ribbonView.hidden = YES;
}
}
Try it and tell me if it works.
Good luck.
That is definitely an issue in implementation of
tableView:cellForRowAtIndexPath:
You need to always call
dequeueReusableCellWithIdentifier:forIndexPath:
and then call your configure method
configureCell:atIndexPath:(NSIndexPath *)indexPath
EDIT:
Also, I think you also need to do [cell.contentView addSubView:...] instead of [cell addSubView:...]

Resources