UITableView with two custom cells (multiple identifiers) - ios

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".

Related

How to add image to tableviewcell

Sorry this is such a new question, but I searched all day and still cannot find the answer. When I run the app, it shows the rows with the label and count only, but the image is not showing up on the table view cell.
#import "ViewController.h"
#interface ViewController () <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray * imageNameArray;
//__weak IBOutlet UIImageView *cell;
__weak IBOutlet UITableView *Table;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self arraySetup];
}
-(void) arraySetup {
imageNameArray = [NSMutableArray arrayWithArray:#[#"2.jpg"]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return imageNameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
//cell.imageView.image = [UIImage imageNamed:imageNameArray [indexPath.row]];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", (int) indexPath.row + 1];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)];
imv.image=[UIImage imageNamed:#"2.jpg"];
[cell.contentView addSubview:imv];
cell.imageView.image = imv.image;
return cell;
}
Here is some very basic code for you to learn from:
I'm using the basic class UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.text = #"Your table row text";
cell.imageView.image = [UIImage imageNamed:#"image.png"];// Here you specify your image
cell.detailTextLabel.text = #"Your descriptive text";
return cell;
}
By default the UITableViewCell has the UIImageView. Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", (int) indexPath.row + 1];
cell.imageView.image = [UIImage imageNamed:#"2.jpg"];
return cell;
}
Another approach is to use your own Custom TableViewCell.
add array in ViewDidLoad:
arrayValue = [[ "userEmailType": "Home", "userImage": "image1.png"], [ "userEmailType": "Home", "userImage": "image2.png"], [ "userEmailType": "Work", "userImage": "image3.png"],]
Custom UITableViewCell code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomCell! = tableView.dequeueReusableCell(withIdentifier: "CustomCellIdentifier", for: indexPath) as! CustomCell
// Static image
cell.checkedImageObj!.image = UIImage.init(named: "resort_selected")
//Want round Image....
cell.contactImage.layer.cornerRadius = cell.contactImage.frame.size.width/2
cell.contactImage.layer.masksToBounds = true
// Set array of Image
cell.contactImage.image = (arrayValue[indexPath.row] as! NSDictionary).value(forKey: "userImage") as? UIImage
}
Please try it.... Thanks
You must init the imageView at the init method of cell like:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)];
imv.tag = 10;
[cell.contentView addSubview:imv];
}
and the you can set the image by
cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d", (int) indexPath.row + 1];
UIImageView *imv = (UIImageView *)[cell.contentView viewWithTag:10];
imv.image=[UIImage imageNamed:#"2.jpg"];
return cell;
I know this is very late reply and only few people are using Objective C these days .Below is working for me . Please note that I am reusing same tableView for populating address-books and when click on each address book loading it's contacts . So you can just remove if conditions.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cells";
UITableViewCell * cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (_isPhoneBooksLoaded){
cell.imageView.image =nil;
}
if (_isContactsLoaded){
cell.imageView.image = [UIImage imageNamed:#"contact_default.png"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Important :
Do not forget to use same cell identifier you set .
Remove if conditions isPhoneBooksLoaded,isContactsLoaded and just keep
cell.imageView.image = [UIImage imageNamed:#"contact_default.png"];
Make sure you are using correct image file name and it exists in your resources .

Is it possible to have two different UiTableViewCells in one UiTableView? [duplicate]

This question already has answers here:
2 different types of custom UITableViewCells in UITableView
(3 answers)
Closed 9 years ago.
I'm having an issue and would like to know if what I am trying to do is even possible.
I have a ViewController that has a UITableView as a subview. I then loaded a custom cell and it worked fine. However, I needed another custom cell in the same tableview - this second cell has a separate nib as well.
Is this possible?
Two different custom cells in on section in a UITableView?
I did try something like this:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"ContentCell";
static NSString *CellIdentifier2 = #"SpaceCell";
// Space Cell
if (indexPath.row== 0) {
CellSpace *cell = (CellSpace *) [tableViewdequeueReusableCellWithIdentifier:CellIdentifier2];
return cell;
}
// Content cell
else {
CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
// Configure cell
return cell;
}
}
However it seems the else statement is never fired and I normally get a crash saying the method returned nil.
UPDATE
I used the advice and code here and came up with this:
#pragma mark - UITableView Delegate Methods
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 160;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 22;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier1 = #"UsernameCell";
static NSString *CellIdentifier2 = #"PasswordCell";
if(indexPath.row == 0) {
BBUsernameRegistrationCell *cell = (BBUsernameRegistrationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier1 owner:self options:nil]; // set properties
cell = nib[0];
}
// set properties
return cell;
}
// Content cell
else {
BBPasswordTableViewCell *cell = (BBPasswordTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier2 owner:self options:nil]; // set properties
cell = nib[0];
}
// set properties
return cell;
}
}
However I only see the first Cell in my tableview. If I switch the cells around the second one loads. But not both at the same time. Does anyone have any idea why?
Yes, it is possible..
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"ContentCell";
static NSString *CellIdentifier2 = #"SpaceCell";
// Space Cell
if (indexPath.row== 0) {
CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell == nil) {
cell = [[CellSpace alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
// set properties
}
// set properties
return cell;
}
// Content cell
else {
CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell == nil) {
cell = [[CellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
// set properties
}
// set properties
return cell;
}
}
in your custom cell, do something like this in initWithStyle method:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CellView" owner:self options:nil];
for (id currentObj in topLevelObjects ) {
if ([currentObj isKindOfClass:[CellView class]]) {
self = (CellView*) currentObj;
}
}
return self;
}
Yes it is possible. But before use this dequeueReusableCellWithIdentifier:. Just register your cell and corresponding nib files.
code as..
-(void)initiateCellRegisteration
{
static NSString *CellIdentifier1 = #"ContentCell";
static NSString *CellIdentifier2 = #"SpaceCell";
[self.tableView registerClass:[ContentCell class] forCellReuseIdentifier:CellIdentifier2];
[self.tableView registerNib:[UINib nibWithNibName:CellIdentifier2 bundle:nil] forCellReuseIdentifier:CellIdentifier2];
[self.tableView registerClass:[SpaceCell class] forCellReuseIdentifier:CellIdentifier1];
[self.tableView registerNib:[UINib nibWithNibName:CellIdentifier1 bundle:nil] forCellReuseIdentifier:CellIdentifier1];
}
Call this method in viewDidLoad
If you are using the cell nib then you need little tricky way to get your cell nib like
static NSString *CellIdentifier = #"ProductCell";
IVProductCell *cell = (IVProductCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"IVProductCell" owner:nil options:nil];
for (id currentObject in objects) {
if ([currentObject isKindOfClass:[IVProductCell class]]){
cell = (IVProductCell*) currentObject;
}
}
}

