Loading a UIImageView subclass from a custom cell - ios

I am not sure if this is a Storyboard Bug. I have created a project with a custom cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HomeGameTurnCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
The custom cell has some image views. One of the image views is a subclass.
#interface HomeTableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet RoundedProfilePicture *profilePictureImageView;
#property (strong, nonatomic) IBOutlet UIImageView *turnThumbnailImage;
#property (strong, nonatomic) IBOutlet UILabel *usernameLabel;
#property (strong, nonatomic) IBOutlet UILabel *lastPlayedLabel;
#end
The RoundedProfilePicture subclass simply has the following:
-(id)init {
NSLog(#"%s",__PRETTY_FUNCTION__);
self = [super init];
if (self) {
[self setupView];
}
return self;
}
- (void)setupView
{
NSLog(#"%s",__PRETTY_FUNCTION__);
self.clipsToBounds = YES;
self.layer.cornerRadius = self.bounds.size.width / 2;
self.layer.borderWidth = 3;
self.layer.borderColor = [UIColor darkGrayColor].CGColor;
}
What I am finding is that the RoundedProfilePicture methods are not being called. Within the storyboard I have setup one prototype cell and the correct identifier. I also have the image view set as the correct Custom class. But it doesn't seem to take effect, is there something I am missing/could check?

When you are reusing an element you should override UITableViewCell method:
-(void)prepareForReuse
instead of init. It will called each time.
If you want one time initialization you should do it in: initWithStyle:reuseIdentifier:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//do your stuff here
}
return self;
}
From apple docs:
If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

