I have a custom tableview cell, when I click the "quantity button" inside the cell, the value of the cell itself resets to the value defined the prototype cell on storyboard (which is "1"). So imagine I click the button under the image of the first cell, the label of the button changes from "5" to "1". I commented out all the code when button is pressed, so assume nothing ishapening there. Why is this?
My Custom Cell (.h)
#interface BLCartItemCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *imgView;
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UIButton *quantityBtn;
#property (weak, nonatomic) IBOutlet UIButton *removeBtn;
#property (weak, nonatomic) IBOutlet UILabel *price;
#end
My Custom Cell (.m)
#implementation BLCartItemCell
#end
In my view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Section 0: Cart cells
static NSString *cartCellIdentifier = #"cartCell";
BLCartItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cartCellIdentifier];
if (cell == nil) {
cell = [[BLCartItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cartCell"];
// [cell.removeBtn addTarget:self action:#selector(removeBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
// [cell.quantityBtn addTarget:self action:#selector(quantityBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
}
// Grab the cart item from cart and set the cell properties
BLCartItem *cartItem = [self.cart.items objectAtIndex:indexPath.row];
cell.title.text = cartItem.title;
cell.quantityBtn.titleLabel.text = [NSString stringWithFormat:#"%i", cartItem.quantity];
cell.price.text = [NSString stringWithFormat:#"$ %.2f", [cartItem totalPrice]];
NSData *image_data = [[NSData alloc] initWithContentsOfURL:cartItem.image];
cell.imgView.image = [UIImage imageWithData:image_data];
// Buttons will keep track of cell index
cell.quantityBtn.tag = indexPath.row;
cell.removeBtn.tag = indexPath.row;
return cell;
I removed the IBActions associated with the button, problem still happens.
I think it has problem when you set buttonTitle like that
cell.quantityBtn.titleLabel.text = [NSString stringWithFormat:#"%i", cartItem.quantity];
try :
[cell.quantityBtn setTitle: [NSString stringWithFormat:#"%i", cartItem.quantity] forState: UIControlStateNormal]];
Read more
Text change on UIButton doesn't stick
Apple Document
Related
I'm trying to add a "favorite button" to a custom cell in a UIableView.
The cell has a label and detailLabel that show fine. I must be doing something wrong but I can't figure out what it is. My code is below, and I changed my original method (commented) after studying this question.
My code is as follows:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
}
cell.nameLabel.text = [myMutableArray valueForKey:#"Name"];
cell.detailLabel.text = [myMutableArray valueForKey:#"Detail"];
if (isFavorite)
{
NSLog(#"fave");
UIImage *btnImage = [UIImage imageNamed:#"goldStar.png"];
[cell.favoriteButton setImage:btnImage forState:UIControlStateNormal];
//[cell.favoriteButton setImage:[UIImage imageNamed:#"yellowStar.png"] forState:UIControlStateNormal];
} else {
NSLog(#"out of fave");
UIImage *btnImage = [UIImage imageNamed:#"greyStar.png"];
[cell.favoriteButton setImage:btnImage forState:UIControlStateNormal];
//[cell.favoriteButton setImage:[UIImage imageNamed:#"greyStar.png"] forState:UIControlStateNormal];
}
return cell;
}
MyCustomCell.h
#interface MyCustomCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *nameLabel;
#property (nonatomic, weak) IBOutlet UILabel *detailLabel;
#property (nonatomic, strong) IBOutlet UIButton *favoriteButton;
MyCustomCell.m
#implementation MyCustomCell
#synthesize nameLabel = _nameLabel;
#synthesize detailLabel = _detailLabel;
#synthesize favoriteButton = _favoriteButton;
- (void)awakeFromNib {
_favoriteButton = [[DBTileButton alloc] init];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
I've checked and the button is hooked up ok in IB. Thanks ahead for any advice.
UPDATE I notice in the log message that the image frame is set at 0,0,0,0. I'm attaching a shot of the custom cell IB setup. Is something amiss there?
As far as I know IBOutlet for any sub classes of UIView shouldn't be strong! Anyway you no need to use IBOutlet, if you wanna initialize your button programmatically: at first, you forgot to set frame of your button and add it to cell view via addSubview. But it is easier way: just add button on storyboard, set custom class and remove this:
_favoriteButton = [[DBTileButton alloc] init];
EDIT: set class for your storyboard's button like on picture
Then change your outlet:
#property (nonatomic, weak) IBOutlet DBTileButton *favoriteButton;
And finally don't forget to connect outlet to your button and remove programmatically initializing.
I created a UITableViewCell prototype cell on storyboard, then I link it to my ImageViewCellwhich inherit from UITableViewCell. I created IBOutlet for each control on prototype cell, it contains some UILabel and a UIImageView. When I tried to populate data into those control, UILabel work well but UIImageView always nil and won't display any image. Code:
#interface ImageTableViewCell : UITableViewCell
#property (readonly, nonatomic, strong) Post *post;
// set value for post
- (void)setPost:(Post *)post;
#end
#implementation ImageTableViewCell
{
__weak IBOutlet UILabel *titleLabel;
__weak IBOutlet UILabel *descriptionLabel;
__weak IBOutlet UILabel *pointLabel;
__weak IBOutlet UIImageView *imageView;
}
- (void)setPost:(Post *)post
{
_post = post;
// update ui
titleLabel.text = self.post.title;
descriptionLabel.text = self.post.descriptions;
pointLabel.text = self.post.point;
[imageView setImage:[UIImage imageNamed:#"mylocalimage.png"]];
}
#end
In my controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(feedData == nil || [feedData count] == 0)
{
return nil;
}
NSDictionary *postData = [feedData objectAtIndex:indexPath.row];
// prepare data to pass to table view cell
Post *post = [[Post alloc] init];
post.title = [postData objectForKey:#"title"];
post.descriptions = [postData objectForKey:#"description"];
post.total_likes = [postData objectForKey:#"total_likes"];
post.total_shares = [postData objectForKey:#"total_shares"];
post.image = [postData objectForKey:#"image"];
ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ImageCell"];;
if(cell == nil)
{
cell = [[ImageTableViewCell alloc] init];
}
[cell setPost:post];
return cell;
}
All the UILabel work well, but UIImageView's image never been set, I tried to set it with a local image but still not work, after of some investigation, I found out that at the time I set image for imageView, it always nil. So how can I overcome this?
Update
Linking image
Try changing the variable name of your UIImageView to something else than "imageView". The reason for this is that UITableViewCell itself has a property named "imageView". So when you are setting the property it must try to set the superclass' property which is not linked to your outlet hence the result.
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.
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
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]]];
}