UITableView scrolling delay

Found The solution with help of #parcs and #payal- This link helped -> http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Basically , the table view lags when i scroll, i have gone through many post but it didn't help-
UITableView in UIScrollview scrolling delay
Delayed UIImageView Rendering in UITableView
slow scrolling of UITableView
UITableView lags while scrolling
This is the code , which i am using in cellForRowIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"WorkoutCell";
WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
cell=(WorkoutCell *)controller.view;
}
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//diplaying Workout Data
[cell setupFont];
cell.lblPoint.text=[NSString stringWithFormat:#"%#",[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"point"]];
cell.lblReps.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"reps"];
cell.lblWorkoutCategory.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"workout"];
cell.lblWeight.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"weight"];
cell.lblSets.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"sets"];
//Formatting Date
cell.lblPostedDate.text=[NSString stringWithFormat:#"Posted On: %#",[self formatDate:[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"time"]]];
return cell;
}
The Function SetupFont called from cellForRowIndexpath
-
(void)setupFont
{
self.lblPoint.font=DEFAULT_FONT(13);
self.pointLblScreen.font=DEFAULT_FONT(13);
self.lblPostedDate.font=DEFAULT_FONT(13);
self.lblReps.font=DEFAULT_FONT(13);
self.lblSets.font=DEFAULT_FONT(13);
self.lblWeight.font=DEFAULT_FONT(13);
self.lblWorkoutCategory.font=DEFAULT_FONT(18);
self.lblWorkoutCategory.strokeColor=kStrokeColor2;
self.lblWorkoutCategory.strokeSize = kStrokeBigSize;
for(UILabel *lbl in [self subviews])
{
if(lbl.tag==9)
{
lbl.font=DEFAULT_FONT(13);
}
}
}
Format Date Function called from cellForRowIndexpath
#pragma mark - Format Date Function
-(NSMutableString*)formatDate:(NSString*)date
{
NSMutableString *formattedDate=[[NSMutableString alloc]init];
NSUInteger p = 0;
NSUInteger length = [date length];
while ( p < length) {
unichar ch = [date characterAtIndex: p];
if (ch!=' ')
{
if(ch=='-')
{
ch='/';
}
NSString *appendString = [NSString stringWithFormat:#"%c",ch];
[formattedDate appendString:appendString];
p++;
}
else
{
break;
}
}
return formattedDate;
}
This is not the right way to do this:
WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
cell=(WorkoutCell *)controller.view;
}
Instead of this, subclass UITableViewCell and instantiate that cell here as follows:
WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"WorkoutCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
Also, move your setupFont method the UITableViewCell subclass.
You are typecasting the UIViewController to UITableviewCell.Instead of that take UITableViewCell class add the outlets whatever you want and use it
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:nil options:nil] objectAtIndex:0];
}
// access that CustomCell variables here
cell.name.text = #"Name";
}
You use
#interface WorkoutCell : UITableViewCell {}
then table view class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * const CellIdentifier = #"WorkoutCell";
WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if( !cell )
{
cell = [[WorkoutCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
}

How to use multiple reusable TableViewCells

I like to add multiple reusable UITableViewCells to a TableView. I am using this code to do this, but it won't work, it only shows the first cell.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
static NSString *costumeCell1 = #"Cell1";
AppDetailCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:costumeCell1];
if (!cell1) {
cell1 = [[AppDetailCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:costumeCell1];
}
return cell1;
}
if (indexPath.row == 1) {
static NSString *costumeCell2 = #"Cell2";
AppDetailCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:costumeCell2];
if (!cell2) {
cell2 = [[AppDetailCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:costumeCell2];
}
return cell2;
} else {
return nil;
}
}
If you set the reuse identifiers properly, you should change this code part
AppDetailCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:costumeCell1];
if (!cell1) {
cell1 = [[AppDetailCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:costumeCell1];
}
with this
AppDetailCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:costumeCell1 forIndexPath:indexPath];
There is no need if(!cell1), because dequeueReusableCellWithIdentifier: forIndexPath: method never returns nil, as I said before that properly reuse identifier set is important.

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