0

I have created a custom UITableView Cell class for my UITableView. However, I cannot set the values within the cell for some reason. As in the values such as userName and userImage do not show when the table is loaded. This problem has been going for a few days. Below is my code, I used dummy values for sake of simplicity. If someone can please let me know what I am doing wrong, it would be appreciated.

Thanks!

CODE

CustomCell.h

@interface CustomCell : UITableViewCell
{
    IBOutlet UILabel *userName;
    IBOutlet UIImageView *userImage;
}

@property(strong,nonatomic) IBOutlet UILabel *userName;
@property(strong,nonatomic) IBOutlet UIImageView *userImage;

@end

CustomCell.m

#import "CustomCell.h"
@interface CustomCell ()
@end

@implementation CustomCell
@synthesize userName;
@synthesize userImage;

  -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        // Custom initialization
        NSArray *nibArray= [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        self= [nibArray objectAtIndex:0];
    }
    return self;
}

-(void) setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}
@end

ViewController.m (The class with the UITableView that calls the custom cell)

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

    static NSString *CellIdentifier = @"CellIdentifier";
    CustomCell *cell=  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) 
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.userName=@"Adam Scott";
    cell.userImage=[UIImage imageNamed:@"adam.png"];
    return cell;
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Teddy13
  • 3,824
  • 11
  • 42
  • 69
  • I'd need more information to help you out. Are you building the cell in interface builder? Have you imported CustomCell.h? Here is a link to another answer I gave that may help: http://stackoverflow.com/a/19062573/1203475 – David L Oct 02 '13 at 23:52
  • Um... You need UILabels or some container to display the text. Setting the cell's variable does not display any text. – rocky Oct 02 '13 at 23:52
  • @rocky The UILabels are connected through the interface builder. Is that fine? Thanks – Teddy13 Oct 02 '13 at 23:54
  • @DavidL Yes I am building the cell through IB. I have changed the class name to CustomCell under the identity inspector. Thanks – Teddy13 Oct 02 '13 at 23:55
  • Right but the UILabel's text is never set when you set your variable. And why are you setting a string into a UILabel (userName)? And an image to a UIImageView? – rocky Oct 03 '13 at 00:00
  • have you declared reuse identifier for your custom cell ? – Соёмбо Бат-Эрдэнэ Oct 03 '13 at 01:57

4 Answers4

1

Here is what I did for a custom cell:

ReceiverCell *receiverCell = (ReceiverCell *)[tableView dequeueReusableCellWithIdentifier:@"receiverCellIdentifier"];

if (receiverCell == nil) {
    receiverCell = [[ReceiverCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"receiverCellIdentifier"];
}

receiverCell.receiverLabel.text=@"The Text";

Assuming you hooked up everything right in IB, I think you need to access the label's text using cell.labelName.text=@"Adam Scott".

David L
  • 4,347
  • 3
  • 18
  • 30
1

I would add two methods to that class:

- (void)setUserName:(NSString *)aUserName {
    [userNameLabel setText:aUserName];

    // mark view as dirty
    [self setNeedsDisplay];
}

Do the same thing for the userImage.

Then set it by calling:

[cell setUserName:@"Foo"];
rocky
  • 3,521
  • 1
  • 23
  • 31
1

You can try this. Change your lines of code

cell.userName=@"Adam Scott";
cell.userImage=[UIImage imageNamed:@"adam.png"];

to this

cell.userName.text=@"Adam Scott";
cell.userImage.image=[UIImage imageNamed:@"adam.png"];
CodeHelp
  • 1,328
  • 5
  • 21
  • 37
1

try like this

 CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if (cell == nil) {
    NSArray *top = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    for (id t in top) {
        if ([t isKindOfClass:[UITableViewCell class]]) {
            cell =(CustomCell*)t;
            break;
        }
    }
}
cell.username.text = @"blabla";
cell.userImage.image = [UIImage imageNamed:@"avatar.png"];

return cell;

}
Unheilig
  • 16,196
  • 193
  • 68
  • 98