Adding imageViews to next row of table view - objective-c

in my app i have a table view and i am adding 4 image view to the first row of my table view .Now i want to add the image view to the next row.How can i do that in the existing code please help.I am posting my part of code:--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
UIImageView * imageView5 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
UIImageView * imageView6 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
int j=0;
imageView1.tag = j;
imageView2.tag = j+1;
imageView3.tag = j+2;
imageView4.tag = j+3;
imageView5.tag = j+4;
imageView6.tag = j+5;
[cell.contentView addSubview:imageView1];
[cell.contentView addSubview:imageView2];
[cell.contentView addSubview:imageView3];
[cell.contentView addSubview:imageView4];
[cell.contentView addSubview:imageView5];
[cell.contentView addSubview:imageView6];
}
for ( int i = 1; i <= j; i++ ) {
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
imageView.image = nil;
}
int photosInRow;
if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([sentence count] % 4 == 0) ) {
photosInRow = 4;
} else {
photosInRow = [sentence count] % 4;
}
for ( int i = 1; i <=[sentence count]; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
[self setImage1:imageView];
}
return cell;
}
I want mu imageView 5 and 6 to be in next row.Please help.How to do it?i am ramming my head against the wall looking for this.
Thanks,
Christy

you can try some thing like bellow ... you basically check the present row number then build your required logic ...
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
}
int j=0; imageView1.tag = j;
if(0 == [indexPath row])
{
UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
imageView2.tag = j+1;
imageView3.tag = j+2;
imageView4.tag = j+3;
[cell.contentView addSubview:imageView1];
[cell.contentView addSubview:imageView2];
[cell.contentView addSubview:imageView3];
[cell.contentView addSubview:imageView4];
}else if(0 == [indexPath row])
{
UIImageView * imageView5 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
UIImageView * imageView6 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
imageView5.tag = j+1;
imageView6.tag = j+2;
[cell.contentView addSubview: imageView5];
[cell.contentView addSubview:imageView6];
}
for ( int i = 1; i <= j; i++ ) {
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
imageView.image = nil;
}

Related

image getting overlaped in table view cell

