Cell reuse issue in table view iphone - iphone

I know this is a repeated question,but im not getting the solution for this problem.
Im using a tableview which i want to change the color of one textlabel according to some status form webservice.
But the problem is every text label color is changing when the table view is scrolled up and down.This is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [[self.Contents objectAtIndex:indexPath.row]event];
if([[[self.Contents objectAtIndex:indexPath.row]status] isEqualToString:#"Failed"]){
cell.textLabel.textColor = [UIColor redColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
When the cell ==nil condition is commented it is working fine.But i know it is not the exact way to do this.
Can anyone please help me with this.
Thanks in advance.

If you're reusing a cell, that cell (and all of its properties) are the same as they were before.
So, your code
if([[[self.Contents objectAtIndex:indexPath.row]status] isEqualToString:#"Failed"])
{
cell.textLabel.textColor = [UIColor redColor];
}
will set the text red in that condition. If the condition is false, then the text colour is not changed. However, if the colour was changed to red before, it is still red. Just add code to set it to the appropriate colour in the other condition.
if([[[self.Contents objectAtIndex:indexPath.row]status] isEqualToString:#"Failed"])
{
cell.textLabel.textColor = [UIColor redColor];
}
else
{
cell.textLabel.textColor = [UIColor blackColor];
}

Related

UILabels within a UITableView cell are overlaying each other in iOS7

I didn't have this problem with iOS6, but am currently having it with iOS7. I have a UITableView and you can see 8 cells at the time the view is loaded. Each populated with different names from and array. If you scroll down, the next two cells look good, but everything past that gets text laid on top of it; That text being the contents of what was in the previous cells. So the 10th cell will have what was in the first cell, as well as what is supposed to be in the 10th cell laid on top of it.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create Label for Name inside cell
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 5.0, 300.0, 30.0 )];
[name setText:[self.entriesArray objectAtIndex:indexPath.row]];
//Check to see if current person is a Parent or Child
NSString *class = [self.database getCellDataWithMembership:[self.MembershipArray objectAtIndex:indexPath.row] andColIndex:4];
if([class isEqualToString:#"CHILD"])
{
name.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f];
name.font = [UIFont fontWithName:#"Helvetica-Bold" size:17.0];
}
[cell.contentView addSubview:name];
return cell;
}
My skill with Table views is makeshift at best. I've been reading lots of documentation and researching solutions, but was not able to come up with a solution. I just find it odd that it works perfect for iOS6, but not for iOS7.
So it fetches a person's name from an array and I want to populate the cells with those names. I was able to originally accomplish this using:
cell.textLabel.text = [self.entriesArray objectAtIndex:indexPath.row];
if([class isEqualToString:#"CHILD"])
{
cell.textLabel.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:17.0];
If I use that instead of the "name" UILabel actions from the first code block, then it displays the names perfectly with no text overlay, but what becomes an issue is the text color. If they are labeled as a CHILD then they should be green text and bold. However, after scrolling down, every person becomes green when they shouldn't be.
Sorry for the lengthy question. I've been working on this and racking my brain around it and I just can't seem to figure it out. Any help would be greatly appreciated.
I'm dynamically adding UI objects to cells. As "caglar" notes, adding and then removing is probs not the best-practice. But, I am doing this also. The way I get around adding loads of UI objects to the cell, each time it's displayed, is by removing all my subviews first. The willDisplayCell delegate then adds them back. Clearly, if you want to remove only certain views, you'll have to tag the view and be more selective with your removal. But you can remove all with the following.
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
}
Just ensure you're adding / removing your labels to the cell's contentView. In iOS7, you'll remove that too, if you're not careful.
Hope it helps you out.
In your code, labels are added to cell whenever cellForRowAtIndexPath: method is called. I mean you add labels many times. You can remove the label which was added before.
//If label was added with tag = 500, remove it
for (UIView *sv in cell.contentView.subviews)
{
if (sv.tag == 500)
{
[sv removeFromSuperview];
}
}
//Create Label for Name inside cell
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 5.0, 300.0, 30.0 )];
[name setText:[self.entriesArray objectAtIndex:indexPath.row]];
name.tag = 500;
However, this solution is not a good solution. I think creating a custom UITableViewCell is the right thing to do.
You can try this method, when the cell is nil, you need to creare the UILabel* name, then you set the name label with a tag name.tag = 1000, then you can access this label with this method UILabel* name = (UILabel*)[cell.contentView viewWithTag:1000];.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//Create Label for Name inside cell
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 5.0, 300.0, 30.0 )];
name.tag = 1000;
[cell.contentView addSubview:name];
}
UILabel* name = (UILabel*)[cell.contentView viewWithTag:1000];
[name setText:[self.entriesArray objectAtIndex:indexPath.row]];
//Check to see if current person is a Parent or Child
NSString *class = [self.database getCellDataWithMembership:[self.MembershipArray objectAtIndex:indexPath.row] andColIndex:4];
if([class isEqualToString:#"CHILD"])
{
name.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f];
name.font = [UIFont fontWithName:#"Helvetica-Bold" size:17.0];
}
return cell;
}
You can set uilabels by using ios6/7 delta shown is size inspector .firstly set your total view for ios7 by changing values x,y,width,height and then change value in ios6/7 delta to make it for ios6 .Hope you get
you may change the this code.
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; -> UITableViewCell *cell = nil;

How to Create a Table like the below

I have created a TableView(Grouped Table View). And I can list the items in the table view. But I want to know how the below table is displayed
Like Calender is in the left side, and Gregorian in the Right Side ?? How that Gregorian is displayed in the Right Side ?? Is it a Custom TableView, Can anyone help me how to achieve that ?
Those are just regular cells with style = UITableViewCellStyleValue1 and also accessoryType = UITableViewCellAccessoryDisclosureIndicator.
To make one programmatically:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"YourIdentifier"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
(Note that in real life you would probably try to dequeueReusableCellWithIdentifier:, and only create a new cell if that returns nil.)
Or, if you're working with IB, set "Style" to "Right Detail" and "Accessory" to "Disclosure Indicator".
You need to use following code helping to you
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
UIImageView *v = [[[UIImageView alloc] init]autorelease];
v.backgroundColor = [UIColor clearColor]; // put clear color
[v setClipsToBounds:YES];
cell.selectedBackgroundView = v;
[cell setClipsToBounds:YES];
}
UIImageView *imgVBG=(UIImageView*)[cell selectedBackgroundView];
if(indexPath.row==0){
imgVBG.image=[UIImage imageNamed:#"TopImg.png"];
} else if((indexPath.section==0 && indexPath.row==4)||(indexPath.section==1 && indexPath.row==7)){
imgVBG.image=[UIImage imageNamed:#"BottomImg.png"];
} else {
imgVBG.image=[UIImage imageNamed:#"MiddleImg.png"];
}
}
Here is your answer UITableViewCell with UITableViewCellStyleValue1, adding new line to detailTextLabel at cell at bottom

change the color of the cell when it is tapped in UITableView

I am using UITableView in my application and I have created custom cell in the file DealsCC.xib and when cell is tapped the color of the cell should be changed to blue but it does not happen in my code. I write the code as follows:
static NSString *MyIdentifier = #"dealsCC";
dealsCC *cell = (dealsCC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
[cell selectionStyle:UITableViewCellSelectionStyleBlue];
I want to mention that in the line
[cell selectionStyle:UITableViewCellSelectionStyleBlue];
warning msg exists and it is "dealsCC may not respond to -selectionStyle"
plz help me to solve this problem
thanx in advance.
please see the following stackoverflow links,please check whether you have followed correclty them
1)link1
2)link2
3)enter link description here
Try this in your custom class for the table view cell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
self.selectionStyle = UITableViewCellSelectionStyleBlue;
[super setSelected:selected animated:animated];
}
The warning is because the method should be
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
and not [cell selectionStyle:UITableViewCellSelectionStyleBlue];
Use cell.selectionStyle = UITableViewCellSelectionStyleGray; for changing the style of the cell selection. But here the selectionStyle only takes input of UITableViewCellSelectionStyle type.
For changing Color you need to use Image. Use selectedBackgroundView.image property and Follow the tutorial Link
Or you can also try
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
EDIT:
OK I am updating code in case you have any other controls on the cell, then you can try below code.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"myCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIView *v = [[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor redColor]; // any color of your choice.
cell.selectedBackgroundView = v;
// After this line add all other controlls you are adding...
}
// Set up the cell...
cell.textLabel.text = #"foo";
return cell;
}

How to Change the color of the text present in table view from its default black color in an iphone application?

How to Change the color of the text present in table view from its default black color in an iphone application ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.textColor = [UIColor yellowColor]; //Change the text color
}
// Configure the cell...
return cell;
}
Try this.
cell.textLabel.textColor=[UIColor blueColor];
You can also give RGB values like
cell.textLabel.textColor = [UIColor colorWithRed:200/256.0 green:0/256.0 blue:67/256.0 alpha:1.0];
All the best.

how to set the background color for single cell in the table view on iphone

I'm having one Table view. In that i want to set the color for the First cell alone..Others should be in white...How can i do that
I need your help....
Thanks in advance.....
In cellForRowAtIndexPath, check if the indexPath.row == 0, then set the custom background. Otherwise set the default background.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
if (indexPath.row == 0) {
//set custom background color
} else {
//set default background color
}
return cell;
}
You should do the color change in the "willDisplayCell" delegate method and not at its creation in "cellForRowAtIndexPath".
Otherwise it may not stick or may display bad when adding, for example, accessories to the cell.
Look at this post for more details : Setting background color of a table view cell on iPhone
if(indexPath.row == 0)
{
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1];
cell.backgroundView = bg;
[bg release];
}