How to fix: initWithFrame:CellFrame reuseIdentifier:cellIdentifier deprecated - objective-c

I upgraded to XCODE 4.2 and suddenly i got all these warning signals. Most of the i am able to fix but the following I do not know how to. I have tried to read-up on it but still have problem.
The code that is deprecated is:
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
I know that i need to use the following:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
However, i get problem with the CellFrame etc. when i test.
Could someone please give me a hint how i should replace the deprecated code with the initWithStyle and get the same result?
Here is the full code:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
CGRect Label2Frame = CGRectMake(30, 33, 270, 25);
CGRect Label3Frame = CGRectMake(30, 56, 270, 25);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:16]];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 2;
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Initialize Label with tag 3.
lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 3;
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
return cell;
}

Just at first initialize with style, and after set reuired frame. I don't see any problems:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.frame = CellFrame;
}

Something along those lines
cell = [[[UITableViewCell alloc] initWithStyle:somestyle reuseIdentifier:#"cellname"] autorelease]
cell.frame = cellFrame;
Iam not sure if you really need to set the cell frame unless you want some specific frame, it should be set to whatever the tableview is.

i just use this code
cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}

Related

UITextView on cell clears when scrolling

Hello fellow programmers,
I am really cracking my head trying to understand what the problem is, but I just can't seem to figure out the problem. I have done research on this but nothing works for me unfortunately. The problem is that I have multiple cells and each cell is given a specific UITextField which I draw on the cellForRowAtIndexPath programmatically. The issues is that when I enter some information on that any Text Field and I scroll down on the table view, the text on the text field disappears into thin air. I am trying to understand why the cell reuse (which I believe is causing the problem), is causing this.
Here is my code for the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
cellText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 260, 20)];
cellText.font = [UIFont boldSystemFontOfSize:11.0];
cellText.textColor = [UIColor grayColor];
[cell.contentView addSubview:cellText];
if (indexPath.section == 0)
{
cellText.text = clientInfoArray[indexPath.row];
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Customer Name"])
{
nameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
nameTxtField.placeholder = #"full name";
nameTxtField.font = [UIFont boldSystemFontOfSize:15.0];
nameTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[nameTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:nameTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Phone"])
{
phoneTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
phoneTxtField.placeholder = #"xxx-xxx-xxx";
phoneTxtField.font = [UIFont boldSystemFontOfSize:15.0];
phoneTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
phoneTxtField.keyboardType = UIKeyboardTypePhonePad;
[phoneTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:phoneTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Date"])
{
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
dateLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:dateLabel];
dateLabel.text = [dateFormatter stringFromDate:[NSDate date]];
}
}
if (indexPath.section == 1) {
cellText.text = vehicleInfoArray[indexPath.row];
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Make and Model"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
modelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:modelLabel];
modelLabel.text = makeAndModelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Color"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
colorLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:colorLabel];
colorLabel.text = colorData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Doors"])
{
doorsTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
doorsTxtField.placeholder = #"#";
doorsTxtField.font = [UIFont boldSystemFontOfSize:15.0];
doorsTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[doorsTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
doorsTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:doorsTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Vehicle VIN"])
{
vinTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
vinTxtField.placeholder = #"#";
vinTxtField.font = [UIFont boldSystemFontOfSize:15.0];
vinTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[vinTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
vinTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:vinTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Fuel"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
fuelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
fuelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:fuelLabel];
fuelLabel.text = fuelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Milage"])
{
milageLabel = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
milageLabel.placeholder = #"ODO";
milageLabel.font = [UIFont boldSystemFontOfSize:15.0];
milageLabel.autocorrectionType = UITextAutocorrectionTypeNo;
[milageLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
milageLabel.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:milageLabel];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Other description"])
{
otherDTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, 300, 125)];
otherDTxtView.font = [UIFont boldSystemFontOfSize:15.0];
[cell addSubview:otherDTxtView];
}
}
if (indexPath.section == 2) {
cellText.text = InAndOutInfoArray[indexPath.row];
}
return cell;
}
Any ideas on how I can fix this problem? + Extra points for a helpful answer. :)
Whenever you are using reuse cell techniques, the cell that is not visible will be deallocated to free some memory.
Since you are creating a textfield in the cellForRow method, it will also be deallocated when the cell is not visible and redrawn when it becomes visible.
You could save the textfield value in some array (using textfield's didEndEditing delegate) and use that array to populate the text during textfield creation process inside the cellForRow method.

How to set UITableViewCell identifier in xcode?

I am getting a crash randomly when I select a cell in a UITableView in a viewcontroller in my project.
I think the problem is caused by the cells I am trying to reuse.
Here is the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self reinitializeStrArray];
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [strArray objectAtIndex:indexPath.row];
return cell;
}
- (void)addStrTableToSubView{
strTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 110, 280, 285)];
[strTable setDelegate:self];
[strTable setDataSource:self];
[self.view addSubview:strTable];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 83, 280, 28)] autorelease];
UIColor *myColor = [UIColor colorWithRed:(215 / 255.0) green:(145 / 255.0) blue:(0.0 / 255.0) alpha: 1];
label.backgroundColor = myColor;
label.font = [UIFont boldSystemFontOfSize:24];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = [#"strings" capitalizedString];
[self.view addSubview:label];
[strTable selectRowAtIndexPath:scrollStrIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}
I create my table in XCode. So there is no UITableViewCell object in IB which has an identifier.
Is it possible to add this in code while creating the table?
Or is the problem somewhere else?
Thanks
When you get a EXC_BAD_ACCESS, usually means that there is an incorrect memory management (i.e. an object was improperly released). My suggestion is to enable the NSZombieEnabled environment variable: it will give you more information about what is the object that causes the problem. More info here.

My UITableView do not scroll

I cannot get my UITableView to load data as i am scrolling. I do have subview in the cell (two lines). It display the first screen correctly but when i scroll only the lines that is on the first screen displays, nothing to scroll.
I have two arrays cellArray1 (main line) and cellArray2 (second line) that I load data from.
I would very much appreciate help to solve this, possible, simple problem.
Here is what i think is relevant code for this question:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier
{
CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
CGRect Label2Frame = CGRectMake(30, 33, 270, 25);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.backgroundColor = [UIColor orangeColor];
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:16]];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.backgroundColor = [UIColor orangeColor];
lblTemp.tag = 2;
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) cell = [self getCellContentView:CellIdentifier];
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
lblTemp1.text = [cellArray1 objectAtIndex:indexPath.row];
lblTemp2.text = [cellArray2 objectAtIndex:indexPath.row];
tableView.backgroundColor = [UIColor orangeColor];
return cell;
}
The answer to this problem was that I had hard coded a parameter that dictate how many items that was loaded during my testing.
I just found the problem, i had hard coded a parameter that dictated how many items that was

			
				
