How do I change attributes on a custom cell? - ios

I know this is simple, but I can't find a simple answer! I have a regular UITableView and I just added a text view to a custom cell. I'm not sure how to access this textview in cellForRowAtIndexPath. I tried making it an iboutlet, but it doesn't show up when I do cell.??!?!?!?. The reuse is set also. Any ideas?
Edit: I also did all the connections in ib
In the .h file...
#property(nonatomic, strong) IBOutlet UITextView *myTextView;
In the .m file...
#synthesize myTextView = _myTextView;
static NSString *CellIdentifier = #"all_table_reuse_identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"%s", __PRETTY_FUNCTION__);
}
The problem is I should be able to do cell.myTextView, but it's not there

For Example
static NSString *cellIdentifier= #"Cell";
CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell=[[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil]objectAtIndex:0];
}
cell.myTextView.text=#"your text";

Do like this,
YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell. myTextView.text = #"My TextView";
return cell;

May be this is what you want.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Why so serious?";
return cell;
Edit: For custom cell
In .h file
#import <UIKit/UIKit.h>
#interface TestCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextView *tv;
#end
In .m
#import "TestCell.h"
#implementation TestCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void) setFrame:(CGRect)frame
{
float inset = 20.0f;
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
#end
In CellForRowAtIndexPath
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.tv.text = #"This is textview";
return cell;
Hope this helps... :)

Related

Centering Image In UITableView