I m parsing a json feed and populating my table view.if my parsed value is "1" and if its for the first four rows of table view cell.i got to make a green checkmark and if its not for the first four rows the checkmark got to be grey check.if my parsed value is null.i got to make no checkmark.everthng works fine.but if i scroll my table view .the check mark images gets overlaped.below is the screen shot and the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
NSString *str=[[NSString alloc]initWithFormat:#"%#",boy];
if ([str isEqualToString:#"1"])
{
if(indexPath.row==0||indexPath.row==1||indexPath.row==2||indexPath.row==3)
{
CGRect starFrame = CGRectMake(260, 18, 50, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"tic.png"];
[cell.contentView addSubview:starImage];
}
else
{
CGRect starFrame1 = CGRectMake(260, 18, 50, 40);
UIImageView *starImage1 = [[[UIImageView alloc] initWithFrame:starFrame1] autorelease];
starImage1.image = [UIImage imageNamed:#"brrr.png"];
[cell.contentView addSubview:starImage1];
}
}
cell.textLabel.text=[story objectAtIndex:indexPath.row];
return cell;
}
You are reusing the cell. So, tableView will return used cell once they are not visible.
To avoid this problem, you should right below code.
if ([str isEqualToString:#"1"])
{
if(indexPath.row==0||indexPath.row==1||indexPath.row==2||indexPath.row==3)
{
CGRect starFrame = CGRectMake(260, 18, 50, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"tic.png"];
starImage.tag = 1000;
[cell.contentView addSubview:starImage];
}
else
{
CGRect starFrame1 = CGRectMake(260, 18, 50, 40);
UIImageView *starImage1 = [[[UIImageView alloc] initWithFrame:starFrame1] autorelease];
starImage1.image = [UIImage imageNamed:#"brrr.png"];
starImage.tag = 1000;
[cell.contentView addSubview:starImage1];
}
}
else
{
UIImageView *starImage1 = [cell.contentView viewWithTag:1000];
starImage1.image = nil;
}
Use the following code,
if ([str isEqualToString:#"1"])
{
if(indexPath.row==0||indexPath.row==1||indexPath.row==2||indexPath.row==3)
{
CGRect starFrame = CGRectMake(260, 18, 50, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"tic.png"];
starImage.tag = 1000;
[cell.contentView addSubview:starImage];
}
else
{
CGRect starFrame1 = CGRectMake(260, 18, 50, 40);
UIImageView *starImage1 = [[[UIImageView alloc] initWithFrame:starFrame1] autorelease];
starImage1.image = [UIImage imageNamed:#"brrr.png"];
starImage1.tag = 1000;
[cell.contentView addSubview:starImage1];
}
}
else
{
UIImageView *starImage = [cell.contentView viewWithTag:1000];
if (starImage) {
[starImage removeFromSuperview];
}
}

Reloading table view in xcode

I have a text box in my app with a button and a table view.Inside this table view i am creating image views which will display some images.Now when i enter a number say 5 in the text box and then click the button then 5 images are displayed on the table view out of which 4 images are in the first row .Now the problem comes if i enter number less than 5 say 3 then i have to get only 3 images in the table view but still i am getting 5 images and if i enter more than 5 then i am getting the desired number on the table view.My piece of code is:-
if([imageArray count] >0)
{
[imageArray removeAllObjects];
}
int j = [text.text intValue];
for(int i=0;i<j;i++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
}
[tableView reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
// 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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,5,100,125)];
if([imageArray count]>0)
{
imageView1.image=[UIImage imageNamed:#"CARRY.png"];
imageView1.image = [imageArray objectAtIndex:0];
[cell.contentView addSubview:imageView1];
[imageView1 release];
}
if([imageArray count]>1)
{
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
imageView2.image = [imageArray objectAtIndex:1];
[imageView2 addSubview:button6];
[cell.contentView addSubview:imageView2];
[imageView2 release];
}
if([imageArray count]>2)
{
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(310, 5, 100, 125)];
imageView3.image = [imageArray objectAtIndex:2];
[cell.contentView addSubview:imageView3];
[imageView3 release];
}
if([imageArray count]>3)
{
UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(460,5,100,125)];
imageView4.image = [imageArray objectAtIndex:3];
[cell.contentView addSubview:imageView4];
[imageView4 release];
}
}
else if(indexPath.row == 1)
{
if([imageArray count]>4)
{
UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(0,5,100,125)];
imageView5.image = [imageArray objectAtIndex:4];
[cell.contentView addSubview:imageView5];
[imageView5 release];
}
if([imageArray count]>5)
{
UIImageView *imageView6 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
imageView6.image = [imageArray objectAtIndex:5];
[cell.contentView addSubview:imageView6];
[imageView6 release];
}
if([imageArray count]>6)
{
UIImageView *imageView7= [[UIImageView alloc] initWithFrame:CGRectMake(310, 5, 100, 125)];
imageView7.image = [imageArray objectAtIndex:6];
[cell.contentView addSubview:imageView7];
[imageView7 release];
}
if([imageArray count]>7)
{
UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(460,5,100,125)];
imageView8.image = [imageArray objectAtIndex:7];
[cell.contentView addSubview:imageView8];
[imageView8 release];
}
}
else if(indexPath.row == 2)
{
if([imageArray count]>8)
{
UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(0,5,100,125)];
imageView8.image = [imageArray objectAtIndex:8];
[cell.contentView addSubview:imageView8];
[imageView8 release];
}
if([imageArray count]>9)
{
UIImageView *imageView8 = [[UIImageView alloc] initWithFrame:CGRectMake(150,5,100,125)];
imageView8.image = [imageArray objectAtIndex:9];
[cell.contentView addSubview:imageView8];
[imageView8 release];
}
}
return cell;
}
I am clearing the array and again adding images to it then reloading the table view but why is the output then not coming according to the needs.Please help.
Thanks,
Christy
The problem is that you're adding the image views but you aren't removing the image views that you've added earlier in case of cell reuse.

App crashing when scrolling table view

In my app i have a table view displaying images view ,,4 image view in one row .My issue is that after loading the table view with some data then if i try to scroll the table view then my app is crashing.On using the breakpoint i found that cell for row at index path methid is called again and my array tries to reload data again.Please help as its getting on my head now,i am posting my code here:--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 70, 80)] autorelease];
UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,70, 80)] autorelease];
UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 70, 80)] autorelease];
UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 70, 80)] autorelease];
UIImageView * imageView5 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 70, 80)] autorelease];
UIImageView * imageView6 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,70, 80)] autorelease];
UIImageView * imageView7 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 70, 80)] autorelease];
UIImageView * imageView8 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 70, 80)] autorelease];
UIImageView * imageView9 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 70, 80)] autorelease];
UIImageView * imageView10 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,70, 80)] autorelease];
UIImageView * imageView11 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 70, 80)] autorelease];
UIImageView * imageView12 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 70, 80)] autorelease];
imageView1.tag=1;
imageView2.tag=2;
imageView3.tag=3;
imageView4.tag=4;
imageView5.tag=5;
imageView6.tag=6;
imageView7.tag=7;
imageView8.tag=8;
imageView9.tag=9;
imageView10.tag=10;
imageView11.tag=11;
imageView12.tag=12;
if ([sentence count]>0) {
[imageViewArray insertObject:imageView1 atIndex:0];
[cell.contentView addSubview:imageView1];
}
if ([sentence count]>1) {
[imageViewArray insertObject:imageView2 atIndex:1];
[cell.contentView addSubview:imageView2];
}
if ([sentence count]>2) {
[imageViewArray insertObject:imageView3 atIndex:2];
[cell.contentView addSubview:imageView3];
}
if ([sentence count]>3) {
[imageViewArray insertObject:imageView4 atIndex:3];
[cell.contentView addSubview:imageView4];
}
if ([sentence count]>4) {
[imageViewArray insertObject:imageView5 atIndex:4];
[cell.contentView addSubview:imageView5];
}
if ([sentence count]>5) {
[imageViewArray insertObject:imageView6 atIndex:5];
[cell.contentView addSubview:imageView6];
}
if ([sentence count]>6) {
[imageViewArray insertObject:imageView7 atIndex:6];
[cell.contentView addSubview:imageView7];
}
if ([sentence count]>7) {
[imageViewArray insertObject:imageView8 atIndex:7];
[cell.contentView addSubview:imageView8];
}
if ([sentence count]>8) {
[imageViewArray insertObject:imageView9 atIndex:8];
[cell.contentView addSubview:imageView9];
}
if ([sentence count]>9) {
[imageViewArray insertObject:imageView10 atIndex:9];
[cell.contentView addSubview:imageView10];
}
if ([sentence count]>10) {
[imageViewArray insertObject:imageView11 atIndex:10];
[cell.contentView addSubview:imageView11];
}
if ([sentence count]>11 || ([sentence count]==12)) {
[imageViewArray insertObject:imageView12 atIndex:11];
[cell.contentView addSubview:imageView12];
}
}
if ([sentence count]!=0)
{
int photosInRow;
if ( (indexPath.row <
[tableView numberOfRowsInSection:indexPath.section] - 1) || ([sentence count] % 4 == 0) ) {
photosInRow = 4;
} else {
photosInRow = [sentence count] % 4;
}
for ( int i = 1; i <=photosInRow ; i++ ){
imageView = (UIImageView *)[cell.contentView viewWithTag:j];
[self setImage1:imageView];
}
}
return cell;
}
I believe the biggest problem here is the memory leaks for you imageViews. Each cell creates 12 imageViews, none of which is released. It seems that you should only create the imageViews you need (for speed), and release them correctly (for memory management).
One way to begin to rewrite this code, is the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
for (int i=0; i <= [sentence count]; ++i) {
UIImageView * imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 4, 70, 80)] autorelease];
imageView.tag = i+1;
[cell.contentView addSubview:imageView];
[imageView release];
}
}
return cell;
}
I don't understand why you are setting imageViewArray or imageView1 repeatedly. Also, this line:
imageView = (UIImageView *)[cell.contentView viewWithTag:j];
makes no sense as j is not defined.
On using the breakpoint i found that cell for row at index path methid is called again and my array tries to reload data again.
That's right. If you don't understand that, you don't understand how a table view works. The cells are created on demand and reused; the same cell might be reused again and again for different rows of the table as the user scrolls. That means that you must prepare all the data beforehand, and simply fetch it on demand for any section/row requested.
To put it another way, MVC: model-view-controller. Your model is your data. tableView:cellForRowAtIndexPath: is view; you must not modify the model during this, because when and how often it is called depends entirely on the needs of the view.
Another thing is that you should not be making a separate array of image views. An image view is view. If you want to store a list of something, store a list of the names of the images or something.