Make sure you are adding to the correct view if you are creating programatically and adding as subview to main view.

UITableViewCell - overlapping with previous cells contents

I have this wierd problem with my table
i Have about 20 cells to display
Each cell is about 84px in height
When i click no the cell, i have set a background colour
The first 4 cells are ok, but when i scroll down and click on the 5th cell, the content of each cell starts to overlap with some other content, usually content from 1st 4 cells.
I belive its some cell reusability or drawing issue. Am not sure how to solve it, i have checked through my code, but i am not changing the cell's content on touch.
Here is my code and will add some pics too
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 104;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [stores count];
}
-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CGRect Label1Frame = CGRectMake(5, 5, 250, 30);
CGRect Label2Frame = CGRectMake(6, 42, 220, 20);
CGRect Label3Frame = CGRectMake(6, 62, 220, 20);
CGRect Label4Frame = CGRectMake(240,56, 70, 12);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}else{
// a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields)
UIView *viewToCheck = nil;
viewToCheck = [cell.contentView viewWithTag:1];
if (!viewToCheck) {
[viewToCheck removeFromSuperview];
DebugLog(#"View removed");
}
}
//cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell setSelectedBackgroundView:bgView];
NSInteger row=indexPath.row;
UILabel *lblTemp;
[[cell contentView] clearsContextBeforeDrawing];
//Line 1
lblTemp=[[UILabel alloc] initWithFrame: Label1Frame];
lblTemp.tag=1;
lblTemp.text=[[stores objectAtIndex:row] objectAtIndex:0] ;
lblTemp.numberOfLines=2;
lblTemp.font = [UIFont boldSystemFontOfSize:13];
lblTemp.adjustsFontSizeToFitWidth=YES;
lblTemp.minimumFontSize=12;
lblTemp.textColor = [UIColor grayColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Line 2
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.tag = 2;
lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:1];
lblTemp.font = [UIFont systemFontOfSize:12];
lblTemp.textColor = [UIColor grayColor ];
lblTemp.textAlignment=UITextAlignmentLeft;
lblTemp.adjustsFontSizeToFitWidth=YES;
lblTemp.minimumFontSize=12;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Line 3
lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
lblTemp.tag = 3;
lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:2];
lblTemp.font = [UIFont systemFontOfSize:12];
lblTemp.textColor = [UIColor grayColor ];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Phone button
UIButton *phoneButton=[[UIButton alloc] initWithFrame:CGRectMake(240,16,30,30)];
[phoneButton setBackgroundImage:[UIImage imageNamed:#"phone.png"] forState:UIControlStateNormal];
[phoneButton setTag:row];
[phoneButton addTarget:self action:#selector(dialNumber:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:phoneButton];
//ANnotation button
UIButton *annotation=[[UIButton alloc] initWithFrame:CGRectMake(274,16,30,30)];
[annotation setTag:row];
[annotation setBackgroundImage:[UIImage imageNamed:#"tab.png"] forState:UIControlStateNormal];
[annotation addTarget:self action:#selector(openMap:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:annotation];
[annotation release];
//Distance label
//Line 3
lblTemp = [[UILabel alloc] initWithFrame:Label4Frame];
lblTemp.tag = 4;
lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:5];
lblTemp.textAlignment=UITextAlignmentCenter;
lblTemp.font = [UIFont systemFontOfSize:13];
lblTemp.textColor = [UIColor grayColor ];
[lblTemp setAdjustsFontSizeToFitWidth:YES];
[cell.contentView addSubview:lblTemp];
[phoneButton release];
[lblTemp release];
[cell setNeedsLayout];
[cell setNeedsDisplay];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath ];
for(UILabel *lbl in cell.contentView.subviews){
if([lbl isKindOfClass:[UILabel class]]){
lbl.textColor=[UIColor whiteColor];
}
}
//UITableViewCell *cell1;
//NSString *row=[NSString stringWithFormat:#"%d",indexPath.row];
svm = [[storesMapView alloc] initWithNibName:#"storesMapView" bundle:nil];
[svm initWithXML:stores:indexPath.row];
CGRect theFrame = svm.view.frame;
theFrame.origin = CGPointMake(self.view.frame.size.width, 0);
svm.view.frame = theFrame;
theFrame.origin = CGPointMake(0,0);
theFrame.size=CGSizeMake(320,355);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
svm.view.frame = theFrame;
[UIView commitAnimations];
[subView addSubview:svm.view];
backButton.hidden=NO;
}
I figured it out with some trial and error; if this can help some one
In cellforRowAtIndexPath i tried to clear all the cells subview before drawing the cell
//TRY TO REMOVE ALL CONTENT
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
I would be happy if someone can point me to some easier way
Thanks
You can use
[[[cell contentView] subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
If the cell in not nil, the above code will reduce the time for using the for loop similar to this
for(UIView *eachView in [cell subviews])
[eachView removeFromSuperview];
I also had the same issue.I also had composed tableView with labels.And when I scroll it down the contents got overlapped.But it got solved simply by editing one statement in cellForRowAtIndexPath.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
I think this will solve your problem.
I know this is a bit late, but I had a similar issue where UILabels created for a cell were still part of the cell when it was reused. So each successive update of the tableview created another UILabel on top of the existing one. I moved the creation of the Labels into the if condition as below and it resolved my issue. Hope it helps someone else. Also note no release as I am using ARC.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cityText = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
cityText.font = [UIFont fontWithName:#"Arial" size:20];
cityText.textAlignment = UITextAlignmentLeft;
cityText.backgroundColor = [UIColor clearColor];
regionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 20)];
regionText.font = [UIFont fontWithName:#"Arial" size:20];
regionText.textAlignment = UITextAlignmentLeft;
regionText.backgroundColor = [UIColor clearColor];
}
Setting nil to label text on UITableViewCell subclass 's method prepareForReuse() solved my problem -
override func prepareForReuse() {
super.prepareForReuse()
self.label1.text = nil
self.label2.text = nil
....
}
Shared if it helps anyone!

UITableviewcell loading data issue

I parsed the XML data from a feed. I stored all data in NSArray. I loaded the images and title in UITableview using UILable. But when I use the scroll, the text will be collapsed as follows.
My code is as follows...
-(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];
}
CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:13.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.text=[title1 objectAtIndex:indexPath.row];
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 release];
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(0.0f, 00.0f, 60.0f, 63.0f);
[cell.contentView addSubview:imageView];
//cell.imageView.image = myimage;
return cell;
}
I don't know how to solve this issue.
Every time the cell is reloaded, the UILabel is added.
Do something like
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:13.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 release];
}
valueField.text=[title1 objectAtIndex:indexPath.row];
To be more specific, since you are dequeuing cells, when you call cellForRowAtIndexPath it potentially can return you a cell that was already created.
That cell already has a label created.
So all of the magic of creating and setting up the custom views in you cell should only be done when dequeueReusableCellWithIdentifier returns a nil value