views:

132

answers:

2

import

I am trying to implement a custom cell to use in my tableview

This is my custom cell interface class

@interface CustomCell : UITableViewCell {
    IBOutlet UILabel *nameLabel;
    IBOutlet UITextView *inputText;
    IBOutlet UIImageView *image;
    IBOutlet UIButton *btnBuy;
}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UITextView *inputText;
@property (nonatomic, retain) IBOutlet UIImageView *image;
@property (nonatomic, retain) IBOutlet UIButton *btnBuy;

@end

This is my view controller

#import "CustomCellProjectViewController.h"

@implementation CustomCellProjectViewController

@synthesize nameLabel, inputText, image, btnBuy, listData;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    image = [[UIImageView alloc] init];
    btnBuy = [[UIButton alloc] init];
    inputText = [[UITextView alloc] init];
    nameLabel = [[UILabel alloc] init];

    NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:image, @"image", inputText, @"text", nameLabel, @"label", btnBuy, @"button", nil];

    NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];
    self.listData = array;

    [row1 release];
    [array release];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    self.listData = nil;
}


- (void)dealloc {
    [listData release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[CustomCell class]]) {
                cell = (CustomCell *)oneObject;
            }
    }
    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath{
    NSUInteger row = [indexPath row];
    if (row == 0)
        return nil;
    return indexPath;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *rowValue = [listData objectAtIndex:row];
    NSString *message = [[NSString alloc] initWithFormat:@"You selected %@", rowValue];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selection" message:message delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [alert show];
    [message release];
    [alert release];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kTableViewRowHeight;
}

@end

But I am getting an error:

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

cell is remaining nil

Can anyone help me please?

A: 

I find it very fishy the way you get the cell from the xib file. Don't know if that is the problem (what happens when you debug into that method?).

Does your CustomCell need to be in a separate xib, are you using it in different VCs? If not, you could try to stick it into the xib of your VC and then simply create an IBOutlet and hook it up to your CustomCell.

Stelian Iancu
The CustomCell is in the same project file under the CustomCell.XIB name
Lily
so in other words I put IBOutlet CustomCell *cell; in my Controller interface file?
Lily
CustomCell is of type UITableViewCell
Lily
Well, no, if you have it in a different xib then you can't just add it in the Controller class. Can you try to debug and see exactly at which step your cell doesn't load? (i.e. remains nil).
Stelian Iancu
if ([oneObject isKindOfClass:[CustomCell class]]) { cell = (CustomCell *)oneObject; } is never true
Lily
How is your XIB file setup? Are you sure you set the class of the object you're placing in the XIB to CustomCell? Is the file owner in that case supposed to do anything, or could you just pass `nil`?
filipe
nil gives an error and the xib is of type of CustomCell
Lily
what do you mean "the xib is of type CustomCell"? Are you setting the File's Owner to CustomCell?
filipe
I think I am. I am setting the file owner of type CustomCell
Lily
+1  A: 

As for me, I will write codes for CustomCell manually. Then, I can use the cell in -cellForRowAtIndexPath as following.

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    { 
        cell = [[[CustomCell alloc] initByMyWayWithIdentifier:CustomCellIdentifier] autorelease];
    }

    // assign necessary information to cell

    return cell;
}
Toro
That's all good, but the original poster wanted (or not?) to use the xib. So I was assuming that the answer to his/her question should take this into consideration.
Stelian Iancu
@Stelian: You are right. I had load cell from xib before, but finally I prefer to write codes for subclassing cell. Usually, custom cell is simple and easy to implement.
Toro