0

Please help me with UITableViewCells.

I have followed many tutorials and sites but didn't get my problem solved.

I have tried to make a simpleCustomCells app but data are not loaded in the table view.

1.I have a table view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *nib = [NSArray arrayWithArray:[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]];

    for(id obj in nib)
    {
        if([obj isKindOfClass:[UITableViewCell class]])
        {
            cell = (CustomCell *)obj;
        }
    }    
}


cell.label.text = @"hello";

return cell;
}

Also tried

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

"within if(cell==nill)"

2.I have created CustomCell : UITableViewCell with only a UILabel outlet. Changed CustomCell.xib identifier's name.

When I run this app I get this kind of error:

  Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x71627f0> setValue:forUndefinedKey:]: this class
 is not key value coding-compliant for the key label.

Please Help me with this issue, where am I going wrong.

codercat
  • 22,873
  • 9
  • 61
  • 85
user2677941
  • 38
  • 1
  • 8

4 Answers4

1

Ok, I think I got where you are getting stuck.

Step 1 : Disconnect the outlet UILabel from the xib file.

Step 2 : Go to your xib file and you will see your objects list on the left hand side of the xib file.

Step 3 : Take your mouse pointer just below where objects is written. You will find Custom Cell written.

Step 4 : Press ctrl and drag it to the label and now Connect the Outlet to label.

Step 5 : Clean your program and Run it again.

All the best. If you still have doubts, I will elaborate. :)

anky_believeMe
  • 484
  • 1
  • 4
  • 14
  • Yes I got it now. I was connecting the label by clicking right of the mouse. I did it as u said by click right of the CustomCell. It worked. Thanks – user2677941 Aug 26 '13 at 10:46
0

I think you should add like this, I hope it works

  static NSString *CellIdentifier = @"Cell";

  customcellCell *cell = (customcellCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];   <- set your customclass in this line.

  if (cell == nil)
   {
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customviewcell" owner:self options:nil];
      cell = [nib objectAtIndex:0];
   }

    cell.label.text = @"hello";

   return cell;
Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55
0

Use following way :

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

        NSString *contactCellID = [NSString stringWithFormat:@"ContactCell%d%d",indexPath.row,indexPath.section];
        contactCell = (ContactListCell *)[tableView dequeueReusableCellWithIdentifier:contactCellID];

        if(!contactCell) {
            contactCell = [[ContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactCellID];
            contactCellID = nil;
        }

        strName = @"First name";

      contactCell.lblFirstName.text = [strName capitalizedString];

      return contactCell;
    }

For Custom cell .h file

@interface ContactListCell : UITableViewCell {

    UILabel *lblFirstName;
}

@property (strong, nonatomic) UILabel *lblFirstName;
@end

For Custom cell .m file

@implementation ContactListCell

@synthesize lblFirstName;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(kxiPadFirstNameCellLandscape, kyiPadFirstNameCellLandscape, kiPadFirstNameCellWidthLandscape, kiPadFirstNameCellHeightLandscape)];
                lblFirstName.backgroundColor= [UIColor clearColor];
                lblFirstName.numberOfLines = 0;
                lblFirstName.textColor = kCustomCellFontColor;
                [self.contentView addSubview:lblFirstName];

    }
    return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
-1

Why not give you're cell an identifier and instantiate it with dequewithreusableidentifier?

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


CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[AanbodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.Label.text =@"Hello"


return cell;
}

i would also refer to this very good tutorial http://www.appcoda.com/customize-table-view-cells-for-uitableview/

jonas vermeulen
  • 1,235
  • 5
  • 23
  • 40
  • Yes I have used cell identifier as well. Still I didn't get my problem solved. – user2677941 Aug 25 '13 at 08:35
  • Yes I think label is correctly defined: @property(nonatomic, weak) IBOutlet UILabel *label; – user2677941 Aug 25 '13 at 08:49
  • The outlet connection was incorrect. I had made connection by clicking right on the label . Now I did it by clicking right on the CustomCell . Surprisingly it worked. Thanks – user2677941 Aug 26 '13 at 10:48