abc123
abc123

Reputation: 8303

How get a custom UITableViewCell to appear in a UITableView in custom UIViewController with storyboard?

I think I"m really close now, but I'm missing something. If I break before I return the cell in the tableView:cellForRowAtIndexPath method, the cell is holding the correct data. But it doesn't appear in the table. I'm assuming that my UITableView is not hooked up to the UIViewController properly.

Here is my custom cell: SCCustomerTableCell.h

#import <UIKit/UIKit.h>
#import "SCCustomer.h"

@interface SCCustomerTableCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *companyLabel;
-(void)configureCellWithCustomer:(SCCustomer *)customer;

@end

SCCustomerTableCell.m

#import "SCCustomerTableCell.h"

@implementation SCCustomerTableCell

-(void)configureCellWithCustomer:(SCCustomer *)customer
{
    self.companyLabel = [[UILabel alloc] init];
    self.companyLabel.text = [customer dbaName];
}

@end

And the custom view controller: SCCustomersVC.h

#import <UIKit/UIKit.h>
#import "SCCustomerTableCell.h"
#import "SCDataObject.h"

@interface SCCustomersVC : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property (strong, nonatomic) IBOutlet UITableView *customersTable;
@property SCDataObject *dataObject;
@property (nonatomic, strong) NSArray *customers;

@end

SCCustomersVC.m

#import "SCCustomersVC.h"
#import "SCSplitVC.h"
#import "SCDataObject.h"

@interface SCCustomersVC ()
@end

@implementation SCCustomersVC

@synthesize customersTable;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomerCell";
    [self.customersTable registerClass:[SCCustomerTableCell class] forCellReuseIdentifier:CellIdentifier];
    SCCustomerTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    SCCustomer *customer = (SCCustomer *)[self.customers objectAtIndex:indexPath.row];
    [cell configureCellWithCustomer:customer];
    return cell;
}

Edit: I forgot screens of storyboard connections:
SCCustomersVC, UITableView, SCCustomerTableCell, UILabel

Upvotes: 0

Views: 335

Answers (1)

rdelmar
rdelmar

Reputation: 104082

Ok, there are several things you're doing wrong. I see from your screen shots that you didn't set your controller as the data source or delegate of the table view. You need to do that. Second, if you make the custom cell in the table view in IB, you don't need to register the class, just make sure that you have the identifier set. Third, since you made the label in IB, and you have an outlet to it, you don't need to alloc init it in your cell's .m file -- in fact you don't need anything at all in the .m . Just put cell.companyLabel.text =... in the cellForRowAtIndexPath method.

Upvotes: 1

Related Questions