Sulaiman Majeed
Sulaiman Majeed

Reputation: 499

Writing to a label when creating a custom cell Xcode

I keep running into a problem when I try to write to a label in a custom cell. So right now, I have a class with a table view called "AccountViewController" and I have a class with my custom cell "AccountViewCell. I can get my cell to correctly display in my table, however I run into a problem when I try to link the label in my custom cell to their outlets and when I write to the labels.

Right now my code is:

AccountViewController.h

#import <UIKit/UIKit.h>
#import <GameKit/GKSession.h>
#import <GameKit/GKPeerPickerController.h>

@interface AccountViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{

    NSArray     *tableData;
    int         cellValue;
    NSString    *cellContents;

}

@property (nonatomic, retain) NSArray   *tableData;
@property (nonatomic, retain) NSString  *cellContents;
@property (nonatomic, retain) NSString  *accountSelected;
@property (nonatomic, retain) NSString  *accountList;
@property (nonatomic, retain) NSString  *amount;

@end

AccountViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

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

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

    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AccountViewCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }

    // cell.accountNameLabel.text = [tableData objectAtIndex:indexPath.row];

    return cell;

}

AccountViewCell.h:

#import <UIKit/UIKit.h>

@interface AccountViewCell : UITableViewCell


@property (nonatomic, weak) IBOutlet UILabel *accountNameLabel;
@property (nonatomic, weak) IBOutlet UILabel *accountNumberLabel;
@property (nonatomic, weak) IBOutlet UILabel *balanceLabel;

@end

AccountViewCell.m

#import "AccountViewCell.h"

@implementation AccountViewCell

@synthesize accountNameLabel = _accountNameLabel;
@synthesize accountNumberLabel = _accountNumberLabel;
@synthesize balanceLabel = _balanceLabel;

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

@end

Again, my code works fine and displays my custom cell when I don't try to set a label's text or connect the outlets to the labels in AccountViewCell as shown here:

https://i.sstatic.net/lCRV6.png

My cell Identifier is correct: https://i.sstatic.net/0ODt1.png

Now, the problem is when I connect the outlets like this: https://i.sstatic.net/bIMm8.jpg

The code will break, even if I don't set or declare the labels in AccountViewController.m It gives me this error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AccountViewController 0x104b2220> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key accountNameLabel.'
*** First throw call stack:
(0x248e012 0x1d2be7e 0x2516fb1 0x815711 0x796ec8 0x7969b7 0x7c1428 0xd620cc 0x1d3f663 0x248945a 0xd60bcf 0xd6298d 0x28e54 0xbfff4b 0xc0001f 0xbe880b 0xbf919b 0xb9592d 0x1d3f6b0 0x287ffc0 0x287433c 0x2874150 0x27f20bc 0x27f3227 0x27f38e2 0x2456afe 0x2456a3d 0x24347c2 0x2433f44 0x2433e1b 0x29e37e3 0x29e3668 0xb4565c 0x49b2 0x2c45)
libc++abi.dylib: terminate called throwing an exception

I need a way to be able to write to the label. I've tried everything I could think of and read many threads, however I cannot figure this out. If someone could please help and could show me a way to write to a label in a custom cell, that would be great.

Upvotes: 0

Views: 678

Answers (1)

Sulaiman Majeed
Sulaiman Majeed

Reputation: 499

Ok, I figured it out. I should not have been linking the labels via file owner but instead via the table cell.

Upvotes: 1

Related Questions