UITableViewCell separator line disappears on scroll - ios

I'm trying to have a separator cell with a custom image. I did try something like that:
In my cellForRowAtIndexPath:
NSString *cellIdentifier = [NSString stringWithFormat:#"identifier"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:19];
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor colorWithRed:128/255.0f green:129/255.0f blue:132/255.0f alpha:1.0f];
cell.backgroundColor = [UIColor whiteColor];
UIImageView *imagView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"reaL.png"]];
imagView.frame = CGRectMake(0, cellHeight, cellWidth, 1);
[cell.contentView addSubview:imagView];
switch (indexPath.row) {
case 0:
cell.imageView.image = [self imageWithImage:[UIImage imageNamed:#"img1.png"] scaledToSize:CGSizeMake(27, 27)];
cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:#"route.png"] scaledToSize:CGSizeMake(27, 27)];
break;
case 1:
cell.imageView.image = [self imageWithImage:[UIImage imageNamed:#"img.png"] scaledToSize:CGSizeMake(27, 27)];
cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:#"money.png"] scaledToSize:CGSizeMake(27, 27)];
break;
case 2:
cell.imageView.image = [self imageWithImage:[UIImage imageNamed:#"auto.png"] scaledToSize:CGSizeMake(27, 27)];
cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:#"cars.png"] scaledToSize:CGSizeMake(27, 27)];
break;
case 3:
cell.imageView.image = [self imageWithImage:[UIImage imageNamed:#"impostazioni.png"] scaledToSize:CGSizeMake(27, 27)];
cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:#"impostazioni.png"] scaledToSize:CGSizeMake(27, 27)];
// cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
break;
case 4:
cell.imageView.image = [self imageWithImage:[UIImage imageNamed:#"info.png"] scaledToSize:CGSizeMake(27, 27)];
cell.imageView.highlightedImage = [self imageWithImage:[UIImage imageNamed:#"info.png"] scaledToSize:CGSizeMake(27, 27)];
break;
default:
break;
}
return cell;
When I lunch the app everything is good, but when I scroll the the table, or when I select a cell the separator lines disappear. How I can have a permanent custom line separator?

You're not creating cells properly in the first place. Here's a few tips to try that might help with the issue you're running into.
First off, since ios6, Apple now allows us to pre-register the classes we're using to dequeue cells so there is no more deed to do the !cell check. In your view did load, you'll want to do something akin to:
[self.tableView registerClass:[CustomTableViewCellClass class] forCellReuseIdentifier:customIdentifier];
This will allow you to dequeue the cell properly every time, as in:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customIdentifier];
Next, and this is where I think you're running into issues. You're creating an image view and adding it to the cell every time the cell is dequeued.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"reaL.png"]];
imageView.frame = CGRectMake(0, cellHeight, cellWidth, 1);
[cell.contentView addSubview:imageView];
You only need to do this the first time the cell is created otherwise you're going to be adding constantly UIImageViews to the top of the cell every time a cell is re-used. A better pattern for this situations would be to have a UIImageView* reference on the custom cell itself. When the cell is dequeue, set the image and frame then. This way the UIImageView is only created once and added to the cell once.
Best of luck!

Related

Replacing button images shows weird issue

I have two images I used for showing on/off. Everything was working perfectly. When my designer asked to replace the images, I replaced the images and replaced the name of the images in the class. Suddenly the switching isn't happening any more. I used [button setImage:image forState:UIControlStateNormal]; With condition. My condition is working perfectly. But image never changes. Replacing these images with the old images worked perfectly for the same code.
Now, I again replaced it back it to the new images and added setBackgroundColor for Button. Strangely the images are getting replaced. If I take away the backgroundColor method it doesn't work. Does anyone knows why this happens?? I think the image is getting set as background image as well. But I haven't used a single code anywhere to set the background image.
Update
This is the place I set the images. Where in viewDidLoad
selectedImage = [UIImage imageNamed:kcheckSelected];
notSelectedImage = [UIImage imageNamed:kcheckUnselect];
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cellIdentifier";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(15, 0, tableview.frame.size.width - 15, 1)];
view.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview:view];
}
NSString *sltd= [selectionArray objectAtIndex:indexPath.row];
UIImage *image = nil;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(tableview.frame.size.width - 60, 5, 60, 50)];
if ([sltd isEqualToString:#"yes"])
{
image = selectedImage;
}
else
{
image = notSelectedImage;
}
[button setBackgroundColor:[UIColor whiteColor]];
button.tag = indexPath.row;
[button addTarget:self action:#selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[cell.contentView bringSubviewToFront:button];
User *userObj = [userArray objectAtIndex:indexPath.row];
cell.textLabel.text = userObj.name;
cell.textLabel.font = [UIFont fontWithName:#"Montserrat-Regular" size:17];
if ([userObj.installOnIpad boolValue] == YES)
{
cell.imageView.image = [UIImage imageNamed:#"ipad-icon-active"];
}
else
{
cell.imageView.image = [UIImage imageNamed:#"ipad-icon-disabled"];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}

Tableview is not displaying any cells or rows content in new pushed viewController

I'm not sure what I'm missing but I'm trying to display a new viewController and passing some data to it, but the problem is when this new viewController gets pushed it doesn't display the cells or rows content like labels, textfields and so on..
This is how I'm pushing from one controller to another..
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BillComparatorViewController *vc = [[BillComparatorViewController alloc] init];
vc.selectedInstitution = [self.myInstitution objectAtIndex:indexPath.row];
vc.managedObjectContext = self.managedObjectContext;
[self.view endEditing:YES];
[self.navigationController pushViewController:vc animated:YES];
}
Then in my new pushed view controller this is my cellForRowAtIndexPath function..
//UITableView Delegate + Datasource
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
for (UIView *view in cell.contentView.subviews)
[view removeFromSuperview];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
CGFloat i = [[self.rowHeights objectAtIndex:indexPath.row] floatValue];
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 280, 15)];
cellLabel.font = [UIFont fontWithName:kFONTNAME size:kSystemFontSizeRows];
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UIButton class]]){
cell.textLabel.font = [UIFont fontWithName:kFONTNAME size:kSystemFontSizeRows];
cell.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
[cell.contentView addSubview:arrow];
}
else if (i > kTableRowHeight)
{
cell.textLabel.text = nil;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kElementWidth/2, 75)];
lbl.text = [self.labels objectAtIndex:indexPath.row];
lbl.font = _myFont;
lbl.numberOfLines = 0;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
[cell.contentView addSubview:lbl];
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UISwitch class]])
{
[cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
}
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UITextField class]])
{
// Can make a class for this cell's contentview, however it never repeats in code
UITextField* txtField = [self.tableViewElements objectAtIndex:indexPath.row];
txtField.textColor = kBlueColor;
txtField.textAlignment = NSTextAlignmentLeft;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // iOS 6 support
NSString *imageName = #"image";
NSString *arrowName = #"image1";
if (selectedRow == indexPath.row)
{
imageName = #"image";
arrowName = #"image1";
txtField.textColor = [UIColor whiteColor];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2*kElementWidth/3 -12, 23, 135, 27)];
imgView.image = [UIImage imageNamed:imageName];
[cell.contentView addSubview:imgView];
UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(kElementWidth+10, 31, 8.5, 10)];
arrowView.image = [UIImage imageNamed:arrowName];
[cell.contentView addSubview:arrowView];
[cell.contentView addSubview:txtField];
}
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
cell.textLabel.font = _myFontSmall;
cell.textLabel.textColor = kBlueColor;
cell.textLabel.backgroundColor = [UIColor clearColor];
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UITextField class]])
{
// Can make a class for this cell's contentview, however it never repeats in code
UITextField* txtField = [self.tableViewElements objectAtIndex:indexPath.row];
txtField.textColor = kBlueColor;
txtField.textAlignment = NSTextAlignmentLeft;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // iOS 6 support
NSString *imageName = #"image";
NSString *arrowName = #"image1";
if (selectedRow == indexPath.row)
{
imageName = #"image";
arrowName = #"image2";
txtField.textColor = [UIColor whiteColor];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2*kElementWidth/3 -12, 12, 135, 27)];
imgView.image = [UIImage imageNamed:imageName];
[cell.contentView addSubview:imgView];
UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(kElementWidth+10, 20, 8.5, 10)];
arrowView.image = [UIImage imageNamed:arrowName];
[cell.contentView addSubview:arrowView];
[cell.contentView addSubview:txtField];
}
if (isSearching)
{
cellLabel.text = [searchElements objectAtIndex:indexPath.row];
[cell.contentView addSubview:cellLabel];
}
}
return cell;
}
It's a bit long sorry but I think it was important to post it..
UPDATE
This is how the problem happens, when user select a row within the current viewcontroller didSelectRowAtIndexPath calls a new viewcontroller and pushes this controller but if the user does this while searching with the keyboard in the screen is when the problem happens that the new viewcontroller pushed doenst show any content in its tableview..
SOLVED
Within my cellForRowAtIndexPath
[cell setTranslatesAutoresizingMaskIntoConstraints:NO];
The issue is with Autolayout. I had the same problem with iOS 8 beta. (I know you're not using i OS 8 but still).
Try turning it off. Should sort the issue out.
i think the problem is initiating the object of BillComparatorViewController.
You do like this .You may get solution
BillComparatorViewController *vc = [[BillComparatorViewController alloc] initWithNibName:"BillComparatorViewController" Bundle:nil];

Segmentcontrol in tableviewcell

I got this tableview with segmented controls in each cell. I get some data from my database that i put into the cells segmented control. But how to set no segment chosen? If noting is found in the database it returns -1. I tried the: segmentedControl.selectedSegmentIndex = -1;, but with no luck. It looks like it set the segmentedControl.selectedSegmentIndex = 0;, be course segment with index 0 is selected no matter what (even if i get a result or not). See screenshots to understand :)
Btw I can select as many segments as I want in each segmented control..
Why is this possible? Will the other problem fix this? :)
THANK YOU!
My code for creating the cells is:
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"StateCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseI dentifier:#"StateCell"];
}
UIImage *correctImageGreen = [[UIImage imageNamed:#"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageGul = [[UIImage imageNamed:#"gul.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageRed = [[UIImage imageNamed:#"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSArray *itemArray = [NSArray arrayWithObjects: correctImageGreen, correctImageGul, correctImageRed, nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(308, 7, 150, 28);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
int newIndexPath = indexPath.row + 1;
NSString *indexPathKey = [NSString stringWithFormat:#"%i", newIndexPath];
NSString *selectedState = [statefieldData valueForKey:[NSString stringWithFormat:#"StateField%#", indexPathKey]];
int selectedIndexInt = [selectedState intValue];
if (selectedIndexInt == -1) {
segmentedControl.selectedSegmentIndex = -1;
}
else {
segmentedControl.selectedSegmentIndex = selectedIndexInt;
}
[cell.contentView addSubview:segmentedControl];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self.tableView setBackgroundColor:[UIColor colorWithRed:37 green:37 blue:37 alpha:0]];
cell.backgroundColor = [UIColor clearColor];
return cell;
[[self tableView] reloadData];
The documentation states:
selectedSegmentIndex
The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Set this property to -1 to turn off the current selection.
If you are setting -1 (UISegmentedControlNoSegment), and it is not deselecting the segment, then there is something else you are doing wrong that we cannot see.

Adding image in TableView Cell does not show

I am adding image in tableVIew cell but it does not show.
I am using if condition to give the image to button but it does not show.
Here is the code for the button addding
addButton = [UIButton buttonWithType:UIButtonTypeCustom];
addButton.Frame=CGRectMake(590,2,42,42);
[addButton addTarget:self action:#selector(tab1Action:) forControlEvents:UIControlEventTouchUpInside];
addButton.tag=indexPath.row;
NSString*test=theData.Status;
if ([test isEqualToString:#"1"]) {
test=#"Already Member of the group";
}
else {
test=#"";
NSLog(#"Else is also Working Fine");
[addButton setImage:[UIImage imageNamed:#"addbutton.png"] forState:UIControlStateNormal];
}
addButton.clipsToBounds=YES;
[[cell contentView] addSubview:addButton];
cell.textLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
cell.textLabel.text=[NSString stringWithFormat:#"%#: %#",theData.GroupTitle,test];
return cell;
You don't need to add to cell's contentview. So do like this..
[cell addSubview:addButton];
your button frame CGRectMake(590,2,42,42) is the error
frame.origin.x is too big than screen size
it is only on size 320,480 in developing
640,960 is automatic
// Create a table for different section of app
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
/* IMPORTANT: As we are using a "Dynamic Type", when table view reload we must remove previous
views from container.
*/
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
// create a rectangle box for image view
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, #"Unhandled value in cellForRowAtIndexPath");
break;
}
return cell;
}
Thanks VKJ,

Different images in different cells of tableViewController are not working properly

I have made a tableViewController. I want to give different images to the 1st and 7th index cell. This is a code snippet inside my cellForRowAtIndexPath delegate method. It initializes the cells properly in the beginning but when I scroll up and down several times then it also starts to give the "button4.png" in the accessory view of other cells.
UIImage *indicatorImage;
if(indexPath.row==0||indexPath.row==6)
{
indicatorImage = [UIImage imageNamed:#"button4.png"];
}
else
{
indicatorImage = [UIImage imageNamed:#"img-arrow.png"];
}
cell.accessoryView =
[[UIImageView alloc]
initWithImage:indicatorImage];
What can be the possible reason for this?
The complete code of the function is a little messy, but I am posting it here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
selection=self.tabBarController.selectedIndex;
static NSString *CellIdentifier = #"Cell";
UILabel *topLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImage *indicatorImage;
if(indexPath.row==0||indexPath.row==6)
{
indicatorImage = [UIImage imageNamed:#"button4.png"];
}
else
{
indicatorImage = [UIImage imageNamed:#"img-arrow.png"];
}
cell.accessoryView =
[[UIImageView alloc]
initWithImage:indicatorImage];
cell.accessoryView =
[[UIImageView alloc]
initWithImage:indicatorImage];
const CGFloat LABEL_HEIGHT = 20;
topLabel =
[[UILabel alloc]
initWithFrame:
CGRectMake(
2.0 * cell.indentationWidth,
0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT),
tableView.bounds.size.width -
4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)];
[cell.contentView addSubview:topLabel];
topLabel.backgroundColor = [UIColor clearColor];
topLabel.shadowColor = [UIColor whiteColor];
topLabel.shadowOffset = CGSizeMake(0, 1);
topLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
topLabel.tag=10;
cell.backgroundView =
[[UIImageView alloc] init];
cell.selectedBackgroundView =
[[UIImageView alloc] init];
}
}
else
{
topLabel = (UILabel *)[cell viewWithTag:10];
}
UIImage *rowBackground;
UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:#"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
if(indexPath.row==0||indexPath.row==6)
{
rowBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
topLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:20];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
NSString *object = _objects[indexPath.row];
NSString *str=[object description];
topLabel.text=[NSString stringWithFormat:#" %#",str];
return cell;
}
You are setting this image in your if (cell == nil) ... block in which you create and configure the cell. But you need to move this image setting outside of that if block because if the cell is being reused (ie you don't have to alloc and init), the old image will be reused. You should review anything inside that if statement for things that should change cell-by-cell, and move that outside of the if block.
When scrolling messes you up like this, it suggests that you're not completely initializing the contents of a REUSED UITableViewCell. While your code snippet says you do set it to something -- your result suggests that you aren't. You can look for that.
For instance, try setting the accessoryView to nil immediately after getting the cell and see if the problem goes away.

Resources