Changing Image on selected state of UITablView cell - ios

I have a created a custom cell and added one label and Image, I have 4 rows in my table each row has a different image and each row opens a different view controller, so, now what I need is on click of a particular row I want the image to change to do that I tried this, but its not working, so please help me out.
if(indexPath.row == 0)
{
if(cell.selected == true)
{
UIImage *cellImage = [UIImage imageNamed:#"abc.png"];
cell.icon.image = cellImage;
}
else
{
UIImage *cellImage = [UIImage imageNamed:#"abc.png"];
cell.icon.image = cellImage;
}
}
Regards
Ranjit

Try to do following when creating your cell or in tableView:willDisplayCell:forRowAtIndexPath: method:
cell.imageView.image = [UIImage imageNamed:#"abc.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"abc.png"];
It will work for your icon property too if it is UIImageView

First create a property in your custom cell for uiImageview and synthesize it..
and the in didSelectRowAtIndexPath delegate method of UITabeView access the property and change the image something like :-
yourCell.yourImageView.image=[UIImage imageNamed:#"yourImage"]
For Sample I am giving my Code :-
#import <UIKit/UIKit.h>
#interface CustomizedCellProductDetails : UITableViewCell {
UILabel *sNO;
UILabel *abcWine;
UILabel *redWine;
UILabel *two;
UILabel *hundred;
UILabel *fourTwo;
UILabel *twoOne;
UIImageView *imgView;
UILabel *itemNo;
UILabel *itemName;
UILabel *itemDesc;
UILabel *department;
UILabel *qtyAvailable;
UIButton *check;
}
#property (nonatomic , retain) UILabel *sNO;
#property (nonatomic , retain) UILabel *abcWine;
#property (nonatomic , retain) UILabel *redWine;
#property (nonatomic , retain) UILabel *two;
#property (nonatomic , retain) UILabel *hundred;
#property (nonatomic , retain) UILabel *fourTwo;
#property (nonatomic , retain) UILabel *twoOne;
#property (nonatomic , retain) UIImageView *imgView;
#property (nonatomic , retain) UILabel *itemNo;
#property (nonatomic , retain) UILabel *itemName;
#property (nonatomic , retain) UILabel *itemDesc;
#property (nonatomic , retain) UILabel *department;
#property (nonatomic , retain) UILabel *qtyAvailable;
#property (nonatomic , retain) UIButton *check;
-(void) clicked;
#end
.m file synthesize it:-
#import "CustomizedCellProductDetails.h"
#implementation CustomizedCellProductDetails
#synthesize sNO,abcWine,redWine,two,hundred,fourTwo,twoOne,imgView,itemNo,itemName,itemDesc,department,qtyAvailable,check;
in tableview delegate :-
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
CustomizedCellProductDetails * cell = (CustomizedCellProductDetails )[tableView cellForRowAtIndexPath:indexPath];
[cell.imgView setImage:[UIImage imageNamed:#"wine.png"]];
}

Try to do following when creating your cell ...
In Implementation Block...
#implementation ABC
{
NSMutableArray *imageArray;
NSMutableArray *imageSelectedArray;
}
Do this In the below method
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell.imageView setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]];
[cell.imageView setHighlightedImage:[UIImage imageNamed:[imageSelectedArray objectAtIndex:indexPath.row]]];
}

Related

uitableview array of objects as datasource

i have a UITableView in a storyboard in an ios xcode project,
i also have an array of menuitem objects stored in itemarray:
NSMutableArray *itemarray;
this is my menuitem.h file
#import <Foundation/Foundation.h>
#interface menuitem : NSObject
#property (nonatomic) NSString *itemtitle;
#property (nonatomic) NSString *itemdesc;
#property (nonatomic) int itemid;
#property (nonatomic) int itemprice;
#property (nonatomic) NSString *itemcat;
#end
this is my tableviewcell.m file:
#import "tableviewcell.h"
#import "menuitem.h"
#interface tableviewcell ()
#property (nonatomic, weak) IBOutlet UILabel *titleLabel;
#property (nonatomic, weak) IBOutlet UILabel *descLabel;
#property (nonatomic, weak) IBOutlet UILabel *priceLabel;
#end
#implementation tableviewcell
-(void)configureWithmenuitem:(menuitem *)item{
NSLog(#"hello");
self.titleLabel.text = item.itemtitle;
self.descLabel.text = item.itemdesc;
self.priceLabel.text = [NSString stringWithFormat:#"%.1d", item.itemprice];
}
#end
i want to get the itemtitle, itemdesc and itemprice from each instance of menuitem into 3 labels on each of the tableviewcells
Your tableviewcell is a View and thus it should only know how to display itself and not what to display. It's the job of your controller (through the delegate tableView:cellForRowAtIndexPath: method) to tell the view what to display.
Thus you should do something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Id"];
//Assuming you've stored your items in an array.
Item *item = [items objectAtIndex:indexPath.row];
cell.titleLabel.text = item.itemTitle;
cell.descLabel.text = item.itemdesc;
cell.priceLabel.text = [NSString stringWithFormat:#"%.1d", item.itemprice];
return cell;
}
^ The above code assumes you have a prototype cell. If you don't, you'll have to alloc if cell is nil after dequeue.

iOS UICollectionView memory leak

I've created UICollectionView through storyboard.
My cell is custom cell class that have 3 buttons with images.
My images are available as part of class GalleryItemInfo. I have an array of those objects
[GalleryDataProvider sharedInstance].itemInfo
There is code for cellForItemAtIndexPath (in one cell there are three buttons for three items in array):
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCellPreviewTriple *cell;
if (indexPath.row % 2 == 0 && !is_iPhone) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellOrangeRed" forIndexPath:indexPath];
if (is_Fingerprint_Version) {
cell.imageViewRope.image = [UIImage imageNamed:#"image-rope-1.png"];
}
} else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellGreenBlue" forIndexPath:indexPath];
if (is_Fingerprint_Version) {
cell.imageViewRope.image = [UIImage imageNamed:#"image-rope-2.png"];
}
}
cell.previewCellDelegate = self;
cell.tag = indexPath.row;
NSInteger leftPreviedId = [cell firstPreviewId];
self.leftPreviewedID = leftPreviedId;
UIImage *image1 = ((GalleryItemInfo *)[[GalleryDataProvider sharedInstance].itemInfo objectAtIndex:leftPreviedId]).slotPreviewImage;
UIImage *image2;
UIImage *image3;
if (leftPreviedId + 1 < [[GalleryDataProvider sharedInstance].itemInfo count])
image2 = ((GalleryItemInfo *)[[GalleryDataProvider sharedInstance].itemInfo objectAtIndex:leftPreviedId + 1]).slotPreviewImage;
if (leftPreviedId + 2 < [[GalleryDataProvider sharedInstance].itemInfo count])
image3 = ((GalleryItemInfo *)[[GalleryDataProvider sharedInstance].itemInfo objectAtIndex:leftPreviedId + 2]).slotPreviewImage;
[cell setupWithImage1:image1 image2:image2 image3:image3];
if (self.isEditModeEnabled) {
[cell showRemoveButtons];
} else {
[cell hideRemoveButtons];
}
return cell;
}
Trouble: when I scroll my collection memory usage increases every swipe from right to left on about 1 megabyte.
Why memory is not released?
Update:
CollectionViewCellPreviewTriple code (created through storyboard):
#import <UIKit/UIKit.h>
#protocol UICollectionViewPreviewCellDelegate;
#interface CollectionViewCellPreviewTriple : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIButton *buttonSlot1;
#property (weak, nonatomic) IBOutlet UIButton *buttonSlot2;
#property (weak, nonatomic) IBOutlet UIButton *buttonSlot3;
#property (weak, nonatomic) IBOutlet UIButton *buttonRemove1;
#property (weak, nonatomic) IBOutlet UIButton *buttonRemove2;
#property (weak, nonatomic) IBOutlet UIButton *buttonRemove3;
#property (weak, nonatomic) IBOutlet UIImageView *imageViewRope;
#property (nonatomic, weak) id<UICollectionViewPreviewCellDelegate> previewCellDelegate;
- (void)setupWithImage1:(UIImage *)image1 image2:(UIImage *)image2 image3:(UIImage *)image3;
- (void)showRemoveButtons;
- (void)hideRemoveButtons;
- (NSInteger)firstPreviewId;
#end
#protocol UICollectionViewPreviewCellDelegate
- (void)collectionViewPreviewCell:(CollectionViewCellPreviewTriple *)collectionViewCell didSelectSubitemWithIndex:(NSInteger)subitemIndex;
- (void)collectionViewPreviewCell:(CollectionViewCellPreviewTriple *)collectionViewCell didEditModeRequestWithStatus:(BOOL)status;
- (void)collectionViewPreviewCell:(CollectionViewCellPreviewTriple *)collectionViewCell didRemoveRequestWithIndex:(NSInteger)subitemIndex;
- (void)slotButtonRequestsShadow:(UIButton *)slotButton;
#end
Update:
- (void)setupWithImage1:(UIImage *)image1 image2:(UIImage *)image2 image3:(UIImage *)image3
{
[self.buttonSlot1 setBackgroundImage:image1 forState:UIControlStateNormal];
[self.buttonSlot1 setBackgroundImage:image1 forState:UIControlStateHighlighted];
//if (image2) {
[self.buttonSlot2 setBackgroundImage:image2 forState:UIControlStateNormal];
[self.buttonSlot2 setBackgroundImage:image2 forState:UIControlStateHighlighted];
[self.buttonSlot2 setHidden:(image2 == nil)];
//}
//if (image3) {
[self.buttonSlot3 setBackgroundImage:image3 forState:UIControlStateNormal];
[self.buttonSlot3 setBackgroundImage:image3 forState:UIControlStateHighlighted];
[self.buttonSlot3 setHidden:(image3 == nil)];
//}
}
Profiling
link for screen
This may be related to the issue that many people have been suffering with where the cell's are not reused.
To test this, you should override the method prepareForReuse and in it write a very simple log:
NSLog(#"%# is being called as expected.", NSStringFromSelector(_cmd));
You should then run your app, scroll the collection view, and check the console to see if this log appears.
If this log does not appear, you may want to check this answer for help as to how to proceed. In my app cells are not reused in the simulator, but are reused on devices. It's odd.

Custom cell not displaying PFImageView?

I have a custom PFTableViewCell using a NIB. All of my custom labels display, but my PFImageView does not. I changed everything to a UIImageView just to make sure it wasn't something wrong with my Parse call, but I couldn't get it to work even with setting a static image in my resources.
My cell's class is set as foodCell and foodcell.h is set as the owner.
Here is a screenshot showing the imageview on my NIB
Here is my foodCell.h to show the outlet of the PFImageView :
#import <Parse/Parse.h>
#interface foodCell : PFTableViewCell
#property (weak, nonatomic) IBOutlet NSObject *foodCell;
#property (weak, nonatomic) IBOutlet UILabel *priceLabel;
#property (weak, nonatomic) IBOutlet UILabel *foodDescription;
#property (weak, nonatomic) IBOutlet UILabel *foodTitle;
#property (strong, nonatomic) IBOutlet PFImageView *foodPic;
#end
Now here is where I configure my cell. Again, all of my other code here works, except the foodCell.foodPic.file :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"foodCell";
foodCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[foodCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.foodPic.file = [object objectForKey:#"foodImage"];
cell.foodTitle.text = [object objectForKey:#"foodName"];
cell.foodDescription.text = [object objectForKey:#"foodDescription"];
NSString *myString = [#"$" stringByAppendingString:[NSString stringWithFormat:#"%1#", [object objectForKey:#"foodPrice"]]];
cell.priceLabel.text = myString;
return cell;
}
As per Parse documentation, you should call loadInBackground for that PFImageView
cell.foodPic.file = [object objectForKey:#"foodImage"];
[cell.foodPic loadInBackground];
Also call [PFImageView class] in your AppDelegate.m didFinishLaunchWithOptions: method.
source: https://parse.com/questions/using-pfimageview-with-storyboard

My text doesn't show up inside UITextView

My view controller contains an UIImageView and a UITextView below. This is my code:
#import <UIKit/UIKit.h>
#interface DescriptionViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIImageView *descriptionImage;
#property (nonatomic, strong) IBOutlet UITextView *descriptionText;
#property (nonatomic, strong) NSString *text;
#property (nonatomic, strong) NSString *image;
#end
#interface DescriptionViewController ()
#end
#implementation DescriptionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.descriptionText = [[UITextView alloc] init];
self.descriptionImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.image]]];
self.descriptionText.text = self.text;
NSLog(#"%#", self.descriptionText.text);
}
#end
My code doesn't show up in textview, but I can print it using NSLog.
What could be the problem ?
You're initializing a new UITextView here.-
self.descriptionText = [[UITextView alloc] init];
This way, you get a new UITextView not linked with the main view. Remove that line and make sure descriptionText is properly linked in your xib or storyboard.
However, if your intention was actually programmatically creating a new UITextView from scratch, then you'll also have to programmatically add it to your main view.

Custom UITableViewCell doesn't appear in the table

I have created a UITableViewCell and I am trying to display it in one of my table, but for some reason it doesn't come, the table is coming empty. And when I click on the one of cell in the table it takes me down to next level, which means the custom UITableViewCell is not getting rendered properly.
Here is my custom UITableViewCell .h file
#interface ProductViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *detailLabel;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UILabel *detailLabel;
#property (nonatomic, retain) UIImageView *imageView;
Here is the .m file
#import "ProductViewCell.h"
#implementation ProductViewCell
#synthesize titleLabel, detailLabel, imageView;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
Here is the .h file which subclasses UITableViewController
#interface ProductCategoryViewController : UITableViewController {
NSArray *tableDataSource;
}
#property (nonatomic, retain) NSArray *tableDataSource;
Here is the .m file for ProductCategoryViewController
#implementation ProductCategoryViewController
#synthesize tableDataSource;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
ProductViewCell *cell = (ProductViewCell*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[ProductViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dictionary objectForKey:#"name"];
cell.imageView.image = [UIImage imageNamed:[dictionary objectForKey:#"imageUrl"]];
return cell;
}
Your help is appreciated.
Thanks,
Yogesh
I got this working by making some changes, in my tableviewcontroller I added an IBOutlet for ProductViewCell and synthesis the same.
Then I changed the xib file for ProductViewCell
1) I make File's Owner to my tableViewController
2) Then here I linked the the IBOutlet that I created in the tableViewContoller to the ProductViewCell.
I am not sure if this is the right way to do it but atleast this is working for me now

Resources