custom tableview and highlightedTextColor - cocoa-touch

i have a table view, with custom cell, i have set the cell for have a highlighted color on text when you tap on it.
//cell specific
NSString *ligneTableau = [NSString stringWithFormat:#"%#", [[table objectAtIndex:indexPath.row] nome]];
cell.label.text = ligneTableau;
cell.label.font = [UIFont fontWithName:#"populaire" size:35];
cell.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
cell.fondo.image = [UIImage imageNamed: #"cell_ant.png"];
//highlighted Text
cell.label.highlightedTextColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
every things work fine, but when come back to the table the text stay highlighted.
I forget somethings?

Probably the cell remains selected when you go back to table. So as soon as come back to table you should set deselected.
[cell setSelected:NO];

In your UpdatesTableViewCell class, you can implement the following methods:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted)
self.label.textColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
else
self.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected)
self.label.textColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
else
self.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
}

Related

How to set a tableview cell's border and color when selected

I have the following code that sets sets a red border around a custom table view cell with white in the middle when it is selected.
- (void)awakeFromNib
{
self.nameLabel.highlightedTextColor = [UIColor whiteColor];
// Set selected background view
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor redColor] CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// Set the content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:frame];
[self addSubview:myImageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
}
I want the opposite: how would you create a cell that has red with a white border / padding?
You can use
[cell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[cell.contentView.layer setBorderWidth:2.0f];
Hope this will help you
try this in custom cell there is a method - (void)setSelected:(BOOL)selected animated:(BOOL)animated use this to change the state of selected and deselect state for example
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
self.contentView.layer.cornerRadius = 10.0f;
self.contentView.layer.borderWidth = 5.0f;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = [UIColor redColor];
self.contentView.layer.borderColor = [UIColor whiteColor].CGColor;
}
else
{
self.contentView.layer.cornerRadius = 10.0f;
self.contentView.layer.borderWidth = 5.0f;
self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.layer.borderColor = [UIColor redColor].CGColor;
}
// Configure the view for the selected state
}
and also set customCell.selectionStyle = UITableViewCellSelectionStyleNone; for cell while creating the cell
why don't you try doing just the opposite such as this
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor whiteColor] CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// Set the content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:frame];
myImageView.backgroundcolor=[UIColor redColor];
[self addSubview:myImageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];

Edit UITableViewCell property on scrolling

I am making an app in which there are quotes from famous people in an array which are being read out by openears and a tableView which shows all the quotes. The currently reading quote is to be displayed in the tableview in a different color than the other quotes. I tried with the following function in cellForRowAtIndexPath but the color of the cell remains the same unless the user scrolls it out and then come back to the view. But I need to set the color automatically and at the same time all other cells should revert back to the initial(white) color. Is there any method which can achieve this? I don't want to reload the table each time to achieve this.
if ([[arrayOfQuoteIds objectAtIndex:indexPath.row]intValue] == currentQuoteId) {
cell.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.contentView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.textLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.detailTextLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.accessoryView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
}
else {
cell.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = [UIColor whiteColor];
cell.accessoryView.backgroundColor = [UIColor whiteColor];
}
I can be wrong but how You want to achieve change of cell appearence without redrawing cell?
If You don't want reload all table try to reload just changed rows
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

UITableViewCell with alternate background color in customized cells

