UITableView cell image problem - iphone

I am trying to import an image in just cell number 1 and 2 ! , but I the result is my image will show in last cell ! I do not know why !! this is the picture that shows my situation :
// Configure the cell.
NSUInteger row = [indexPath row];
cell.textLabel.text = [titles objectAtIndex:row];
cell.detailTextLabel.text = [subtitle objectAtIndex:row];
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
}
return cell;
}

Either in your tableView:cellForRowAtIndexPath: set the imageView's image to nil before conditionally checking to set the new image or implement prepareForReuse on your cell subclass and set all of the cell's views values to nil.
This will ensure that reused cells are 'clean' before they're brought on screen.
Alternatively you could edit your switch to look like:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
default:
cell.imageView.image = nil;
break;
}

The first thing that it makes me think of is that the first cell gets somehow recycled and used for the last cell. You can try to set the image view to nil for every cell and set it just in the first two cells. Should be something like this:
cell.imageView.image = nil;
Hope it helps! ;D

You might have used "static NSString *reuseIdentifier = #"Identifier" ", this means that all cells will be considered same by this reuseIdentifier. Only visible cells would be different so for example, if there are 5 visible cells on device, then only 5 new cells would be allocated for cell, and then if you scroll down or up, these 5 cells will be reused if you specified reuseIdentifier statically.
I would suggest to make cell uniquely identified by reuseIdentifier, change the above line to "NSString *reuseIdentifier = [NSString stringWithFormat:#"cell%d",indexPath.row]" This would solve the issue.
Hope this helps.

Your cells are reused by the tableview to save memory. You have to tell the cell not to use an image on every other cell. Modify the switch command like this:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
default:
cell.imageView.image = nil;
break;
}
Sandro Meier

try this it might help you
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
default
cell.imageView.image=nil;

Related

UIImage displayed in cells with specified string value