The issue is that when a UIimageView is subclassed and called from the storyboard a different method is called for the init.
-(id)initWithCoder:(NSCoder *)aDecoder {
// As the subclassed UIImageView class is called from storyboard the initWithCoder is overwritten instead of init
NSLog(#"%s",__PRETTY_FUNCTION__);
self = [super initWithCoder:aDecoder];
if (self) {
[self setupView];
}
return self;
}
This resolves the issue of the rounded profile view not being called.

Related

Reuse custom TableViewCell

I have a viewController that contains a programmatically created tableView.
#property (strong, nonatomic) UITableView *numbersTableView;
//...
self.numbersTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width-20, 50)];
self.numbersTableView.delegate = self;
self.numbersTableView.dataSource = self;
self.numbersTableView.tag = 1;
self.numbersTableView.layer.cornerRadius = 5;
[self.numbersTableView setBounces:false];
[self.numbersTableView setHidden:true];
[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:#"NumbersTableViewCell"];
[self.view addSubview:self.numbersTableView];
For the prototype cell I want to use a prototype that I created somewhere else in another viewController and I designed It in storyboard.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.tag == 1) { //tableView == self.numbersTableView
NSString *cellID = #"NumbersTableViewCell";
AZGCountryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[AZGCountryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.countryName.text = self.numbersArray[indexPath.row].number;
cell.flagImageView.image = [UIImage imageNamed:self.numbersArray[indexPath.row].country];
return cell;
}
}
And my custom cell contains one UIImageView and one UILabel but when I run the code the cells are empty and I can see the UIImageView and UILabel are nil!
#import <UIKit/UIKit.h>
#interface AZGCountryCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *flagImageView;
#property (strong, nonatomic) IBOutlet UILabel *countryName;
#property (strong, nonatomic) IBOutlet UILabel *countryCode;
#end
#import "AZGCountryCell.h"
#implementation AZGCountryCell
#synthesize flagImageView;
#synthesize countryName;
- (void)awakeFromNib {
[super awakeFromNib];
self.flagImageView.layer.cornerRadius = self.flagImageView.frame.size.width/2;
self.flagImageView.layer.masksToBounds = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
Then what should I do to have properly filled cells in my numbersTableView?
Unfortunately you can't reuse a table cell you've defined in a table view in a storyboard in your separate class.
In order for you to be able to share your cell between the different views, you'll have to extract the view to a separate nib and register that nib go be used on both tables.
Alternatively you can implement the cell's UI in code in the table view cell subclass.
You getting empty cell because custom tableview cell not registered, use
[self.numbersTableView registerNib:[UINib nibWithNibName:#"AZGCountryCell" bundle:nil] forCellReuseIdentifier:#"NumbersTableViewCell"];
instead of
[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:#"NumbersTableViewCell"];

Empty custom prototype cell

I'm new in iPhone development and have problem with prototype cell.
In my storyboard, I have navigation controller. I pushed it with view controller (as main window), it has a button, when I click it I open tableView controller with custom prototype cell
- (IBAction)searchClick:(id)sender {
CNCarTableController *car = [[CNCarTableController alloc]init];
[self.navigationController pushViewController:car animated:TRUE];
}
But when it opens it has empty rows
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: ( NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TransportCell";
CNTransportCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(cell == nil){
cell = [[CNTransportCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.textLabel.text = #"bmv";
cell.name = (UILabel*) [cell viewWithTag:100];
[cell.name setText:#"a text"];
cell.number.text = #"number";
cell.car.text = #"car";
return cell;
}
But if will use cell.textLabel.text = #"bmv"; all works ok. But for custom cell way with tags or cell.lbl.text don't work. What is wrong there? Can it be a nuance of navigation controller with view controller?
Custom cell code:
#interface CNTransportCell : UITableViewCell
#property (nonatomic,weak) IBOutlet UILabel *name;
#property (nonatomic,weak) IBOutlet UILabel *number;
#property (nonatomic,weak) IBOutlet UILabel *car;
#end
#implementation CNTransportCell
#synthesize name;
#synthesize number;
#synthesize car;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
you have done small mistake there. Your custom cell class is "CNPlayerCell" & you are allocating new cells from "CNTransportCell" class.
One more thing, have you assigned custom class to your prototype cell in IB? (In identity inspector)

Accessing the IBOutlets's properties in the custom UITableViewCell with an IBAction

I'm a newbie in objective-c and iOS
I created several custom table cells with outlets in it. But I'm not sure if I use the right way.
What I want to do is to get the property of an Outlet which is inside the custom cell, by an IBAction defined in the table view controller.
I have a custom table cell called KolonNumberCell, and a UISlider in it.
I want to define an IBAction called playNumbers in the UITableViewController. The slider seems to work fine in the custom cell. I can get its value.
But I cannot figure out how to get the value of this slider when the playButton outlet is tapped.
I would be so grateful if you could give me some information or show me a way.
Thanks in advance
TableController.h
#import <UIKit/UIKit.h>
#class KolonNumberCell;
#class BankoNumberCell;
#interface TableViewController : UITableViewController{
NSArray *bundleArray;
IBOutlet KolonNumberCell *kolonCell;
IBOutlet BankoNumberCell *bankoCell;
UIButton *playButton;
}
#property (strong) IBOutlet KolonNumberCell *kolonCell;
#property (strong) IBOutlet BankoNumberCell *bankoCell;
#property (nonatomic,retain) NSArray *bundleArray;
#property (nonatomic,retain) IBOutlet UIButton *playButton;
- (void)playNumbers:(id)sender;
#end
part of TableController.m
#import "TableViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize bundleArray,kolonCell,bankoCell,playButton;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *bundle1 = [[[NSBundle mainBundle] loadNibNamed:#"KolonNumberCell" owner:self options:nil] objectAtIndex:0];
NSArray *bundle2 = [[[NSBundle mainBundle] loadNibNamed:#"BankoNumberCell" owner:self options:nil] objectAtIndex:0];
bundleArray = [[NSArray alloc] initWithObjects:bundle1,bundle2, nil];
}
- (void)playNumbers:(id)sender
{
NSLog(#"button");
// Get the value of the slider in KolonNumberCell... but how?
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
tableView.allowsSelection = NO;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [bundleArray objectAtIndex:indexPath.section];
}
return cell;
}
#end
One of my customtableviewcell implementation file
#import "KolonNumberCell.h"
#implementation KolonNumberCell
#synthesize label,slider;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
self.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"doubleRound.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0]];
}
- (IBAction)sliderChanged:(id)sender
{
[label setText:[NSString stringWithFormat:#"%i",(int)slider.value]];
}
+ (NSString *)reuseIdentifier {
return #"Cell";
}
#end
Your design is odd because your code will become really messy with more Cells.
Matt Gallagher has a nice implementation for Custom TableViews. He is using the TableViewCell as a Controller and puts all the logic related to cell in the cells class. The discussion thread on Stack Overflow:
CustomTableViewController
If you still want to do it your way:
Give your Slider a tag: slider.tag = x //Make sure that the tag is unique
In PlayNumbers:
UISlider *slider = (UISlider *)[[[bundleArray objectAtIndex:0] contentView] viewWithTag:x];
NSLog(#"%i",(int)slider.value);
Edit: thanks for correcting the syntax

Custom UITableViewCell not showing content

I'm having major issues trying to get a custom UITableViewCell show any custom labels/controls. I have a similar project using same code and it works perfectly...the only difference is I use the table cell in a UITableViewController, as opposed to the problem page where I use the table cell in a UITableView embedded in a UIViewController. . Some searching of stackoverflow has suggested the following issues, all of which I have set up correctly:
1) CellIdentifier in tableView:cellForRowAtIndexPath: matches the storyboard identifier
2) All IBOutlets are connected properly
I suspect it may be something to do with the way I load the page (I do lots of page loading in viewDidLayoutSubviews method as I use storyboard autolayout and so I need access to frame sizes etc which isn't available in viewDidLoad)
Any suggestions?
Andy
EDIT: By the way it works perfectly if I use a standard UITableViewCell and instead of my custom label just use the cell.textLabel.text = #"..." etc. It seems to be something related to my custom cell.
viewController.h
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UIView *flipContainerView;
...
#end
viewController.m
#implementation ViewController
#synthesize tableView = _tableView;
#synthesize flipContainerView = _flipContainerView;
...
- (void)viewDidLayoutSubviews {
self.tableView.frame = CGRectMake((self.flipContainerView.frame.size.width / 2) - (self.flipContainerView.frame.size.width / 2),
(self.flipContainerView.frame.size.height / 2) - (self.flipContainerView.frame.size.height / 2),
self.flipContainerView.frame.size.width,
self.flipContainerView.frame.size.height);
...
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"List Cell";
ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.testLabel.text = nil;
cell.testLabel.text = #"test value";
return cell;
}
ListCell.h
#interface ListCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *testLabel;
#end
ListCell.m
#import "ListCell.h"
#implementation ListCell
#synthesize testLabel = _testLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end

custom cell not appear in tableview

I use storyboards. I create new class for my Cell, in storyboard I write in Reuse Identifier "userFullCell". My Cell class .h
#import <UIKit/UIKit.h>
#interface UsefullLinksCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *image;
#property (weak, nonatomic) IBOutlet UILabel *shortTitle;
#end
.m default
#import "UsefullLinksCell.h"
#implementation UsefullLinksCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
property image and shortTitle connected in storyboard with UILabel and UIImageView in Cell
Then in my class with UITableView I import "UseFullLinksCell.h" and in viewDidLoad wrote:
[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:#"userFullCell"];
tableView is my outlet:
#property (weak, nonatomic) IBOutlet UITableView *tableView;
and then I try to create cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"userFullCell";
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *info = resourceArray[indexPath.row];
if(info){
cell.image.image = info[#"logo"];
cell.shortTitle.text = info[#"title"];
}
return cell;
}
So, cell initialize, but image and text not assigned, and after going end of method return 0x0000000 for cell. If I create for standart cell code in this method, I mean UITableViewCell *cell, there is all good. Where I could make mistake?
PS Sorry I can not give screenshots from storyboard, because I write from not my computer, if you want, I provide images later
You should register the xib file (with registerNib:forCellReuseIdentifier:) where you made your custom cell, not the class. Also, I don't know if this will make a difference or not, but replace this:
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
With this (notice the forIndexPath: at the end of the method):
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
After Edit:
If added a custom cell to your table view in the storyboard, and gave it the identifier "userFullCell", then you don't need to register anything -- in fact, registering your class makes it not work. So, just comment out that register statement, and see if that works.
You have missed something:
NSArray *objects=[[NSBundle mainBundle]loadNibNamed:#"UsefullLinksCell" owner:self options:nil];
UsefullLinksCell *cell =[objects objectAtIndex:0];
this will give you the object of your custom cell.
I think you have to do the following
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
image=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50.0,50.0)];
shortTitle=[[UILabel alloc] initWithFrame:CGRectMake(60.0,0.0,200.0,50.0)];
}
return self;
}
Else all should work.. The thing is your components are not initialized anywhere, either it should be loaded from xib or, you have to initialize them manually.
Hope this helps.

Resources