I'd like the background to of my UITableViewCells to have a different color every two cells displayed, but when I scroll down and back, they all get the same color. How can I get this effect knowing that my cells have different contentView size (according to their content) ?
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 20.0f
#define NAME_CELL_HEIGHT 20.0f
#import "CartCell.h"
#implementation CartCell
#synthesize nameLabel = _nameLabel;
#synthesize ingredientsLabel = _ingredientsLabel;
#synthesize myStore;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
myStore = [Store sharedStore];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.nameLabel = nil;
self.ingredientsLabel = nil;
// SET "NAME" CELL
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.nameLabel setLineBreakMode:UILineBreakModeWordWrap];
[self.nameLabel setMinimumFontSize:FONT_SIZE];
[self.nameLabel setNumberOfLines:1];
[self.nameLabel setTag:1];
self.nameLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
[self.nameLabel sizeToFit];
self.nameLabel.backgroundColor = [UIColor clearColor];
[[self contentView] addSubview:self.nameLabel];
// SET "INGREDIENTS" CELL
self.ingredientsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.ingredientsLabel setLineBreakMode:UILineBreakModeWordWrap];
[self.ingredientsLabel setMinimumFontSize:FONT_SIZE];
[self.ingredientsLabel setNumberOfLines:0];
[self.ingredientsLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[self.ingredientsLabel setTag:2];
self.ingredientsLabel.backgroundColor = [UIColor clearColor];
[[self contentView] addSubview:self.ingredientsLabel];
if (myStore.cellBackgroundShouldBeLight == YES) {
NSLog(#"clear [in] ? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
self.contentView.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1];
myStore.cellBackgroundShouldBeLight = NO;
} else {
NSLog(#"clear [in] ? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
self.contentView.backgroundColor = [[UIColor alloc]initWithRed:187.0/255.0 green:268.0/255.0 blue:229.0/255.0 alpha:1];
myStore.cellBackgroundShouldBeLight = YES;
}
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
UPDATE:
I'm know trying to set it in cellForRowAtIndexPath as it was suggested, but I get the same result: scrolling down worked fine the first time, but then scrolling up again messed up the cells background color.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CartCell";
CartCell *cell = (CartCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Recipes *info = [_fetchedResultsController objectAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[CartCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// if (!cell.nameLabel) {
// cell.nameLabel = (UILabel*)[cell viewWithTag:1];
// // cell.nameLabel = (UILabel*)[cell viewWithTag:1];
// }
// if (!cell.ingredientsLabel)
// cell.ingredientsLabel = (UILabel*)[cell viewWithTag:2];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [info.ingredients sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[cell.nameLabel setFrame:CGRectMake(10, 10, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), NAME_CELL_HEIGHT)];
[cell.ingredientsLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN + NAME_CELL_HEIGHT, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
// SETTING TEXT CONTENT
cell.nameLabel.text = info.name;
cell.ingredientsLabel.text = info.ingredients;
// SETTING BACKGROUND COLOR
// UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
// [lab setBackgroundColor:[UIColor blueColor]];
if (myStore.cellBackgroundShouldBeLight == YES) {
NSLog(#"clear? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
cell.contentView.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:84.0/255.0 blue:229.0/255.0 alpha:1];
// cell.backgroundView = lab;
// ingredientsLabel.backgroundColor = [UIColor clearColor];
// nameLabel.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1];
// [cell setBackgroundColor: [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1]];
// [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
myStore.cellBackgroundShouldBeLight = NO;
} else {
// cell.contentView.tag = 2;
NSLog(#"clear? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
cell.contentView.backgroundColor = [[UIColor alloc]initWithRed:187.0/255.0 green:184.0/255.0 blue:229.0/255.0 alpha:1];
myStore.cellBackgroundShouldBeLight = YES;
}
return cell;
}
It is very simple, the indexPath tells you everything you need to know. If the indexPath.row is even then use one color. If the indexPath.row is odd use a different color.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
…
// SETTING BACKGROUND COLOR
// UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
// [lab setBackgroundColor:[UIColor blueColor]];
if (indexPath.row % 2) {
cell.contentView.backgroundColor = [[[UIColor alloc]initWithRed:87.0/255.0 green:84.0/255.0 blue:229.0/255.0 alpha:1] autorelease];
} else {
cell.contentView.backgroundColor = [[[UIColor alloc]initWithRed:187.0/255.0 green:184.0/255.0 blue:229.0/255.0 alpha:1] autorelease];
}
…
return cell;
}
Your method is having problems because blindly assuming cells will be asked for in alternating pairs is a bad assumption. The tableView could ask for cells in any order is chooses. In your example, I believe cells could be asked for as follows. First, 0, 1,…, 9 are asked for. Next, you scroll down and 10, 11, and 12 are fetched. At this point, 0, 1, and 2 have gone off the screen. You scroll back up and 2 is asked for, but oh no, your model is on an odd number alternation, so you get the wrong color.
Use the -willDisplayCell method.
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.row %2) { //change the "%2" depending on how many cells you want alternating.
UIColor *altCellColor = [UIColor colorWithRed:255/255.0 green:237/255.0 blue:227/255.0 alpha:1.0]; //this can be changed, at the moment it sets the background color to red.
cell.backgroundColor = altCellColor;
}
else if (indexPath.row %2) {
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0]; //this can be changed, at the moment it sets the background color to white.
cell.backgroundColor = altCellColor2;
}
}
The appropriate place to change your cell's background color would be the "cellForRowAtIndexPath:" method, where the cells data gets filled out and returned to the table view.
One way to do this would be: When the data goes into the cell, change the background color depending on what row you're on.
Put the color on the cellForRowAtIndexPath: don't set on custom cell.
Take a look what I use to customize my table
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#if USE_CUSTOM_DRAWING
const NSInteger TOP_LABEL_TAG = 1001;
const NSInteger BOTTOM_LABEL_TAG = 1002;
UILabel *topLabel;
UILabel *bottomLabel;
#endif
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//
// Create the cell.
//
cell =
[[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
autorelease];
#if USE_CUSTOM_DRAWING
UIImage *indicatorImage = [UIImage imageNamed:#"indicator.png"];
cell.accessoryView =
[[[UIImageView alloc]
initWithImage:indicatorImage]
autorelease];
const CGFloat LABEL_HEIGHT = 20;
UIImage *image = [UIImage imageNamed:#"imageA.png"];
//
// Create the label for the top row of text
//
topLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT),
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:topLabel];
//
// Configure the properties for the text that are the same on every row
//
topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor colorWithRed:0.25 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]];
//
// Create the label for the top row of text
//
bottomLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:bottomLabel];
//
// Configure the properties for the text that are the same on every row
//
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];
//
// Create a background image view.
//
cell.backgroundView =
[[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView =
[[[UIImageView alloc] init] autorelease];
#endif
}
#if USE_CUSTOM_DRAWING
else
{
for (UIView *sub in [cell.contentView subviews]) {
// if([sub class] == [UITableViewCellContentView class])
NSLog(#"this is uilabel %#",[sub class]);
}
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
}
topLabel.text = [NSString stringWithFormat:#"Cell at row %ld.", [indexPath row]];
bottomLabel.text = [NSString stringWithFormat:#"Some other information.", [indexPath row]];
//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [aTableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:#"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
rowBackground = [UIImage imageNamed:#"topRow.png"];
selectionBackground = [UIImage imageNamed:#"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:#"bottomRow.png"];
selectionBackground = [UIImage imageNamed:#"bottomRowSelected.png"];
}
else
{
rowBackground = [UIImage imageNamed:#"middleRow.png"];
selectionBackground = [UIImage imageNamed:#"middleRowSelected.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
// cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:rowBackground];
// cell.selectedBackgroundView.backgroundColor = [UIColor colorWithPatternImage:selectionBackground];
//
// Here I set an image based on the row. This is just to have something
// colorful to show on each row.
//
if ((row % 3) == 0)
{
cell.imageView.image = [UIImage imageNamed:#"imageA.png"];
}
else if ((row % 3) == 1)
{
cell.imageView.image = [UIImage imageNamed:#"imageB.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:#"imageC.png"];
}
#else
cell.text = [NSString stringWithFormat:#"Cell at row %ld.", [indexPath row]];
#endif
return cell;
}
past it after all #import lines
#define USE_CUSTOM_DRAWING 1
Heading ##Simplest way of changing alternate colors
if(indexPath.row%2) {
cell.backgroundColor=[UIColor nameUrColor] //brownColor, yellowColor, blueColor
} else {
cell.backgroundColor=[UIColor nameAnotherColor]
}
if(cell.contentView)
{
[cell.nameLbl setFont:[UIFont systemFontOfSize:24]];
int red_value = arc4random() % 210;
int green_value = arc4random() % 210;
int blue_value = arc4random() % 210;
cell.contentView.backgroundColor = [UIColor colorWithRed:red_value/255.0 green:green_value/255.0 blue:blue_value/255.0 alpha:0.6];
}

Changing elements in selected UITableViewCell subclass

I have implemented a custom UITableViewCell for my application and have created a custom background view and selectedbackgroundview, both of which work great. However, I have several other images and uilabels on the cell that I want to change the color's of when it is selected. I have overwritten the following method and it logs correctly when the cell is selected but it does not change the image or the text color.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
NSLog(#"setSelected:YES");
separatorImage.image = [UIImage imageNamed:#"SeparatorSelected"];
titleLabel.textColor = [UIColor whiteColor];
} else {
NSLog(#"setSelected:NO");
separatorImage.image = [UIImage imageNamed:#"Separator"];
titleLabel.textColor = [UIColor grayColor];
}
}
Any Idea what I am doing wrong?
Update
separatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Separator"]]];
[separatorImage setFrame:CGRectMake(99, 4, 5, 71)];
separatorImage.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:separatorImage];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(111, 4, 188, 26)];
titleLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:21.0];
titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:titleLabel];
I have a hunch that sending -setSelected:animated: to super at the beginning of the method is preventing the changes to your cell. Try moving the call to super to the end of the method. You should also override -setHighlighted:animated: or your selection will appear to lag.

Loading data issue in UITableview

I created an application using UITableView. I parsing the data from URL and listed in UITableView.
But in UITableView, the first the data only displayed again and again.
I don't know why this problem occurred.
My code is as below:
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
CGRect frame = CGRectMake(10.0f, 5.0, 300.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:16.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
valueField.text=[title1 objectAtIndex:count1];
[cell.contentView addSubview:valueField];
UIImage *favOn = [UIImage imageNamed:#""];
UIImage *favOff = [UIImage imageNamed:#""];
titlebutton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 15.0, 300.0, 55)];
[titlebutton setTag:indexPath.row];
//titlebutton.tag = 111;
[titlebutton setImage:favOff forState:UIControlStateNormal];
[titlebutton setImage:favOn forState:UIControlStateSelected];
if([self getFavState:#"111"])
{
[titlebutton setSelected:YES];
}
else
{
[titlebutton setSelected:NO];
}
[titlebutton addTarget:self action:#selector(favButtonSwitch:) forControlEvents:UIControlEventTouchUpInside];
[titlebutton setBackgroundColor:[UIColor clearColor]];
[titlebutton setTitle:#"" forState:UIControlStateNormal];
[cell.contentView addSubview:titlebutton];
CGSize labelSize = [valueField.text sizeWithFont:valueField.font constrainedToSize:valueField.frame.size lineBreakMode:UILineBreakModeWordWrap];
int labelHeight = labelSize.height;
NSLog(#"labelHeight = %d", labelHeight);
frame = CGRectMake(10.0f, 40.0, 300.0, 55);
pubField = [[UILabel alloc] initWithFrame:frame];
[pubField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
pubField.tag = 111;
pubField.font=[UIFont fontWithName:#"Helvetica" size:12.0];
pubField.textAlignment = UITextAlignmentLeft;
pubField.lineBreakMode=UILineBreakModeWordWrap;
pubField.numberOfLines = 0;
pubField.textColor = [UIColor whiteColor];
pubField.highlightedTextColor = [UIColor whiteColor];
pubField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
pubField.adjustsFontSizeToFitWidth = YES;
pubField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
pubField.text=[pubDate objectAtIndex:indexPath.row];
[cell.contentView addSubview:pubField];
frame = CGRectMake(5.0, 70.0, 300.0, 55);
desField = [[UILabel alloc] initWithFrame:frame];
[desField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
desField.tag = 111;
desField.font=[UIFont fontWithName:#"Helvetica" size:13.0];
desField.textAlignment = UITextAlignmentLeft;
desField.lineBreakMode=UILineBreakModeWordWrap;
desField.numberOfLines = 0;
desField.textColor = [UIColor whiteColor];
desField.highlightedTextColor = [UIColor whiteColor];
desField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
desField.adjustsFontSizeToFitWidth = YES;
desField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
desField.text=[description objectAtIndex:indexPath.row];
[cell.contentView addSubview:desField];
}
UIImage *patternImage = [UIImage imageNamed:#"bg.jpg"];
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor colorWithPatternImage: patternImage];
cell.backgroundView = backView;
NSString *image1 =[images objectAtIndex:indexPath.row];
NSLog(#"Images ..... = %#",image1);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:indexPath.row]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: myimage ];
imageView.frame = CGRectMake(10.0f, 110.0f, 120.0f, 120.0f);
[cell.contentView addSubview:imageView];
return cell;
}
do all the alloc/init and all things, that will be the same in any cell in the
if(cell==nil){
//<-- here
}
while setting text and any thing that changes by the specific data outside the block
if(cell==nil){
}
//<-- here
i.e:
if(cell==nil){
//....
valueField.tag = 111;
valueField.font = [UIFont fontWithName:# "Helvetica" size:16.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode = UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[cell.contentView addSubview : valueField];
//....
}
//....
valueField.text = [title1 objectAtIndex:count1];
//....
the first cell data is getting displayed in all the cells because when you initialize and create your reusable cell, you fill the data there, you need to initialize a reusable cell that doesnt have all its values set, and then set this information for each cell. That is, you need to have all the information you want for each particular cell outside the if(cell==nil) part of this code, and rely on the indexPath to know which cell you are in, and feed in different data based on this.
You seem to be constructing the subviews of the cell correctly when there is no reusable cell available. The question is, what does your code do when you do dequeue a reusable cell? It appears that you do nothing. You should move any row-specific initialization (setting label text values, images, etc.) outside the check for whether the dequeued cell is nil. In this way, whether you just constructed a new cell or dequeued a previously-created cell, the data being displayed correctly correlates to the row being displayed.
Well actually your code this way is quite hard to decode,
but first of all, you declare a lot of tag that should be used to retrieve the UIViews you're declaring.
Unfortunately these are all the same (tags) so you might never get what you want. You should maybe put in some constant in your header file like:
#define kPubLabel 100
then use them.
then you usually retrieve these after declaring your cell to populate them.
In the mean time, retrieving your images from url in this method is one of the best way to have your application being slow, since each time you scroll, you'll have to re-fetch your image from where it come from causing your table view to "freeze".
Finally, the only thing that might change, according to your code is the imageView that you add to your frame. You usually have an ImageView in which you put an image that you might have stored/cached but rarely change the contentView of a cell.
ie
Maybe you should externalize your cell building code in a class overriding UITableViewCell.
Put different tags on every element in your cell to retrieve them in your method.
Retrieve your elements in your method according to there tags.
Populate and only populate them in your method.
Retrieve your image once and for all for performance improval.
I'm not sure this will help, but in my point of view, there are actually a lot work to do in your code.