This piece gives strange results in my TableView. I want the UIImage displayed for the cells where the reprecented objects value for key "Marked" = "Yes".
What is the correct code for this?
if ([[[sortedObjects objectAtIndex: indexPath.row] valueForKey:#"Marked"] isEqual:#"Yes"])
{
cell.markedImageView.image = [UIImage imageNamed:#"markedItem.png"];
}
The problem is that you need to clear the imageView image each time the cell is reused.
- (void)configCell:(MyCustomCell *)cell atIndexPath:(NSIndexPath)indexPath
{
cell.markedImageView.image = nil;
// configure cell normally
if ([[[sortedObjects objectAtIndex: indexPath.row] valueForKey:#"Marked"] isEqual:#"Yes"])
{
cell.markedImageView.image = [UIImage imageNamed:#"markedItem.png"];
}
}

how to tag UIImage in UITableViewCell

I want to toggle UITableViewCell image - cell.imageView.image. (eg. Red <--> Green)
If the current image is green, the image suppose to change to Red when the user click the UITableViewCell.
Once I set the image
cell.imageView.image = [UIImage imageNamed:#"Green.png"];
How to detect which image is the cell currently using?
Thanks for any help!
Set the tag on the imageView itself:
#define IMAGE_TAG_GREEN 50
#define IMAGE_TAG_RED 51
-(UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *CELL_ID = #"some_cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID];
if(cell == nil) {
//do setup here...
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID] autorelease];
cell.imageView.tag = //some logic here...
}
if(cell.imageView.tag == IMAGE_TAG_GREEN) {
//...
} else {
//...
}
return cell;
}
Since tag is an inherited property from UIView, you cannot use it with UIImage itself, but you can use it with UIImageView
It's a bit crude, but you should be able to set up an if statement like the following:
if ([cell.imageView.image isEqual:[UIImage imageNamed:#"Green.png"]]) {
// image is green;
} else {
// image is red;
}
I tested it out just to make sure and it works fine
I believe just add a Boolean expression and make it TRUE if it is green and FALSE if it is red.
Make this boolean expression extern type so that this could be a global expression.
Set the boolean value on clicking the image.
I believe this would be of some help.
I'm new to this - but I imagine you could use an if/then statement.
If (cell.imageView.image = [UIImage imageNamed:#"Green.png"]) {
cell.imageView.image = [UIImage imageNamed:#"Green.png"];
} else
{
cell.imageView.image = [UIImage imageNamed:#"Red.png"];
}
Or you could go really fancy and use the ternary operator. Something like the following (note the code below is probably wrong - but should hopefully get you started!):
cell.imageView.image = ([UIImage imageNamed:#"Green.png"]) ?
cell.imageView.image = [UIImage imageNamed:#"Green.png"]; :
cell.imageView.image = [UIImage imageNamed:#"Red.png"];
Let us know how you go with this.
Kolya

UITableViewCell caching its backgroundView property

I have a UITableView which makes use of cells with custom backgroundViews. I'm assigning the backgroundViews in tableView:cellForRowAtIndexPath:indexPath: as suggested here. In a nutshell, dependent upon the position of a UITableViewCell within its UITableView, I want to assign certain backgroundView images. Here's the code in question:
UIImage *rowBackground = nil;
if (row == 0 && row == sectionRows - 1) {
rowBackground = [UIImage imageNamed:#"row_bg_start_and_end.png"];
} else if (row == 0) {
rowBackground = [UIImage imageNamed:#"row_bg_start.png"];
} else if (row == sectionRows - 1) {
rowBackground = [UIImage imageNamed:#"row_bg_end.png"];
} else {
rowBackground = [UIImage imageNamed:#"row_bg.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
This works fine, and I see the results I'm expecting. I have issues, however, when it comes to the removal or addition of rows, which invariably means certain rows retain their previous backgroundViews, rather than recalculating their placement and updating the view in question.
I understand the purpose of dequeueReusableCellWithIdentifier and why this is happening. But I'm not sure how to go about fixing it correctly. I can tell that the rows are recalculated correctly when tableView:cellForRowAtIndexPath:indexPath: is called by scrolling them off screen, which results in their backgroundView being reset correctly.
Should I be setting the backgroundView property in willDisplayCell:forRowAtIndexPath: also? How should I handle this situation?
The easiest approach to fix the problem you're having is to reprocess the background images of all visible rows when you add new rows to a section.
i.e.
for (UITableViewCell *cell in aTableView.visibleCells)
{
NSIndexPath *cellIndexPath = [aTableView indexPathForCell:cell];
/* look at the cell.backgroundImage and if it's not appropriate
for the indexPath, update it */
}
You could carefully override all the UITableView methods that insert new rows and only check perform this work for the specific rows that need to be updated but I don't think this would be worth the effort.

Issue about vertical text alignment in UITableViewCell (iPhone)

I use just UITableViewCell.
It occur only in real device(version 3.1.2) and in simulator(version 3.1.2) doesn't have any problem.
Thank you for your advice.
Here is my problem image
It seems like a bug to me, but a workaround is to set cell.detailTextLabel.text = #""; if you don't want any text to appear on the right side of the cell.
Or
If there are empty rows try filling it with #""
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"";
break;
case 1:
[cell addSubview:label];
break;
case 2:
cell.textLabel.text = #"";
break;
}
The 0th row and 2nd row don't have any data in them. Hence filled it with #"".

iphone code - custom table instead of default one

i'm using a default style table (UITableViewCellStyleSubtitle)
i want to add more then one detailTextLabel in each row,
how can i customize it?
code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Leave cells empty if there's no data yet
if (nodeCount > 0)
{
// Set up the cell...
ARecord *aRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = aRecord.lDate;
cell.detailTextLabel.text = aRecord.WNum;
// Only load cached images; defer new downloads until scrolling ends
//(!aRecord.appIcon) - use icon
if (!aRecord.appIcon)
{
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:aRecord forIndexPath:indexPath];
}
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
cell.imageView.image = aRecord.appIcon;
}
}
return cell;
}
The best way of doing this is to add a UILabel to the cell.contentView. You would do this when you initially create the cell. I've found two things to be especially helpful: to lay out the label on a table cell in a throwaway document in Interface Builder to determine the initial frame. It's also especially helpful to set the autoresizingMask so that the label will be resized appropriately when the cell is resized (due to autorotation, going into edit mode, etc.).
Finally, you'll need to set the table view's rowHeight to a higher value to accommodate the larger cells, otherwise you'll end up with overlapping cells.
Also, set a tag on your label to make it easy to retrieve with viewWithTag: when you go to update the text.
You could add the labels to cell.contentView.