UIImageView outlet of Custom TableViewCell nil - ios

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.

Related

UILabel unrecognized selector sent to instance

So I have a custom UITableViewCell with 3 UILabels. Each of them the text is set label.text = #"something";. But when I scroll past (lets say cell 0) and then scroll back up to it, the app crashes with -[__NSCFString setText:]: unrecognized selector sent to instance with the line where I set the text highlighted red. Also all the other cells (that use the same re-usable cell) do not have that label populated. In the UITableViewCell subclass I have the properties set with strong.
Class with UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ChampionDetailSpellCell *cell = [tableView dequeueReusableCellWithIdentifier:#"championAbilityCell" forIndexPath:indexPath];
// Performance
cell.layer.rasterizationScale = [[UIScreen mainScreen] scale];
cell.layer.shouldRasterize = YES;
// Design
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = nil;
// Reset
cell.abilityIcon.image = nil;
cell.abilityNameLabel.text = nil;
cell.abilityCost.text = nil;
cell.abilityRange.text = nil;
cell.abilityDescription.text = nil;
// Content
if (indexPath.row == 0) {
// Passive
// Icon
[DDragon getChampionPassiveIcon:self.championInfo[#"passive"][#"image"][#"full"] :^(NSURL *championPassiveIconURL) {
[cell.abilityIcon setImageWithURL:championPassiveIconURL];
}];
// Name
cell.abilityNameLabel.text = self.championInfo[#"passive"][#"name"];
// Tooltip
cell.abilityDescription.text = self.championInfo[#"passive"][#"sanitizedDescription"];
} else {
// Ability
// Image
[DDragon getChampionAbilityIcon:self.championInfo[#"spells"][indexPath.row - 1][#"image"][#"full"] :^(NSURL *championAbilityIconURL) {
[cell.abilityIcon setImageWithURL:championAbilityIconURL];
}];
// Name
cell.abilityNameLabel = self.championInfo[#"spells"][indexPath.row - 1][#"name"];
// Cost
cell.abilityCost.text = [NSString stringWithFormat:#"%# %#", self.championInfo[#"spells"][indexPath.row - 1][#"costBurn"], self.championInfo[#"spells"][indexPath.row - 1][#"costType"]];
// Range
cell.abilityRange.text = self.championInfo[#"spells"][indexPath.row - 1][#"rangeBurn"];
// Tooltip
cell.abilityDescription.text = self.championInfo[#"spells"][indexPath.row - 1][#"sanitizedTooltip"];
}
return cell;
}
UITableViewCell subclass
#interface ChampionDetailSpellCell : UITableViewCell
#property (strong) IBOutlet UIImageView *abilityIcon;
#property (strong) IBOutlet UILabel *abilityNameLabel;
#property (strong) IBOutlet UILabel *abilityCost;
#property (strong) IBOutlet UILabel *abilityRange;
#property (strong) IBOutlet UITextView *abilityDescription;
#end
I see this wrong :
// Name
cell.abilityNameLabel = self.championInfo[#"spells"][indexPath.row - 1][#"name"];
I think it should be :
// Name
cell.abilityNameLabel.text = self.championInfo[#"spells"][indexPath.row - 1][#"name"];

Programatically creating the custom UICollectionViewCell and setting the properties

I've been trying to configure a custom UICollectionViewCell that is supposed to present a UIView consisting of a few subviews generated with a custom method.
The problem is that when I try to pass the needed data from -cellForItemAtIndexPath to the cell's properties (NSStrings) the app crashes and the data doesn't seem to be passed at all, because NSLog returns (null) for all of the properties.
I thought that the problem was with the properties not being initialized, but when I initialized them in -initWithFrame, they didn't return any values. I tried to use the -initWithCoder and -awakeFromNib and it didn't work.
I also tried to utilize a custom method that would set the properties for me. While it seems to be working, I can't seem to find a way to add the generated view to the cell's view, as using [self.contentView addSubview:...] seems to be adding every generated view to every cell in the Collection View.
If you guys could help me out, I would be much grateful - I've spent my entire day on this, and I just can't get it to work. Perhaps I should do it differently?
My custom collection view cell's .h file:
#import "SHDesignChicago.h"
#interface SHDesignChicagoCollectionViewCell : UICollectionViewCell
#property (strong, nonatomic) NSString *name;
#property (strong, nonatomic) NSString *date;
#property (strong, nonatomic) NSString *going;
#property (strong, nonatomic) PFFile *background;
#property (strong, nonatomic) UIView *generatedDesign;
- (void)setCellWithEventName:(NSString *)name eventDate:(NSString *)date eventGoing:(NSString *)going eventBackground:(PFFile *)background;
#end
And my custom collection view cell's .m file:
#import "SHDesignChicagoCollectionViewCell.h"
#implementation SHDesignChicagoCollectionViewCell
- (void)setCellWithEventName:(NSString *)name eventDate:(NSString *)date eventGoing:(NSString *)going eventBackground:(PFFile *)background {
UIImage *backgroundImage = [[UIImage alloc] initWithData:[background getData]];
NSLog(#"Event details:\nName: %#\nDate: %#\nGoing: %#\nBackground: %#", name, date, going, background);
self.generatedDesign = [SHDesignChicago designWithName:name date:date going:going background:backgroundImage frame:self.frame];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//self.autoresizesSubviews = YES;
__block UIImage *backgroundImage;
[self.background getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) backgroundImage = [[UIImage alloc] initWithData:data];
}];
NSLog(#"Event details:\nName: %#\nDate: %#\nGoing: %#\nBackground: %#", self.name, self.date, self.going, self.background);
if (self.name != nil) {
UIView *generatedDesign = [SHDesignChicago designWithName:self.name date:self.date going:self.going background:backgroundImage frame:self.frame];
[self addSubview:generatedDesign];
}
}
return self;
}
#end
Also the cellForItemAtIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PFObject *event = [self.events objectAtIndex:indexPath.row];
NSString *name = event[#"eventName"];
NSDate *date = event[#"eventDate"];
PFFile *background = event[#"coverPhoto"];
if ([event[#"designId"] isEqual: #(1)]) {
// SHDesignChicago
SHDesignChicagoCollectionViewCell *chicagoCell = (SHDesignChicagoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"chicago" forIndexPath:indexPath];
/*
chicagoCell.name = #"TEST";
chicagoCell.date = #"TODAY, 5 PM";
chicagoCell.going = #"NATALIA KAWECKA AND 35 OTHERS ARE GOING";
chicagoCell.background = background;
*/
[chicagoCell setCellWithEventName:name eventDate:#"TODAY, 5 PM" eventGoing:#"NATALIA M AND 35 OTHERS ARE GOING" eventBackground:background];
return chicagoCell;
}
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
return cell;
}

Why won't this UIButton image show in a custom UITableViewCell?

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.

UIButton Label Resets When Clicked Inside A TableViewCell

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

How to stop reusing uitableviewCell?

Hi in my app i have to load images to UITableViewCell .which are coming from the server.so the problem is when ever i scroll the tableview images are loading every time and its loading lazy so i tried to stop reusing cells but its not working ,How to stop reusing the cells in UITableView.
my code is
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
RestaurantCustomCell *cell =(RestaurantCustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *topLevelObjects ;
topLevelObjects= [[NSArray alloc]init];
if(cell==nil)
{
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RestaurantCustomCell" owner:self options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (RestaurantCustomCell *) currentObject;
break;
}
}
}
NSDictionary *dict=[restauarantsArr objectAtIndex:indexPath.row];
NSString *imgStr=[dict valueForKey:#"Img"];
[self processImageDataWithURLString:imgStr andBlock:^(NSData *imageData) {
if (self.view.window)
{
UIImage *img = [UIImage imageWithData:imageData];
if(img!=nil)
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[img drawInRect:CGRectMake(0,0,100,100)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
cell.restaurantImg.image=newImage;
UIGraphicsEndImageContext();
}
}
}];
cell.nameLbl.text=[dict valueForKey:#"Restaurants"];
cell.typeLbl.text=[dict valueForKey:#"Rest_Cuisine"];
cell.timingsLbl.text=[dict valueForKey:#"Rest_Timings"];
cell.categoryLbl.text=[dict valueForKey:#"Rest_Category"];
cell.addressLbl.text=[dict valueForKey:#"Address"];
return cell;
}
You can not doing this in default table view cell. If you want to create this kind of things then you need to create custom tableview cell anbd do it what you want.
You have to make a custom cell like that:
RestaurantCustomCell.h
#import <UIKit/UIKit.h>
#interface RestaurantCustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *nameLbl;
#property (weak, nonatomic) IBOutlet UILabel *typeLbl;
#property (weak, nonatomic) IBOutlet UILabel *timingsLbl;
#property (weak, nonatomic) IBOutlet UILabel *categoryLbl;
#property (weak, nonatomic) IBOutlet UILabel *addressLbl;
#property (weak, nonatomic) IBOutlet UIImageView *restauranImg;
#end
RestaurantCustomCell.m
#import "RestaurantCustomCell.h"
#implementation RestaurantCustomCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)prepareForReuse{
self.nameLbl.text = #"";
self.typeLbl.text = #"";
self.timingsLbl.text = #"";
self.categoryLbl.text = #"";
self.addressLbl.text = #"";
}
#end
after in IB you add a new UITableViewCell at your UITableView and the class of it with you new custom cell set the Identify ID like "RestaurantCustomCell" add your labels and imageView to your custom cell and connect the Outlet, then you modify you tableView:cellForRowAtIndexPath: like that:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"RestaurantCustomCell";
RestaurantCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dict=[restauarantsArr objectAtIndex:indexPath.row];
NSString *imgStr=[dict valueForKey:#"Img"];
[self processImageDataWithURLString:imgStr andBlock:^(NSData *imageData) {
if (self.view.window)
{
UIImage *img = [UIImage imageWithData:imageData];
if(img!=nil)
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[img drawInRect:CGRectMake(0,0,100,100)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
cell.restaurantImg.image=newImage;
UIGraphicsEndImageContext();
}
}
}];
cell.nameLbl.text=[dict valueForKey:#"Restaurants"];
cell.typeLbl.text=[dict valueForKey:#"Rest_Cuisine"];
cell.timingsLbl.text=[dict valueForKey:#"Rest_Timings"];
cell.categoryLbl.text=[dict valueForKey:#"Rest_Category"];
cell.addressLbl.text=[dict valueForKey:#"Address"];
return cell;

Resources