1

I am doing an exercise about tableView and tableViewCell. And I am having some problems with custom view cells.

I have created my own custom tableViewCell with .xib file called as newCellView.xib. I have required .h and .m files and I choose super class as UITableViewCell.

In my view controller called as TableViewController I was creating a table with default cells like this.

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

   cell.textLabel.text = [showNames objectAtIndex:[indexPath row]];
   cell.imageView.image = [UIImage imageNamed:[iconArray objectAtIndex:[indexPath row]]];

 return cell;
}

Once I have created my own custom view. I imported newViewCell.h into my viewController and I updated the code like this.

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{

static NSString *newCellViewString = @"newCellView";

newCellView *cell = [tableView dequeueReusableCellWithIdentifier:newCellViewString];

 //newCellView *cell = [[newCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newCellView];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newCellView" owner:self options:nil];
    cell = (newCellView *)[nib objectAtIndex:0];
}

But when I run the app I see a blank page, like I have no related view. Maybe I forgot some connections to add. I can't even see empty cells to view. Only a white blank page. Any help would be great. Thanks.

EDIT FOR OTHER FILES

Here are my .h and .m files.

#import <UIKit/UIKit.h>

@interface newCellView : UITableViewCell <UITableViewDelegate>

@property (nonatomic, strong) IBOutlet UILabel *nameLabel;
@property (nonatomic, strong) IBOutlet UIImageView *iconImageView;
@property (retain, nonatomic) IBOutlet UIButton *extendButton;

@end

.m file

#import "newCellView.h"

@implementation newCellView

@synthesize nameLabel;
@synthesize extendButton;
@synthesize iconImageView;

- (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
}

- (void)dealloc {
[extendButton release];
[nameLabel release];
[iconImageView release];
[super dealloc];
}
@end
erenkabakci
  • 442
  • 4
  • 15

4 Answers4

4

When dealing with custom cell, I'm always trying to encapsulate everything related to the cell in a UITableViewCell subclass (including the NIB / outlets / ..) and use the tableView registerClass: forCellReuseIdentifier: method to tell my table which class to use for its cells.

In your example to do so you could:

In your newCellView.m, add the nib loading in the cell init:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newCellView" owner:self options:nil];
    self = [nib objectAtIndex:0];

}
return self;
}

Make sure all Outlets connections are correct. (i.e. your UITableViewCell in your Nib is of class NewCellView,..)

Then in the viewDidLoad of your controller you tell your table to use newCellView for its cells:

[yourTableView registerClass:[newCellView class] forCellReuseIdentifier:@"newCellView"];

Finally in the cellForRowAtIndexpath:

newCellView *cell = [tableView dequeueReusableCellWithIdentifier:@"newCellView"];

if (cell == nil)
{
    cell = [[NewCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newCellView]";
}
Eric Genet
  • 1,260
  • 1
  • 9
  • 19
2

I think, the problem with your question is that the custom cells are not attached to the custom class cell. When you create a UITableViewCell class, it does not create a xib along with it. Then you need to create a xib file, that needs to be attached to the custom class file.

Create an EMPTY NIB file, with no content. Then add a uitablecustomcell through the objects, and when you do add the new object, GO TO File Inspector and in File Name enter the name newCellView. Now the custom cells will display EMPTY rows.

Now, add several views to the custom cell and attach those views via IBOutlets created in .h files, namely nameLabel, iconImageView, extendButton.

Hitesh
  • 542
  • 3
  • 12
0

This is a simple error I have encountered before. You forgot to typecast. it should be:

cell = (newCellView*)[nib objectAtIndex:0];

If this does not resolve your issue make sure you have your xib file set to use the "newCellView" class and not the default "UITableViewCell" class. Also, if you created a tableview manually and added it as a subview of another view or set it as your view in the loadView method or similar, rather than subclassing UITableViewController make sure you set the frame for the tableview and that you added it as a subview.

You may want to remove the from your newCellView.h class. The delegate for the tableview should not be a view or a subclass of one. Especially not the cell that the tableview will be presenting. The UITableViewController should be receiving the delegate methods.

ksealey
  • 1,698
  • 1
  • 17
  • 16
  • actually it was like that before i forgot to edit again. thanks but still not working. – erenkabakci Jul 26 '13 at 07:38
  • how to set the frame as subview ? – erenkabakci Jul 26 '13 at 07:48
  • Im not sure what you mean. Do you mean how do you set the Frame? you should only have to if you manually created an instance of a tableView. If you did you can set the frame by using [tableview setFrame:[UIScreen mainScreen].bounds]; – ksealey Jul 26 '13 at 07:56
0

I found the answer. Everything was working good in my code except the positioning of the cell view's.

Project was using autolayout as a default property. So whenever i see the white page actually cells were out of bounds. I disabled autolayout and set movement properties of the items from size inspector.

Thanks for efforts.

erenkabakci
  • 442
  • 4
  • 15