I have this code segment:
if (cell == nil)
{
CGRect cellFrame = CGRectMake(0,0,300,250);
cell = [[UITableViewCell alloc] initWithFrame:cellFrame
reuseIdentifier:CellTableIndetifier];
CGRect nameLabelRect = CGRectMake(0, 5, 70, 20);
UILabel* nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.text = #"Name";
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview: nameLabel];
CGRect colorLabelRect = CGRectMake(0, 25, 70, 20);
UILabel* colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];
colorLabel.textAlignment = NSTextAlignmentCenter;
colorLabel.text = #"Color";
colorLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview: colorLabel];
CGRect priceLabelRect = CGRectMake(0, 45, 70, 20);
UILabel *priceLabel = [[UILabel alloc] initWithFrame:priceLabelRect];
priceLabel.text = #"Price";
priceLabel.textAlignment = NSTextAlignmentCenter;
colorLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:priceLabel];
CGRect nameValueRect = CGRectMake(80, 5, 200, 20);
UILabel* nameValue = [[UILabel alloc] initWithFrame: nameValueRect];
nameValue.tag = kNameValueTag;
[cell.contentView addSubview:nameValue];
CGRect colorValueRect = CGRectMake(80, 25, 200, 20);
UILabel* colorValue = [[UILabel alloc] initWithFrame:colorValueRect];
colorValue.tag = kColorValueTag;
[cell.contentView addSubview:colorValue];
CGRect priceValueRect = CGRectMake(80, 45, 200, 20);
UILabel *priceValue = [[UILabel alloc] initWithFrame:priceValueRect];
priceValue.tag = kPriceValueTag;
[cell.contentView addSubview:priceValue];
}
and I would like to make that make that into a subclass, so I don't have to write all those lines, I just say cell = CustomCell and it does everything in the subclass.
Here is the basic code for a subclass of UITableCellView :
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
{
}
#end
-----------------------------------------------------------
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
}
/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}*/
#end
It is auto-generated if you create a new file of type Objective-C Class and specify UITableViewCell in filed subclass of
Following is what I usually do. If you use the cell only in 1 view controller, you can just put it in the same file as the view controller.
#interface MyCell : UITableViewCell
#property (strong, nonatomic) UILabel* nameValue;
#property (strong, nonatomic) UILabel* colorValue;
#property (strong, nonatomic) UILabel* priceValue;
#end
#implementation MyCell
-(id)init {
self = [super initWithStyle:whatever_style];
// Create & position UI elements
UILabel* nameLabel = [[UILabel alloc] init];
nameLabel.frame = .... // frame, font, etc
[self.contentView addSubview:nameLabel]
self.nameValue = [[UILabel alloc] init];
self.nameValue = .... // frame, font, etc
[self.contentView addSubview:self.nameValue];
// Do the same thing for color, price
return self;
}
#end
By exposing nameValue, colorValue, priceValue, I allow them to be changed from outside (ie the UITableViewController). I didn't expose other labels because they are static. Unless you need special positioning, you don't have to override layoutSubviews. autoresizingMask is sufficient in most cases.
There is two way I use to solve this.
The "quick and dirty" is to design a UITableViewCell into your UITableView with the stuff you need (UILabel, UIImageView,...) and set a unique tag for each element, then when you dequeue a UITableViewCell you can reuse the elements in like this :
UILabel *nameLabel = (UILabel*)[cell viewWithTag:NAME_LABEL_TAG];
if(!nameLabel) {
// If the label does not exist, create it
CGRect nameLabelRect = CGRectMake(0, 5, 70, 20);
nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.text = #"Name";
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview: nameLabel];
}
Or the (imo) best way is to create a custom UITableViewCell and subclass UItableviewCell, you have a good tutorial there : Custom UITableViewCell
I guess your are putting this stuff in your cellForRowAtIndexPath: delegate method and I can see why your strive to remove it from this place.
Create a new Objective-C class via New->File and put the subview related calls you posted in the layoutSubviews: method. In cellForRowAtIndexPath: in your table view delegate now use this class instead of the generic UITableViewCell. Don't forget to import your newly created file.
#import "CellVideo.h"
#implementation CellVideo
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(#"initWithCoder");
self = [super initWithCoder: aDecoder];
if (self)
{
// Initialization code
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] init];
[moviePlayer.view setFrame:CGRectMake(10, 75, 300, 260)];
[moviePlayer.view setBackgroundColor:[UIColor blackColor]];
[moviePlayer.view setTag:333];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayer.scalingMode = MPMovieScalingModeFill;
_movie=moviePlayer;
UIImageView *imagrViewThumb=[[UIImageView alloc]initWithFrame:CGRectMake(10, 75, 300, 260)];
[imagrViewThumb setBackgroundColor:[UIColor redColor]];
[imagrViewThumb setTag:333];
[self.contentView insertSubview:imagrViewThumb atIndex:0];
}
return self;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
///use it in this way
CellIdentifier=#"cellvideo";
UITableViewCell *cell=nil;
// CellVideo *cellVideo=nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Related
If I use this code to indent from left, everything is indented (separator + content)
table.contentInset = UIEdgeInsetsMake(0, 20, 0, 0);
Same if I use this:
cell.layoutMargins = UIEdgeInsetsMake(0, 20, 0, 0);
This doesn't help neither, because it doesn't move the image.
cell.indentationLevel = 10;
You can use a CustomTableCell to get what you want:
At first, in ViewController:
#import "ViewController.h"
#import "MyTableCell.h"
#interface ViewController ()
#property UITableView *tableView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
_tableView.backgroundColor = [UIColor grayColor];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (nil == cell) {
cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
//Update data for cell
return cell;
}
#end
This is MyTableCell.h:
#import "MyTableCell.h"
#interface MyTableCell()
#property UIView *containerView;
#property UIView *separationLine;
#end
#implementation MyTableCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
_containerView = [[UIView alloc] init];
_containerView.backgroundColor = [UIColor redColor];
[self addSubview:_containerView];
_separationLine = [[UIView alloc] init];
_separationLine.backgroundColor = [UIColor blackColor];
[self addSubview:_separationLine];
return self;
}
-(void)layoutSubviews{
_containerView.frame = CGRectMake(20, 0, self.frame.size.width-20, self.frame.size.height-1);
_separationLine.frame = CGRectMake(10, self.frame.size.height-1, self.frame.size.width-10, 1);
}
#end
And this is the screenshot for the code:
You can modify the code in "layoutSubviews" as your wish.
Hope it can help you.
I have a UIButton subclass which customizes a button. Then I have a UITableViewCell subclass which uses this button and position it in the cell. Finally I have a subclass of UITableViewController which displays the table view and contains the customized cell. My problem is that the customized button "starbtn" won't show up in the table, while everything else is working properly. What is wrong in my code?
favButton.h
#import <UIKit/UIKit.h>
#interface favButton : UIButton {
BOOL _checked;
}
#property (nonatomic, setter = setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
#end
favButton.m
#import "favButton.h"
#implementation favButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
[self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"startbefore.jpg"];
[self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
}
else
{
[self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"startafter.jpg"];
[self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
}
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
imageCellCell.h
#import <UIKit/UIKit.h>
#import "favButton.h"
#interface imageCellCell : UITableViewCell
#property (nonatomic, strong) UIView *view;
#property (nonatomic, strong) UILabel *label1;
#property (nonatomic, strong) UILabel *label2;
#property (nonatomic, strong) UIImageView *prodimage;
#property (nonatomic, strong) UIImageView *thumbsup;
#property (nonatomic, strong) UILabel *label3;
#property (nonatomic, strong) UIImageView *basket;
#property (nonatomic, strong) UIButton *homebtn;
#property (nonatomic, strong) favButton *starbtn;
#end
imageCellCell.m
#import "imageCellCell.h"
#implementation imageCellCell
#synthesize view;
#synthesize label1;
#synthesize label2;
#synthesize prodimage;
#synthesize thumbsup;
#synthesize label3;
#synthesize basket;
#synthesize homebtn;
#synthesize starbtn;
- (instancetype)init {
self = [super init];
self.starbtn = [[favButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startbefore.jpg"]
forState:UIControlStateNormal];
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate image
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
// initiate label1 score value
label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)];
label1.textColor = [UIColor greenColor];
[[self label1] setFont:[UIFont systemFontOfSize:25]];
// initiate label2 score label
label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)];
// initiate image
thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)];
// initiate label3 manufacturer
label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)];
// initiate image
basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)];
// initiate home button
homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)];
[homebtn setTintColor:[UIColor clearColor]];
[homebtn setBackgroundImage:[UIImage imageNamed:#"home.jpg"]
forState:UIControlStateNormal];
[view addSubview:prodimage];
[view addSubview:label1];
[view addSubview:label2];
[view addSubview:thumbsup];
[view addSubview:label3];
[view addSubview:basket];
[view addSubview:homebtn];
[view addSubview:starbtn];
}
return self;
}
#end
ProdViewController.m
#import "ProdViewController.h"
#import "imageCellCell.h"
#interface ProdViewController ()
#end
#implementation ProdViewController
#synthesize tableview;
#synthesize tabbar;
#synthesize addfav;
#synthesize favData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
favData = [[NSMutableArray alloc] init];
[tableview setDataSource:self];
[tableview setDelegate:self];
}
- (void)viewDidUnload
{
[self setTableview:nil];
[self setTabbar:nil];
[self setAddfav:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier;
NSString *CellIdentifierimg;
UITableViewCell *cell;
if (cell == nil) {
if (indexPath.row == 0) {
cell = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
} else if (indexPath.row == 1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}else if (indexPath.row == 2) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}else if (indexPath.row == 3) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}else if (indexPath.row == 4) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
switch ([indexPath row])
{
case 0:
{
imageCellCell *firstRowCell = (imageCellCell *)cell;
[firstRowCell.thumbsup setImage: [UIImage imageNamed:#"thumbs-up-green.jpg"]];
[firstRowCell.prodimage setImage: [UIImage imageNamed:#"test1.jpg"]];
[firstRowCell.label1 setText:#"10"];
[firstRowCell.label2 setText:#"Score"];
[firstRowCell.label3 setText:#"Name"];
[firstRowCell.basket setImage: [UIImage imageNamed:#"Basket.jpg"]];
// reference of the home button to the buttonclick method
[firstRowCell.homebtn addTarget:self action:#selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
// reference of the favorites button to the buttonclick method
[firstRowCell.starbtn addTarget:self action:#selector(clickFavButton:) forControlEvents:UIControlEventTouchUpInside];
firstRowCell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case 1:
{
cell.textLabel.text = #"Row 2";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case 2:
{
cell.textLabel.text = #"Row 3";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
case 3:
{
cell.textLabel.text = #"Row 4";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
break;
}
}
return cell;
}
-(IBAction)clickFavButton:(id)sender{
[favData addObject:#"productname"];
}
#end
why u are adding the starbtn in init method, put that code in - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
comment out entire init method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate image
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
// initiate label1 score value
label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)];
label1.textColor = [UIColor greenColor];
[[self label1] setFont:[UIFont systemFontOfSize:25]];
// initiate label2 score label
label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)];
// initiate image
thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)];
// initiate label3 manufacturer
label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)];
// initiate image
basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)];
// initiate home button
homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)];
[homebtn setTintColor:[UIColor clearColor]];
[homebtn setBackgroundImage:[UIImage imageNamed:#"home.jpg"]
forState:UIControlStateNormal];
//initilise hear
//edit
starbtn = [[favButton alloc]init];
starbtn.frame = CGRectMake(243,0, 30, 30)];
//edit
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startbefore.jpg"]
forState:UIControlStateNormal];
[view addSubview: starbtn]; //add this
[view addSubview:prodimage];
[view addSubview:label1];
[view addSubview:label2];
[view addSubview:thumbsup];
[view addSubview:label3];
[view addSubview:basket];
[view addSubview:homebtn];
[view addSubview:starbtn];
}
return self;
}
Edited Answer
in your "favButton.h" file
#import <UIKit/UIKit.h>
#interface favButton : UIButton
{
BOOL _checked;
}
#property (nonatomic, setter = setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
#end
in "favButton.m" file
#import "favButton.h"
#implementation favButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
// [self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"22.jpg"];
// [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
[self setImage:[UIImage imageNamed:#"22.jpg"] forState:UIControlStateNormal];
}
else
{
// [self setTintColor:[UIColor clearColor]];
UIImage* img = [UIImage imageNamed:#"33.jpg"];
// [self setImage:img forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected|UIControlStateDisabled];
[self setImage:[UIImage imageNamed:#"33.jpg"] forState:UIControlStateNormal];
}
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
in imageCellCell.h file
include only these methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
// Initialization code
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate image
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
// initiate label1 score value
label1 = [[UILabel alloc] initWithFrame:CGRectMake(230,80,150,20)];
label1.textColor = [UIColor greenColor];
[[self label1] setFont:[UIFont systemFontOfSize:25]];
// initiate label2 score label
label2 = [[UILabel alloc] initWithFrame:CGRectMake(232,50,150,20)];
// initiate image
thumbsup = [[UIImageView alloc]initWithFrame:CGRectMake(240,108, 30, 30)];
// initiate label3 manufacturer
label3 = [[UILabel alloc] initWithFrame:CGRectMake(90,200,180,20)];
// initiate image
basket = [[UIImageView alloc]initWithFrame:CGRectMake(280,1, 30, 30)];
// initiate home button
homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)];
[homebtn setTintColor:[UIColor clearColor]];
[homebtn setBackgroundImage:[UIImage imageNamed:#"home.jpg"]
forState:UIControlStateNormal];
starbtn = [[favButton alloc]initWithFrame:CGRectMake(243,0, 30, 30)];
starbtn.backgroundColor = [UIColor greenColor];
// starbtn.frame = CGRectMake(243,0, 30, 30);
//edit
[starbtn setTintColor:[UIColor clearColor]];
[starbtn setBackgroundImage:[UIImage imageNamed:#"startbefore.jpg"]
forState:UIControlStateNormal];
[view addSubview: starbtn]; //add this
[view addSubview:prodimage];
[view addSubview:label1];
[view addSubview:label2];
[view addSubview:thumbsup];
[view addSubview:label3];
[view addSubview:basket];
[view addSubview:homebtn];
[view addSubview:starbtn];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
in controller all are same no need to change
End Edit
Hope this helps u .. :)
CustomCell.h
#interface CustomCell : UITableViewCell
//Properties created in Code, not via IB.
#property (nonatomic, strong) UILabel *labelUsername;
#property (nonatomic, strong) UIView *circle;
//Properties Created through IB by control+drag
#property (nonatomic, strong) UILabel *labelFirstName;
#property (nonatomic, strong) UILabel *labelLastName;
#end
CustomCell.m
#implementation CustomCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//Creating properties in code
self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
[self.circle setBackgroundColor:[UIColor brownColor];
self.labelUsername = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
self.labelUsername.textColor = [UIColor blackColor];
[self.contentView addSubview:self.labelUsername];
[self.contentView addSubview:self.circle];
}
return self;
}
tableView.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inviteCellIdentifier];
}
cell.labelFirstName.text = #"FirstName";
cell.labelLastName.text = #"LastName";
return cell;
}
The code above continued to show labelUsername and circle. However, the properties created using the IB (labelFirstName and labelLastName) did not appear.
So in tableView.m in viewDidLoad I registered the Nib with the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"CustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:#"CustomCell"];
}
Now labelFirstName and labelLastName appear, but the properties created with code (labelUsername and circle) DO NOT appear.
How can I get all 4 properties to show?
Try this in CustomCell.m,
- (void)awakeFromNib
{
[super awakeFromNib];
self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
[self.circle setBackgroundColor:[UIColor brownColor];
self.labelUsername = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
self.labelUsername.textColor = [UIColor blackColor];
[self.contentView addSubview:self.labelUsername];
[self.contentView addSubview:self.circle];
}
Try this after self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
NSArray *views = [[NSBundle mainBundle]loadNibNamed:#"yourNibName" owner:self options:NULL];
[self addSubview:[views lastObject]];
Disclaimer: I'm new comparably new to iOS development. I'm using ARC for this project
I have a very simple custom UITableViewCell
UITableViewListRightAlignedCell.h
#interface UITableViewListRightAlignedCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *lblTitle;
#property (weak, nonatomic) IBOutlet UIImageView *imgIcon;
#end
UITableViewListRightAlignedCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self initalize];
}
return self;
}
-(void)awakeFromNib {
[super awakeFromNib];
[self initalize];
}
-(void) initalize {
self.lblTitle.backgroundColor = [UIColor clearColor];
self.lblTitle.textAlignment = UITextAlignmentRight;
self.lblTitle.font = [UIFont fontWithName:FONTRobotoRegular size:16];
self.lblTitle.textColor = RGBColor(0x5D350BFF);
}
This custom cell works fine when I have a cell in my storyboard and I connect UILabel to lblTitle and an UIImage to imgIcon.
However, I want to use the same custom cell class somewhere else in the code with a programmatically created UITableView. The problem is that the properties lblTitle and imgIcon are not being set because they are not initialized and I can't initialize them because them because are they are outlets and weak.
I need to know what's the correct approach in this case.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createMyLabel:nil];// create your label here
// Initialization code
[self initalize];
}
return self;
}
-(void)awakeFromNib {
[super awakeFromNib];
[self initalize];
}
-(void) initalize {
//self.lblTitle.backgroundColor = [UIColor clearColor];
self.lblTitle.textAlignment = UITextAlignmentRight;
self.lblTitle.font = [UIFont fontWithName:FONTRobotoRegular size:16];
self.lblTitle.textColor = RGBColor(0x5D350BFF);
}
-(void)createMyLabel:(id)sender{
//Create your label
UILabel *MyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
//Set background color for debugging
[storeAddress setBackgroundColor: [UIColor blueColor]];
//set this created label as your lblTitle label
[self setLblTitle:MyLabel];
//Add it to the cell's content view
[[self contentView] addSubview:[self lblTitle]];
//Then do something with that
//[self initalize];
//[MyLabel release]; // For Non ARC
}
Each table view cell contains contentView. This view is what you see in IB. When you add controls to it in IB (UILabel in your case), you can use them as weak because this contentView already has strong reference to it. So what you need:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[label setText:#"Test"];
[cell.contentView addSubview:label];
[cell setTitleLabel:label];
You have to know that when using a Storyboard, only awakeFromNib is called. initWithStyle is called only in the case of a programmatical initialization by another class.
Therefore, you are correct when using this kind of code :
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self initalize];
}
return self;
}
-(void)awakeFromNib {
[super awakeFromNib];
[self initalize];
}
-(void) initalize {
// initialization code
}
This is exactly what Paul Hegarty shows in the Stanford iOS course. It ensures that whatever the caller, your class is initialized in a consistent way.
So, to answer your question, if you want to use this class programmatically, add this kind of code ONLY in the initWithStyle method (since it's the one being called programmatically) :
_lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.contentView addSubview:_lblTitle];
Same thing for your other property.
Of course, you don't add that to the awakeFromNib method, since your outlets will already be initialized by the storyboard itself.
Regards,
Fred
I am using this code for multiple cell types in a UITableView
The problem is that the cell text is invisible. The code for cellForRowAtIndexPath as well as the cell class code is given below:
code:
static NSString *kCellIdentifier = #"NewsViewControllerTableCell";
static NSString *kCellIdentifier2 = #"SubscribeCell";
if ((indexPath.row==0) && ([[NSUserDefaults standardUserDefaults] boolForKey:#"subscribeButtonOption"]))
{
SubscribeCell* cell = (SubscribeCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier2];
if (cell == nil) {
cell = [[[SubscribeCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35.0) reuseIdentifier:kCellIdentifier2] autorelease];
cell.contentView.backgroundColor = kColorR53G53B53;
cell.subscribeLabel.font = kLucidaSansStdFontBold_14;
cell.subscribeLabel.textColor = [UIColor whiteColor];
}
cell.subscribeLabel.textColor=[UIColor redColor];
cell.subscribeLabel.text = #"+ SUBSCRIBE TO NEWSLETTER";
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor =kColorR53G53B53;
[cell setNeedsDisplay];
return cell;
}
else
{
//another cell
}
=========
header:
#import <UIKit/UIKit.h>
#interface SubscribeCell : UITableViewCell{
UILabel *subscribeLabel;
}
#property(nonatomic, retain) UILabel *subscribeLabel;
#end
and implementation class:
#import "SubscribeCell.h"
#implementation SubscribeCell
#synthesize subscribeLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
subscribeLabel.textColor=[UIColor whiteColor];
self.backgroundColor=kColorR53G53B53;
}
return self;
}
Check to see if subscribeLabel is nil. You're creating it in initWithNibName:bundle: but are initializing with initWithFrame:reuseIdentifier:, so it's not reaching your label creation code.
If I try to compile your code, I get an error message stating that UITableViewCell does not declare a method called 'initWithNibName: bundle:'. You should use the proper initialization method 'initWithStyle: reuseIdentifier:'. You also forget to add the subscribeLabel to the contentView of the cell.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
subscribeLabel.textColor=[UIColor whiteColor];
[self.contentView addSubview:subscribeLabel];
self.backgroundColor=kColorR53G53B53;
}
return self;
}