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"];
Related
I have a Popup-View which contains two labels, a tableview and button. I created a ViewController as in Display UIViewController as Popup in iPhone described.
My special requirement is now, that the tableview is not necessary in all cases, so I tried to hide it and expected a reduced height of the Popup-View. But I always get the same height. I also tried to use a UIStackView, but the height of the view wasn't changed in case of hiding the tableview.
I also need to have the view in center of the display in both cases of height.
enter image description here
#interface AuthorizationMessageViewController ()
#property (weak, nonatomic) IBOutlet UIView *messageView;
#property (weak, nonatomic) IBOutlet UILabel *titelLabel;
#property (weak, nonatomic) IBOutlet UILabel *detailsLabel;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UIButton *okButton;
- (IBAction)okButtonTouchUp:(id)sender;
#end
#implementation AuthorizationMessageViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.messageView.layer.cornerRadius = 5;
self.messageView.layer.masksToBounds = YES;
self.messageView.backgroundColor = COLOR_BACKGROUND_WHITE;
self.messageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.backgroundColor = COLOR_BACKGROUND_WHITE;
self.titelLabel.text = WHLocalizedString(#"EventHeaderAuthorization", nil);
[self setupView];
}
- (void)setupView
{
NSString *ns_messageText;
ns_messageText = #"test";
if (YES)
{
ns_messageText = #"Hide"
[self.tableView setHidden:YES];
}
else
{
ns_messageText = #"No Hide"
[self.tableView setHidden:NO];
}
self.detailsLabel.text = ns_messageText;
self.detailsLabel.textColor = COLOR_TEXT_GREY_KEY;
self.detailsLabel.numberOfLines = 0;
[self.detailsLabel sizeToFit];
}
#pragma mark - Table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 7;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 21;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"testCell"];
UILabel *weekDayLabel = (UILabel *)[cell viewWithTag:10];
weekDayLabel.text = #"weekday";
weekDayLabel.textColor = COLOR_TEXT_GREY_KEY;
weekDayLabel.font = FONT_LIGHT_SIZE_15;
UILabel *testLabel = (UILabel *)[cell viewWithTag:11];
testLabel = #"testLabel"
testLabel = COLOR_TEXT_GREY_KEY;
testLabel = FONT_LIGHT_SIZE_15;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
I hope that someone has a solution or an idea for that.
Imagine the following UI
a centered UIView that contains a UIButton and UITableView , you need to hook height constraint of the table and do this inside the popup if you want to hide it
#IBOutlet weak var heightTblCon:NSLayoutConstraint!
//
self.heightTblCon.constant = show ? 300 : 0
self.view.layoutIfNeeded()
BTW i changed color of background view for clarification purposes that should be transparent for modals
Try calling sizeToFit() on Popup-View after hiding the tableView:
popupView.sizeToFit()
Try to Adjust the Height of your view using the NSLayoutConstraint. Create an outlet for the same and manage height programatically. For example:
myConstraintOutlet.constant = 10
myTableView.layoutIfNeeded()
Here is the link for further assistant.
Regarding your second query to have the pop up always in centre you can either do it using Autolayout or programatically define the centre of your pop up.
myView.center = CGPoint(x: superView.center.x, y: superView.center.y)
Hope that helps.
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;
}
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 have a custom UITableViewCell which contains an UIImageView and two UILabel. When, I run the app, UIImageVIew get displayed but not UILabel.
Here is my Code:
ViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
titleLabel = #[#"Kiruba",#"Kiruba",#"Kiruba",#"Kiruba"];
subtitleLabel = #[#"xxx",#"xxx",#"CHN",#"CHN"];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return titleLabel.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
// NSLog(#"Text : %#",[titleLabel objectAtIndex:indexPath.row]);
cell.TitleLabel.text = [titleLabel objectAtIndex:indexPath.row];
cell.SubtitleLabel.text = [subtitleLabel objectAtIndex:indexPath.row];
cell.myImageView.image = [UIImage imageNamed:#"DSCN5478.JPG"]
return cell;
}
CustomCell.h :
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *TitleLabel;
#property (weak, nonatomic) IBOutlet UILabel *SubtitleLabel;
#property (weak, nonatomic) IBOutlet UIImageView *myImageView;
Any idea, Why the UIImageView get displayed but not UILabel?
I'm using Xcode 5 and set target IOS version as 7.
Add your UILabel and UIImageView to the contentView of your custom UITableViewCell.
Here are the ViewControllers again.
Here is my ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property (copy, nonatomic) NSDictionary *firstTableView;
#property (copy, nonatomic) NSArray *firstTableViewKey;
#property (weak, nonatomic) IBOutlet UILabel *norskLabel;
#property (weak, nonatomic) IBOutlet UILabel *infinitivLabel;
#property (weak, nonatomic) IBOutlet UILabel *presensLabel;
#property (weak, nonatomic) IBOutlet UILabel *preteritumLabel;
#property (weak, nonatomic) IBOutlet UILabel *perfektumLabel;
#end
Here is my ViewController.m:
#import "ViewController.h"
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SectionsTableIdentifier];
NSString *path = [[NSBundle mainBundle] pathForResource:#"SterkeVerb" ofType:#"plist"];
self.firstTableView = [NSDictionary dictionaryWithContentsOfFile:path];
self.firstTableViewKey = [[self.firstTableView allKeys] sortedArrayUsingSelector:#selector(compare:)];
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.firstTableViewKey count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = self.firstTableViewKey[section];
NSArray *nameSection = self.firstTableView[key];
return [nameSection count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.firstTableViewKey[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
NSString *key = self.firstTableViewKey[indexPath.section];
NSArray *nameSection = self.firstTableView[key];
cell.textLabel.text = nameSection[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0){
_norskLabel.text = #"å bake";
_infinitivLabel.text = #"zu backen";
_presensLabel.text = #"bäckt/backt";
_preteritumLabel.text = #"backte";
_perfektumLabel.text = #"hat gebacken";
}
else if (indexPath.row == 1){
_norskLabel.text = #"å motta";
_infinitivLabel.text = #"zu empfangen";
_presensLabel.text = #"empfängt";
_preteritumLabel.text = #"empfing";
_perfektumLabel.text = #"hat empfangen";
}
}
Ok, so i have my table view devided into sections(A-Z) and when i filled in the code it worked perfectly for the first section. BUT, when i pressed one of the cells in the next section, it showed the same information as the first cell in the first section. WHY?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0){
_norskLabel.text = #"å bake";
_infinitivLabel.text = #"zu backen";
_presensLabel.text = #"bäckt";
_preteritumLabel.text = #"backte";
_perfektumLabel.text = #"hat gebacken";
else if (indexPath.row == 1){
_norskLabel.text = #"å begynne";
_infinitivLabel.text = #"zu beginnen";
_presensLabel.text = #"beginnt";
_preteritumLabel.text = #"begann";
_perfektumLabel.text = #"hat begonnen";
}
}
If you set the text in tableview:didSelectRowAtIndexPath: it will just get overwritten when tableview:cellForRowAtIndexPath: is called
Inside tableview:didSelectRowAtIndexPath:, you need to access the data driving the table and change it at its source.
NSString *key = self.firstTableViewKey[indexPath.section];
NSArray *nameSection = self.firstTableView[key];
nameSection[indexPath.row] = #"New Value";