Images in Imageviews are swapping after scrolling GMGridView up & down in iphone - ios5

I have added UIImageView in GMGridView as I want to display the images in grid. After adding imageview in grid all works perfectly. I want to show different image in first row & first column. So I set that image after compairing with index.
But when I scroll up & down , it changes the image from first column to 2nd or 3rd column. Also same image is shown in the 6th or 7th row. What going wrong in this? Here is my code
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
UIImageView *imageView = nil;
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
cell.deleteButtonIcon = [UIImage imageNamed:#"close_x.png"];
cell.deleteButtonOffset = CGPointMake(-15, -15);
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
cell.contentView = imageView;
}
NSLog(#"Project Index value:- %d",index);
if (index == 0) {
imageView.image = [UIImage imageNamed:#"add.png"];
}
else{
imageView.image = [UIImage imageNamed:#"face.png"];
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
return cell;
}
Also do I need [[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)]; ?
Can anybody help me ? Thanks

Try using the code
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index {
CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
GMGridViewCell *cell = [gridView dequeueReusableCell];
UIImageView *imageView = nil;
if (!cell)
{
cell = [[GMGridViewCell alloc] init];
cell.deleteButtonIcon = [UIImage imageNamed:#"close_x.png"];
cell.deleteButtonOffset = CGPointMake(-15, -15);
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
NSLog(#"Project Index value:- %d",index);
if (index == 0) {
imageView.image = [UIImage imageNamed:#"add.png"];
}
else{
imageView.image = [UIImage imageNamed:#"face.png"];
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
cell.contentView = imageView;
}
return cell;
}
I think that the problem may be you are removing the cell using makeObjectsPerformSelector and then it'll never be added again after removing since the control may not enter into the condition if (!cell) {} due to the presence of the instance cell.
Hope this helps you.

Related

my imageviews are not behaving properly in the table view?

I have a table view loading in the below manner. If a condition is met I need to add a particular image above the cell.imageview. Also the images are coming in different dimensions. Below is my code can anybody point me in where i am going wrong.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if(array==nil||[array count]==0)
{
}
else
{
NSMutableDictionary *dicttable=[array objectAtIndex:indexPath.row];
NSString *head=[dicttable objectForKey:#"name"];
NSString *type=[dicttable objectForKey:#"type"];
NSString *imgUrl = [dicttable objectForKey:#"image"];;
if(imgUrl!=nil)
{
if(![[ImageCache sharedImageCache] hasImageWithKey:imgUrl])
{
cell.imageView.image = [UIImage imageNamed:#"noimage_icon.png"];
NSArray *myArray = [NSArray arrayWithObjects:cell.imageView,imgUrl,#"noimage_icon.png",[NSNumber numberWithBool:NO],nil];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate performSelectorInBackground:#selector(updateImageViewInBackground:) withObject:myArray];
cell.imageView.frame=CGRectMake(0,0,48,48);
cell.imageView.bounds=CGRectMake(0,0,48,48);
[cell.imageView setClipsToBounds:NO];
}
else
{
cell.imageView.image = [[ImageCache sharedImageCache] getImagefromCacheOrUrl:imgUrl];
cell.imageView.frame=CGRectMake(0,0,48,48);
cell.imageView.bounds=CGRectMake(0,0,48,48);
[cell.imageView setClipsToBounds:NO];
}
}
else
{
cell.imageView.image = [UIImage imageNamed:#"noimage_icon.png"];
}
if([type isEqualToString:#"YES"])
{
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"bluel.png"]];
[cell setBackgroundView:img];
[img release];
cell.textLabel.text = head;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.textColor=[UIColor grayColor];
cell.detailTextLabel.text = subtitle1;
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
else
{
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"wnew1.png"]];
[cell setBackgroundView:img];
[img release];
cell.textLabel.text = head;
cell.textLabel.backgroundColor = [UIColor clearColor];
[cell.imageView addsubview:spclimage]
cell.textLabel.text = head;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.textColor=[UIColor grayColor];
cell.detailTextLabel.text = subtitle1;
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
}
return cell;
Here the problem is only in the last row the special image is adding. Not in all rows. Also the image view size is different in all the time the tableview is reloading?
A couple of thoughts:
The updateImageViewInBackground seems suspect, as the name suggests that you're updating an image view, but you don't specify which cell you're updating.
I also see you doing a addSubview:spclimage. Obviously, if that spclimage was on another cell, as soon as you do addSubview, it will be removed from the previous location before being added to current cell. In fact, just the notion of adding an image as a subview of an existing imageview is curious.
If your cache doesn't have the image yet, I see where you're initializing the image with noimage_icon.png, but I don't see where you're actually updating the image view. You say updateImageViewInBackground is "then updating the image". Do you mean setting the image property for the imageView for this cell for this indexPath? Or maybe updating spclimage? If so, that's problematic.
The typical pattern (using GCD) for this would be:
cell.imageView.image = [UIImage imageNamed:#"noimage_icon.png"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = ... // do whatever you need to do to get the image, load cache, etc.
// ok, now that you have the image, dispatch the update of the UI back to the main queue
dispatch_async(dispatch_get_main_queue(), ^{
// because we're doing this asynchronously, make sure the cell is still
// visible (it could have scrolled off and the cell was dequeued and
// reused), so we're going to ask the tableview for the cell for that
// indexPath, and it returns `nil` if it's not visible. This method is
// not to be confused with the similarly named `UITableViewControllerDelegate`
// method.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// if the image view is still visible, update it
if (cell)
{
cell.imageView.image = image;
}
});
});

How can I insert an image in a UILabel, or make it look like I did?

I am writing a geometry based app. At one point in the app, there will be a UITableView with some custom cells. These cells contain UILabels. Amid the text of some these labels, I want to insert symbols that look these two triangles:
(source: wiley.com)
However, since I cannot find these symbols in any Apple fonts, is there a way to insert an image into the string in place of a symbol?
Here is a (very) rough idea of what I'm going for (the actual table will not be static):
Ok, I get what you're trying to do. The key, I think, is to just keep adding controls to your cell, calculating the width as you go along.
First, I'd suggest a data structure to hold your cell contents. A simple array will do the job. I generally do this stuff as an ivar:
#interface LabelWithImagesViewController ()
{
NSMutableArray *_cells;
}
#end
Then fill this array with the text and images you want. I'm doing a single row, but you can repeat for every row you need.
- (void)viewDidLoad
{
[super viewDidLoad];
_cells = [[NSMutableArray alloc] init];
[_cells addObject:[[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"triangle.png"],
#"CAT",
[UIImage imageNamed:#"semiequal.png"],
[UIImage imageNamed:#"triangle.png"],
#"DOG",
#" If",
[UIImage imageNamed:#"triangle1.png"],
#"then",
[UIImage imageNamed:#"triangle2.png"],
nil]];
}
And then, you need to create your cell:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _cells.count;
}
#define kEquationTag 100
#define kCellHeight 44
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"equationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *equationContainer;
if (cell == nil)
{
// if we don't have a cell create it, including the frame to hold our custom stuff
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
equationContainer = [[UIView alloc] initWithFrame:cell.contentView.bounds];
equationContainer.tag = kEquationTag;
[cell.contentView addSubview:equationContainer];
}
else
{
// if we are dequeing one that already exists, let's get rid of the old custom stuff
equationContainer = [cell.contentView viewWithTag:kEquationTag];
for (UIView *view in equationContainer.subviews)
{
[view removeFromSuperview];
}
}
// Configure the cell...
NSArray *cellContents = [_cells objectAtIndex:indexPath.row];
NSUInteger x = 0;
UIFont *font = [UIFont systemFontOfSize:12.0];
for (NSObject *obj in cellContents)
{
if ([obj isKindOfClass:[NSString class]])
{
NSString *text = (NSString *)obj;
CGSize size = [text sizeWithFont:font];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, (kCellHeight - size.height)/2.0, size.width, size.height)];
label.text = text;
label.font = font;
[equationContainer addSubview:label];
x += size.width;
}
else if ([obj isKindOfClass:[UIImage class]])
{
UIImage *image = (UIImage *)obj;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, (kCellHeight - image.size.height) / 2.0, image.size.width, image.size.height)];
imageView.image = image;
[equationContainer addSubview:imageView];
x += image.size.width;
}
}
return cell;
}
This yields:
Make a UILabel and use instances of that UILabel for each letter.
Use some geometry logic about the rect of the image view to place the letters regardless of size...such as
UILabel *aLetterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
aLetterLabel.text = #"A";
//centered top
aLetterLabel.frame = CGRectMake((shapeImage.frame.origin.x + (shapeImage.frame.size.width/2)), shapeImage.frame.origin.y, 35, 35);
//centered
aLetterLabel.frame = CGRectMake((shapeImage.frame.origin.x + (shapeImage.frame.size.width/2)), (shapeImage.frame.origin.y + (shapeImage.frame.size.height/2)), 35, 35);
//cenetered bottom
aLetterLabel.frame = CGRectMake((shapeImage.frame.origin.x + (shapeImage.frame.size.width/2)), (shapeImage.frame.origin.y+(shapeImage.frame.size.height-35)), 35, 35);
//left center align
aLetterLabel.frame = CGRectMake(shapeImage.frame.origin.x, (shapeImage.frame.origin.y + (shapeImage.frame.size.height/2)), 35, 35);
Wrote these up really fast as a proof of concept...feel free to revise, etc.
You can create your own font with the exact symbols you need. Try this:
http://glyphdesigner.71squared.com
You can't insert an image in a UILabel.
But you can add a UIImageView in the custom cells of your UITableView and then put a UIImage inside.

UITableView experiences choppy scrolling when cell has a UIImageView subview

This has been driving me crazy for the better part of the day.
I've got a UITableView with UIImageViews. These imageviews load a locally saved PNG-file in the cellForRow-function of the tableview, and this works fine except the tableview will stop scrolling for a fraction of a second when a cell with an image in it scrolls into sight so to speak. I've trawled StackOverflow and google for an answer but I've come up short - so any help will be greatly appreciated.
Here is my code for the CellForRow-function:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if([currSection isEqualToString:#"composer"]){
MySlide *s = [slidesArray objectAtIndex:indexPath.row];
cell.textLabel.hidden = YES;
UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)];
if([s.slideImage isEqualToString:#""] || s.slideImage == nil){
//no custom image in this cell - go with default background image
whiteView.image = [UIImage imageNamed:#"cellback2.png"];
whiteView.backgroundColor = [UIColor whiteColor];
}else{
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
NSData *data = [[NSData alloc] initWithContentsOfFile:s.slideImage];
UIImage *im = [[UIImage alloc] initWithData:data];
whiteView.image = im;
whiteView.image = [self imageWithImage:whiteView.image CovertToSize:CGSizeMake(204.8,153.6)];
whiteView.backgroundColor = [UIColor whiteColor];
}
[cell.contentView addSubview:whiteView];
[whiteView release];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
There are a couple of changes to be made, first off the UIImageViews should be added when the cell is generated, rather than every time tableView:cellForRowAtIndexPath: is hit (as #Vishy suggests). Secondly you should cache the images you are loading from the documents directory ([UIImage imageNamed:] does this automatically for bundle resources).
#interface MyViewController () {
NSMutableDictionary *_imageCache;
}
#end
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// other viewDidLoad stuff...
_imageCache = [[NSMutableDictionary alloc] init];
}
- (void)viewDidUnload {
[super viewDidUnload];
// other viewDidUnload stuff...
[_imageCache release];
_imageCache = nil;
}
- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)];
whiteView.tag = 111;
whiteView.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:whiteView];
[whiteView release];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.hidden = YES;
}
UIImageView* iView = (UIImageView*) [cell.contentView viewWithTag:111];
if([currSection isEqualToString:#"composer"]) {
MySlide *s = [slidesArray objectAtIndex:indexPath.row];
if([s.slideImage isEqualToString:#""] || s.slideImage == nil) {
//no custom image in this cell - go with default background image
iView.image = [UIImage imageNamed:#"cellback2.png"];
}
else {
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
// use the image path as the cache key
UIImage *theImage = [_imageCache objectForKey:s.slideImage];
if (theImage == nil) {
// load the image and save into the cache
theImage = [UIImage imageWithContentsOfFile:s.slideImage];
theImage = [self imageWithImage:theImage CovertToSize:CGSizeMake(204.8, 153.6)];
[_imageCache setObject:theImage forKey:s.slideImage];
}
iView.image = theImage;
}
}
}
#end
As a general rule, tableView:cellForRowAtIndexPath: is a method you need to get out of fast, so loading images from disk should be avoided wherever possible.
Change the code as per below...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)];
whiteView.tag = 111;
whiteView.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:whiteView];
[whiteView release];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.hidden = YES;
}
UIImageView* iView = (UIImageView*) [cell.contentView viewWithTag:111];
if([currSection isEqualToString:#"composer"])
{
MySlide *s = [slidesArray objectAtIndex:indexPath.row];
if([s.slideImage isEqualToString:#""] || s.slideImage == nil)
{
//no custom image in this cell - go with default background image
iView.image = [UIImage imageNamed:#"cellback2.png"];
}
else
{
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
iView.image = [UIImage imageWithContentsOfFile:s.slideImage];
iView.image = [self imageWithImage:iView.image CovertToSize:CGSizeMake(204.8,153.6)];
}
}
}
There is also a delay the first time each UIImage is displayed. See this post for details on prompting the work at cache time rather than display time:
Setting image property of UIImageView causes major lag

Adding UIImageView dynamically in UITableViewCell

I am using a UITableView to display some information about the consequences of driving to fast: What the fine is going to be etc. I also use some red "dots" that you get in your driving licence. Since this can be between 0 - 6 for each row I have done the following:
- (UITableViewCell *)tableView:cellForRowAtIndexPath:
// Code if no cell is available in the que
// End of that code
UIImage *seniorImage = [UIImage imageNamed:#"ticket_dot.png"];
if (seniorDots == 0) {
} else {
for (int i = 0; i < seniorDots; i++) {
UIImageView *seniorImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40.0 + (i * 15.0), 40.0, 15.0, 40.0)];
seniorImageView.image = seniorImage;
seniorImageView.contentMode = UIViewContentModeCenter;
[cell.contentView addSubview:seniorImageView];
[seniorImageView release];
}
}
seniorDots is a variable that I get from a plist file. The code is working as intended but as you may see I have no way of removing them. So the first time I open the tableview I see the expected view, but if I scroll up again the code just keep adding UIImageViews on the cell, not removing the old ones. How can I reference these dots to remove them from screen before adding new ones?
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imgView;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)];
[imgView setImage:[UIImage imageNamed:#"img.png"]];
imgView.tag = 55;
[cell.contentView addSubview:imgView];
[imgView release];
}
else
{
imgView = (id)[cell.contentView viewWithTag:55];
}
You can put below code inside your cellForRowAtIndexPath method:
//Remove Other Data
for (UIImageView *img in cell.contentView.subviews) {
if ([img isKindOfClass:[UIImageView class]]) {
[img removeFromSuperview];
}
}
I hope it will be helpful to you. Let me know in case of any difficulty.

UITableViewCell backgroundView gets reused when it shouldn't

I am wanting to add a custom background and selected background images for my tableview cells. Currently it seems that when the cells get reused, the background images get screwed up, the top cell will use the bottom cells image, etc etc.
Am I reusing cells incorrectly in this case?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UIImageView *linkAvailableImageView = nil;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)];
UIImageView *selectedBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)];
// Asset
Asset *asset = nil;
asset = (Asset *)[items objectAtIndex:indexPath.row];
int count = [items count];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.row == 0 && count > 1) {
backgroundImage.frame = CGRectMake(10, 0, tableView.bounds.size.width-20, 45);
backgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
selectedBackgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45);
selectedBackgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundSelectedTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
} else if (indexPath.row == count-1 && count > 1) {
backgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
selectedBackgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundSelectedBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
} else if (indexPath.row == 0 && count == 1) {
backgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45);
backgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
selectedBackgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundSelectedSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
} else {
backgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10];
selectedBackgroundImage.image = [[UIImage imageNamed:#"MDACCellBackgroundSelectedMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10];
}//end
backgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[backgroundView addSubview:backgroundImage];
[backgroundImage release];
selectedBackgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[selectedBackgroundView addSubview:selectedBackgroundImage];
[selectedBackgroundImage release];
cell.backgroundView = backgroundView;
[backgroundView release];
cell.selectedBackgroundView = selectedBackgroundView;
[selectedBackgroundView release];
linkAvailableImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width-39, 9, 24, 24)] autorelease];
linkAvailableImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
linkAvailableImageView.image = [UIImage imageNamed:#"MDACLinkArrow.png"];
linkAvailableImageView.tag = 3;
[cell.contentView addSubview:linkAvailableImageView];
} else {
linkAvailableImageView = (UIImageView *)[cell.contentView viewWithTag:3];
}
// Get asset
cell.textLabel.opaque = NO;
cell.textLabel.text = asset.name;
cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
cell.textLabel.backgroundColor = [UIColor colorWithWhite:94./255. alpha:1];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.6];
cell.textLabel.shadowOffset = CGSizeMake(0, -1);
// Set the kind of disclosure indicator
if ([asset.children intValue] > 0) {
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}//end
// Lazy Load the image
if (!asset.appIcon) {
// Download icon
[self startIconDownload:asset forIndexPath:indexPath];
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:#"default-icon.png"];
} else {
cell.imageView.image = asset.appIcon;
}//end
return cell;
}//end
The problem here is that you are using the same cell identifier regardless of the position in the table view.
So you initially create the cells based on the indexPath.row and the count, but you associate those cells with an identifier of #"Cell". So when you scroll down dequeueReusableCellWithIdentifier will return a cell configured for the beginning of the list (indexPath.row == 0 && count > 1) and use it for the end of the list.
You need to make sure cell identifier reflects the code at the beginning of your cell==nill if block, so that you only reuse cells that have been configured for the position in the table you are creating.
As Eiko points out, you are also leaking your UIView and UIImageView objects. You could stick them in the if block, release them explicitly or just make them autorelease.
Sorry, but that code has lots of problems: You are leaking the UIView and UIImageView objects, and the whole reuse of cells is wrong, hence your problems.
You should set up a new cell (with views) only in the if (cell == nil) part, and don't forget to release/autorelease your views. Then, outside of that block, you configure your cell accordingly (set its contents).
I strongly suggest to look through some of Apple's example projects!