Table View displaying different images while scrolling

I am loading some images in my table view with a click of a button.The images are loaded fine and in desired positions.But when i scroll the table view then images seems to change .Let me elaborate only 2 rows are visible in my table view initially ,and lets assume the images are there in both rows and 4 images in a row.Now when i scroll the table view downwards or upwards the row which is scrolled above or below the table view frame will show the new image in its image view.And everytime when i scroll the images keep on changing.What could be the reason.I am scratching my head with this.Please help.I am posting a part of my code:-
-(UITableViewCell*)tableView(UITableView*)
ableViewcellForRowAtIndexPath(NSIndexPath*)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:AutoCompleteRowIdentifier] autorelease];
}
UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
imageView1.tag = 1;
imageView2.tag = 2;
imageView3.tag = 3;
imageView4.tag = 4;
[cell.contentView addSubview:imageView1];
[cell.contentView addSubview:imageView2];
[cell.contentView addSubview:imageView3];
[cell.contentView addSubview:imageView4];
UIImageView * imageView;
for ( int i = 1; i <= 4; i++ ) {
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
imageView.image = nil;
}
int photosInRow;
if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) ||
(count % 4 == 0) ) {
photosInRow = 4;
} else {
photosInRow = count % 4;
}
for ( int i = 1; i <= photosInRow; i++ ) {
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
[self setImage1:imageView];
}
return cell;
}
-(void)setImage1:(UIImageView *)imageView
{
UIImageView *imageView1=[[UIImageView alloc]init];
imageView1=imageView;
imageView1.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", j]];
j++;
}
Any help will be appreciated.
Thanks,
Christy
Your setImage1: method should be modified to take in the photo index as j is not a proper way to track the current image as cellForRowAtIndexPath: may be called in any order.
- (void)setImage1:(UIImageView *)imageView forPhotoIndex:(NSInteger)index {
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%ld.png", index]];
}
and minor modification in cellForRowAtIndexPath: would be,
[..]
for ( int i = 1; i <= photosInRow; i++ ) {
imageView = (UIImageView *)[cell.contentView viewWithTag:i];
[self setImage1:imageView forPhotoIndex:(indexPath.row * 4 + i - 1)];
}
[..]
When reusing a cell, you currently just add new UImageView instances. The reused cell already has subviews added and you just add more. Be sure to only add new UIImageViews if the cell was not reused before (within the if-clause)
if (cell == nil) {
cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:AutoCompleteRowIdentifier] autorelease];
UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
imageView1.tag = 1;
imageView2.tag = 2;
imageView3.tag = 3;
imageView4.tag = 4;
[cell.contentView addSubview:imageView1];
[cell.contentView addSubview:imageView2];
[cell.contentView addSubview:imageView3];
[cell.contentView addSubview:imageView4];
}
EDIT: See comments below about issues in setting the actual image.

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