I am having a lot of problems centering an image in my UITableViewCell. I have spent countless hours trying all the solutions listed on stack overflow as well as countless other websites. My text will center, but for some reason my image won't center and there is a white line on the left side of the table that I can not get rid of either.(From what I have read in the iOS documentation, this is new in iOS 7, but people have gotten it to go away) I have set all the insets to zero both programmatically and via the inspector. I am using core data to retrieve the images, which is working fine, it is just my image won't center.
Here is my method for the tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]];
UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:#"image"]];
cell.imageView.image = cellImage;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
Along with that, I can't seem to get my label to go above the image, which is a whole other problem. But what I have done to try to fix this is make a custom cell subclass, which didn't work. I am all out of resources and ideas to fix this.
EDIT ABOUT CUSTOM SUBCLASS
So I might have the wrong idea of a custom subclass of UITableViewCell. But what I did was create a class, with two variables, one for the UIImageView and the other for the label, in a class called customCell. Then in the main table view I called them like this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
//cell.textLabel.text = [NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]];
//UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:#"image"]];
//cell.imageView.image = cellImage;
//cell.textLabel.textAlignment = NSTextAlignmentCenter;
//cell.textLabel.textColor = [UIColor whiteColor];
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[customCell.cellTitle setText:[NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]]];
[customCell.cellImage setImage:[UIImage imageWithData:[moment valueForKey:#"image"]]];
return cell;
}
Then simply in the custom cell class I just connected the label and image to an IBOutlet and synthesized them.
EDIT 2 CUSTOM SUBCLASS ADDITION
The header file has two properties. One for cellTitle which is a UILabel and one for cellImage which is a UIImageView.
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *cellImage;
#property (weak, nonatomic) IBOutlet UILabel *cellTitle;
The implementation file:
Here I am just synthesizing the two objects.
#synthesize cellImage;
#synthesize cellTitle;
- (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
}
CustomCell *customCell = (CustomCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
[customCell.cellTitle setText:[NSString stringWithFormat:#"%#",
[moment valueForKey:#"name"]]];
[customCell.cellImage setImage:
[UIImage imageWithData:[moment valueForKey:#"image"]]];
return cell;
I believe you're returning the default cell, not the customCell
To your cell subclass make a method
- (void)layoutSubviews {
self.cellTitle.frame = CGRectMake(...); //self.view.frame will return the cell's frame
self.cellImage.frame = CGRectMake(...); //you should set the frames of title and imageView however you want them in here
}
(void)layoutSubviews {
[Self.cellImage setCentre:Self.contentView.Centre];}
After a long day of trying to figure this out, I had one little mistake that caused this whole debacle. I had my cell Identifier set to "CellIdentifier" in the inspector. I changed it to "Cell" and edited some code and boom!
I added some code to the CustomCell:
-(void)layoutSubviews {
self.imageView.center = self.contentView.center;
[super layoutSubviews];
}
Which fixed everything. No more bar on the right side and both title and image are centered. Here is the final code in the TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
/*cell.textLabel.text = [NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]];
UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:#"image"]];
cell.imageView.image = cellImage;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];*/
[cell.cellImage setImage:[UIImage imageWithData:[moment valueForKey:#"image"]]];
[cell.cellTitle setText:[NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]]];
return cell;
}
I still have commented out code in there, sorry about that. But thank you to all whole helped out in solving this problem!
DONT USE THE DEFAULT IMAGEVIEW FROM THE CELL... create a UIImageView... add it to cell's view at the desired position.. =)
GL HF
you can make custom cell for table view and load this cell in table view as you can place your contents in custom cell at desired position whatever you want.This is the best approach to make table view cell if your application requires complex design in table view.Example:static NSString *CustomCellIdentifier = #"CEll";//[NSString stringWithFormat:#"Cell%d",indexPath.row];
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; implement this in your cellforrowAtIndexPath method and add your cell nib -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self = (customCell *)[[[NSBundle mainBundle] loadNibNamed:#"customCell" owner:self options:nil] firstObject];}
}

I am unable to set a label in TableViewCell

I'm just trying to test out the custom cell by have all the cells in the table say "Hi", but it won't work... the cells are all blank. It seems that the TheCell class is called before I can set the label text... I don't know how else to do it.
TheCell.h
#import <UIKit/UIKit.h>
#interface TheCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UILabel *arrivalLabel;
#end
TheCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.arrivalLabel.frame = CGRectMake(0, 0, 320, 30);
[self.contentView addSubview:self.arrivalLabel];
}
return self;
}
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"anIdentifier";
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TheCell alloc] init];
cell.arrivalLabel.text = #"hi";
return cell;
}
You have a few issues. You are calling the init method of your cell class. You should be calling the initWithStyle:reuseIdentifier: method instead.
You should also only allocate the cell if the call to dequeue... returns nil.
Then you need to make sure that your cell's initWithStyle:reuseIdentifier: method is creating the label instance for the arrivalLabel property.
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.arrivalLabel.text = #"hi";
The other big problem is in your cell's initWithStyle:reuseIdentifier: method. You never create the label.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_arrivalLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[self.contentView addSubview:_arrivalLabel];
}
return self;
}
This is not correct:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[TheCell alloc] init];
You have to do something like:
TheCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[TheCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

I can't make corners of images and buttons rounded

I'm creating the cells of a UITableViewController.
In one of them, a small image is contained. I tried the following to make its corners rounded:
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
In another cell prototype, I tried to make the corners of a button rounded:
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
In both cases I included
#import <QuartzCore/QuartzCore.h>
The button and the image whose corners must be rounded are property of the UITableViewController owning them:
#import <UIKit/UIKit.h>
#interface profileRegistrationCellVC : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *profileImage;
#property (weak, nonatomic) IBOutlet UIButton *chooseImageFromRoll;
#property (weak, nonatomic) IBOutlet UIButton *shootImage;
#end
In relative .m class:
#import "profileRegistrationCellVC.h"
#import <QuartzCore/QuartzCore.h>
#implementation profileRegistrationCellVC
#synthesize profileImage;
#synthesize chooseImageFromRoll;
#synthesize shootImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
shootImage.layer.cornerRadius = 8;
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
profileImage.layer.borderColor = [UIColor whiteColor].CGColor;
profileImage.layer.borderWidth = 20.0;
[self addSubview:profileImage];
[self addSubview:chooseImageFromRoll];
[self addSubview:shootImage];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Here's the code of my function cellForRowAtIndexPath, in my uitableviewcontroller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"profileRegistrationCell";
profileRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[profileRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.profileImage.image.layer.masksToBounds = YES;
//cell.profileImage.layer.cornerRadius = 5.0;
return cell;
}
else if (indexPath.section == 1) {
static NSString *CellIdentifier = #"regularRegistrationCell";
regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.regularFieldName.text = [_registrationItems objectAtIndex:indexPath.row];
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Email*"])
cell.regularTextField.keyboardType = UIKeyboardTypeEmailAddress;
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Età"]) {
cell.regularTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
return cell;
}
else{
static NSString *CellIdentifier = #"orientationRegistrationCell";
orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.fieldLabel.text = [_registrationItems objectAtIndex:[_registrationItems count]-1];
cell.orientationLabel.text = #"Non specificato";
return cell;
}
}
But in no case I managed to make corners rounded. Can you tell where I'm mistaking?
Thanks
The code snippets above are both correct and have no issues. My assumption is that your issue lies elsewhere in creating the button that contains the image. The following code will create a button and will round it's corners, also I added the border to it in case you want to add that too. You can plug in the image you have in there as well. I have added a image of what this code will create for your reffrence also. Hope it will help you out.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
Below is the image of the button that is related with the above code
Edit:
another way of doing the round corners is to use the method masksToBounds here is an example of it within the generic cell right out of the box from the template.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
cell.imageView.image = [UIImage imageNamed:#"acolon.png"];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
return cell;
}
here is the screen shot of the result:
i know you using a custom cell, so implement the maskToBount in the cellForRowAtImdexPath or wherever you are populating the tableview with its custom cells.

UITableView with two custom cells (multiple identifiers)

I am trying to put a cell as a space between each cell - which will be hidden by setting alpha = 0. In my table, the space cells will be for rows that are odd.
Note that the actual cell height is 85, but the hidden cell height (ie space between cells) is 20.
The problem is that the space cell height is 85, but not 20, I don't know why. Maybe the cell is not loaded correctly.
Cell here is the UITableViewCell - the actual cell - with identifier 'Cell'.
Cell2 is the space with identifier 'Space'.
Each class above has its own UITableViewCell class and the XIB files are also assigned to each of them. The identifier is also set in the IB for each Xib.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"Cell";
static NSString *CellIdentifier2 = #"Space";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(!cell)
{
NSArray *ar = [[NSBundle mainBundle] loadNibNamed:#"CellView" owner:nil options:nil];
for (id obj in ar)
{
if ([obj isKindOfClass:[Cell class]])
{
cell = (Cell *)obj;
break;
}
}
}
if (indexPath.row % 2 == 1)
{
Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (!cell2)
{
NSArray *ar = [[NSBundle mainBundle] loadNibNamed:#"Cell2" owner:nil options:nil];
for(id obj in ar)
{
if([obj isKindOfClass:[Cell2 class]])
{
cell2 = (Cell2 *)obj;
break;
}
}
// Method 1
cell2 = [[Cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
// Method 2
//cell2 = [[Cell2 alloc] init];
// Method 3
//cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
[cell2.contentView setAlpha:0];
// prevent selection and other stuff
[cell2 setUserInteractionEnabled:NO];
}
return cell2;
}
else
{
// Configure the actual cell
}
return cell;
}
* I've renamed some of your NIB/Class names for a better understanding. *
First, you should register each cells' NIB:
- (void)viewDidLoad{
[super viewDidLoad];
static NSString *CellIdentifier1 = #"ContentCell";
static NSString *CellIdentifier2 = #"SpaceCell";
UINib *nib = [UINib nibWithNibName:#"CellViewNIBName" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1];
nib = [UINib nibWithNibName:#"CellSpaceNIBName" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2];
self.contentView.hidden = YES;
[self loadData];
}
Because you have the NIBs registered, dequeueReusableCellWithIdentifier: will always return a cell:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"ContentCell";
static NSString *CellIdentifier2 = #"SpaceCell";
// Space Cell
if (indexPath.row % 2 == 1) {
CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
return cell;
}
// Content cell
else {
CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
// Configure cell
return cell;
}
}
Last, but not least, make sure to implement the following delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Space cell's height
if (indexPath.row % 2 == 1) {
return 20.0f;
}
// Content cell's height
else {
return 80.0f;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *returncell;
static NSString *cellIdentifier ;
if(indexPath.section == 0)
{
cellIdentifier = #"cell1";
}
else if (indexPath.section == 1)
{
cellIdentifier = #"cell2";
}
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
MapTableViewCell *myCustomCell = (MapTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(indexPath.section == 0)
{
if(myCell == nil)
{
myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
getLocationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
getLocationBtn.frame = CGRectMake(myCell.frame.origin.x,myCell.frame.origin.y+5 , 200, 30);
[getLocationBtn setTitle:#"your button title" forState:UIControlStateNormal];
[getLocationBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[getLocationBtn addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}
[myCell.contentView addSubview:getLocationBtn];
returncell = myCell;
}
else if (indexPath.section == 1)
{
if (myCustomCell == nil)
{
myCustomCell = [[MapTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
myCustomCell.nearbyLocation.text = #"demo Text";
returncell = myCustomCell;
}
return returncell;
}
//mycustom tablviewcell
import "MapTableViewCell.h"
#implementation MapTableViewCell
#synthesize nearbyLocation;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
self.backgroundColor = [UIColor groupTableViewBackgroundColor];
nearbyLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 30)];
[self addSubview:nearbyLocation];
}
return self;
}
#end
Best way to use number of custom cells with default cell
In addition for the answers provided, I want to emphasize on the Cell Identifier for each different custom cells must be different too.
For example custom cellA with identifier "Cell" and custom cellB with identifier "Cell2".

iOS subclassing tableviewcell returns nil

I am trying to subclass a UITableViewCell for my table but the cell keeps on returning nil. Anyone know why? Thanks.
TableView Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSLog(#"Nil = YES");
}
cell.callType = currentCall.currentCallType;
return cell;
}
SubClass Code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
UIView *myContentView = self.contentView;
label.text = callType;
label.textColor = [UIColor blackColor];
[label addSubview:myContentView];
}
return self;
}

Resources