initWithStyle:(UITableViewCellStyle) not called - ios

A weird thing just happend, i trying to build my custom tablecell but my initWithStyle is not called.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
My Tablecell looks normal:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSLog(#"xx1111");
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
How i'm trying to load the Customcell nib:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TFDCustomCell = #"TFDCell";
TFDCell *cell = (TFDCell *)[tableView dequeueReusableCellWithIdentifier:TFDCustomCell];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TFDCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[TFDCell class]])
cell = (TFDCell *)oneObject;
}
return cell;
}
But the NSLog(#"xx1111"); doenst appear in my logs. When i place a NSLog in 'setSelected' it works 'fine'

The solution was simple
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(#"initWithCoder");
self = [super initWithCoder: aDecoder];
if (self)
{
}
return self;
}

As I know if you load your view (in current case cell) from nib initWithStyle: method wont be called. Overload awakeFromNib: method instead to make custom initialization.

You're not initializing the table's cells with initWithStyle so initWithStyle of your custom cell won't be fired. But awakeFromNib will be called for your initialization call of dequeueReusableCellWithIdentifier.

Related

Tableview and custom cells unpredictable behavior

I've got a tableview controller with custom cells. For each type of cell, I created a prototype cell in the storyboard as well as a class.
Here's one of the cells:
The cell has a circular button that contains a number.
I'm trying to modify the value of the number in my cellForRowAtIndexPath method like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
TrackMilstoneCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"TrackMilstoneCell"];
if (cell == nil) {
cell = [[TrackMilstoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TrackMilstoneCell"];
}
cell.backgroundColor = cell.contentView.backgroundColor;
cell.milestoneNumber.backgroundColor = UIColorFromRGB(0xA875E1);
[cell.milestoneNumber.titleLabel setText:#"2"];
return cell;
} ...
However, I'm getting a very unpredictable behavior. Every time the tableview is reloaded I sometimes get 1 (the default in storyboard), and sometimes 2 (which is what i want).
This is the code for my (TrackMilstoneCell) class:
#import "TrackMilstoneCell.h"
#implementation TrackMilstoneCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews
{
[self viewSetup];
}
-(void)viewSetup
{
self.milestoneNumber.layer.masksToBounds = NO;
self.milestoneNumber.layer.borderColor = [UIColor whiteColor].CGColor;
self.milestoneNumber.layer.borderWidth = 4;
self.milestoneNumber.layer.cornerRadius = self.milestoneNumber.bounds.size.width / 2.0;
}
- (void)awakeFromNib
{
// Initialization code
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
The problem is with reusability, so here the best solution is reset the label in prepareForReuse method like this:
- (void)prepareForReuse {
[super prepareForReuse];
[self.milestoneNumber setTitle:#"" forState:UIControlStateNormal];
}
And while configuring the cell, set title like:
[self.milestoneNumber setTitle:#"2" forState:UIControlStateNormal];
I think you should set default stage of your button in awakeFromNib.
In your custom table view cell class:
- (void)awakeFromNib
{
// Initialization code
[super awakeFromNib];
self.milestoneNumber.titleLabel.text = #"";
}
kaushal's suggestion did the trick!
Instead of setting the title like this:
[cell.milestoneNumber.titleLabel setText:#"2"]
I did:
[cell.milestoneNumber setTitle:#"2" forState:UIControlStateNormal];
And now it's working just fine. Though I'm not sure why exactly.

UITextField delegate in custom UITableViewCell

I have custom cell prototype in my storyboard called YesNoTableViewCell. In that cell, I have UITextField.
I want to have the delegate methods of UITextField in my custom cell class.
Here is my code:
MyController
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"YesNoTableViewCell";
YesNoTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[YesNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
YesNoTableViewCell.m
#interface YesNoTableViewCell : UITableViewCell <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *yesNoInput;
#end
YesNoTableViewCell.h
#import "YesNoTableViewCell.h"
#implementation YesNoTableViewCell
#synthesize yesNoInput = _yesNoInput;
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_yesNoInput.delegate = self;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
But realized this never called:
(id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
because of:
YesNoTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
never returns nil.
Is there any suggestion for this?
it never return cell nil as you implemented
if (cell == nil){
cell = [[YesNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
so just remove this code

loadNibNamed at Base Class override the instance of Child Class

I have class hierachy with
ParentCell extends UITableViewCell
ChildCell extends ParentCell
ParentCell have separate XIB, In child cell i was creating and adding only one button to one view at ParentCell XIB. but i cant add action for this button. Because even i was creating a instance for ChildCell, that returns the instance of ParentCell
Because i use loadNibNamed to get the XIB with IBOutlet connections.
# initWithStyle method in ParentCell Class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self = [[NSBundle mainBundle] loadNibNamed:#"ParentCell" owner:self options:nil]
[0];
}
return self;
}
# initWithStyle method in ChildCell Class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.button=[[UIButton alloc] init];
[self.contentView addSubview:button];
}
return self;
}
# View Controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ChildCell ";
ChildCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[ChildCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"Cell : %#", cell); //this have instance of ParentCell instead of ChildCell
}
}
Now temporarily solved by this way
# initWithStyle method in ParentCell Class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *views = [mainBundle loadNibNamed:#"ParentCell"
owner:self
options:nil];
//Here we are linking the view with appropriate IBOutlet by their tag
self.lblTitle=[views[0] viewWithTag:100];
self.lblContent=[views[0] viewWithTag:200];
}
return self;
}
# initWithStyle method in ChildCell Class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.button=[[UIButton alloc] init];
[self.contentView addSubview:button];
}
return self;
}
But i don't know this is right approach to follow, or we have some other better approach then this..
You should use the registerNib:forCellReuseIdentifier: method at the viewDidLoad of you UITableView class.
static NSString *parentCellIdentifier = #"parentCellIdentifier";
static NSString *childCellIdentifier = #"childCellIdentifier";
[self.tableView registerNib:[UINib nibWithNibName:#"ParentCell" bundle:nil] forCellReuseIdentifier:parentCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:#"ChildCell" bundle:nil] forCellReuseIdentifier:childCellIdentifier];
(Don't forget to set the appropriate ReuseIdentifier at the XIB file)
This is the best practice, and you can get rid of your initWithStyle implementations.

UITableViewCellStyle Custom iOS 7

I'm having a bit confusion with the UITableViewCellStyle. I want to create a custom UITableViewCell like this:
But the text and the image not appear in the cell when I run the app. In Storyboard,I've put the TableView Style to 'Custom'.
What am I doing wrong?
MainTableViewController
#import "MainTableViewController.h"
#import "CustomMainCell.h"
static NSString *CellIdentifier = #"MainCell";
#interface MainTableViewController ()
//
#property(nonatomic, strong) NSArray *dataSource;
//
#property(nonatomic, strong) NSArray *iconsSource;
#end
#implementation MainTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Currículum Vitae";
// Inicializo los arrays con los datos
self.dataSource = #[#"PERFIL",#"EXPERIENCIA",#"EDUCACIÓN",#"HABILIDADES",#"INTERESES"];
self.iconsSource = #[#"perfil.png",#"experiencia.png",#"educacion.png",#"habilidades.png",#"intereses"];
// Register Class for Cell Reuse Identifier
[self.tableView registerClass:[CustomMainCell class] forCellReuseIdentifier:CellIdentifier];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Eliminio las líneas que separan las celdas
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomMainCell *cell = (CustomMainCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.title.text = self.dataSource[indexPath.row];
cell.icon.image = self.iconsSource[indexPath.row];
return cell;
}
CustomMainCell.h
#interface CustomMainCell : UITableViewCell
//
#property (weak, nonatomic) IBOutlet UILabel *title;
//
#property (weak, nonatomic) IBOutlet UIImageView *icon;
#end
CustomMainCell.m
#import "CustomMainCell.h"
#implementation CustomMainCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textLabel.textColor = [UIColor brownColor];
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
You shouldn't have:
// Register Class for Cell Reuse Identifier
[self.tableView registerClass:[CustomMainCell class] forCellReuseIdentifier:CellIdentifier];
because the cell is registered from the storyboard when the view controller is unarchived. By registering the class you are removing the archive (NIB) registration.
Additionally:
if (cell == nil) {
cell = [[CustomMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
shouldn't be required as you will always get a valid cell back (because the identifier is registered)

UITableView as subview is always empty

I have a ViewController which has several subviews (Graphs) added using function
[self.view addSubview:subView]
Now, I want to add a UITableView as subview, but when I do this, my table is always empty. Methods like numberOfSectionsInTableView, numberOfRowsInSection, etc are never called.
My table doesn't have a XIB file. This is the code from my .M file:
#import "TableSubviewViewController.h"
#implementation TableSubviewViewController
#synthesize data;
#synthesize tableFrame;
-(id)initWithStyle:(UITableViewStyle)style{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void)viewDidLoad{
[super viewDidLoad];
self.view.frame = tableFrame;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [[self.data allKeys]count];
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [[self.data allKeys]objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Some text";
return cell;
}
And this is the code in my main ViewController, in which I'm creating TableSubviewViewController:
TableSubviewViewController *tableSubView = [[TableSubviewViewController alloc]initWithStyle:UITableViewStyleGrouped];
tableSubView.tableFrame = tableFrame;
tableSubView.data = data;
[self.view addObject:tableSubView.view];
What Am I missing?
Thank you
TableSubviewViewController tableSubView must be main ViewController's global variable , can't be a local variable in the method viewDidLoad of main ViewController
U need to set datasource and delegate methods do like this
// in TableSubviewViewController.h file put a custom initilizer
#interface TableSubviewViewController : UIViewController
- (id)initTheSubviewTableVIewWithFrame:(CGRect)rect; //your custom initialiser
#end
//TableSubviewViewController.m file
#import "TableSubviewViewController.h"
#interface TableSubviewViewController ()<UITableViewDataSource,UITableViewDelegate>
#end
#implementation TableSubviewViewController
//dont use this method left blank
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialisation
}
return self;
}
//you can make custom initiliser method
- (id)initTheSubviewTableVIewWithFrame:(CGRect)rect
{
self = [super init];
if(self)
{
UITableView *subViewTable = [[UITableView alloc]initWithFrame:rect];//since u are creating the tableview programatically u need to set datasource and delegate programatically
subViewTable.dataSource = self; //add this
subViewTable.delegate = self; //this also
[self.view addSubview:subViewTable];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//datasource and delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//use as reqired
//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section{
// return 3;
//}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Some text";
return cell;
}
//in main ViewController.m controler u need to do like this
TableSubviewViewController *subViewTable = [[TableSubviewViewController alloc]initTheSubviewTableVIewWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 150)];//created the subviewTableView tableview
//set properties that u defined for tableview delegate and datasource
[self.view addSubview:subViewTable.view];
hope this helps u :)
Make sure your .h file use
and set delegate and datasource to self.
TableSubviewViewController *tableSubView = [[TableSubviewViewController alloc]initWithStyle:UITableViewStyleGrouped];
tableSubView.tableFrame = tableFrame;
tableSubView.delegate = self;
tableSubView.dataSource = self;
tableSubView.data = data;
[self.view addObject:tableSubView.view];
After that check whether array self.data have object or not. It might be possible that array is blank.

Resources