0

I've been trying to link my tableviewcell to my tableview which is in a UIView through my uiviewcontroller. Although the tableview is there, my custom tableviewcell is not loading. Log is showing Cell is initialized and populated

Screenshot of what it looks like

Here are the codes that I use in the viewcontroller while the tableview is already setup in a UIView

viewcontroller.m

- (void)viewDidLoad {
    [super viewDidLoad];

    barcodeItems = [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];
    NSLog(@"%@", barcodeItems);

    [photoCaptureView.itemsTableView registerNib:[UINib nibWithNibName:@"BarcodeItemsTableViewCell" bundle:nil] forCellReuseIdentifier:@"BarcodeItemsCell"];
    photoCaptureView.itemsTableView.rowHeight = 60;
    photoCaptureView.itemsTableView.dataSource = self;
    photoCaptureView.itemsTableView.delegate = self;

}

#pragma mark UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return barcodeItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Cell Initialized");
    static NSString *cellIdentifier = @"BarcodeItemsCell";

    BarcodeItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[BarcodeItemsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

//     Configure the cell...
    NSLog(@"Cell Populated");
    cell.barcodeLabel.text = @"TEST";
    UIImage *btnImage = [UIImage imageNamed:@"barcodeIcon"];
    [cell.leftButton setImage:btnImage forState:UIControlStateNormal];

    return cell;
}
jordanzee
  • 3
  • 3
  • Have you properly set the delegate and datasource of the tableview to your view controller? – Groot Mar 25 '20 at 10:13
  • Is `NSLog(@"Cell Populated");` called? When is the call of `reloadData` for the tableView? Who is the datasource/delegate (since it's a property of CaptureView) – Larme Mar 25 '20 at 10:20
  • Is just the custom cell not showing or the default UITableViewCell is showing the same behaviour ? – Najeeb ur Rehman Mar 25 '20 at 10:25
  • so I just set the delegate and datasource in viewdidload. – jordanzee Mar 25 '20 at 10:31
  • update: Log is showing cell is initialized and populated but showing empty cells. I provided a screenshot of what it looks like in the question – jordanzee Mar 25 '20 at 10:50
  • Register before setting the datasource/delegate? Also, did you use a custom Xib for the cell or not? Because you changed your code from that. – Larme Mar 25 '20 at 10:56
  • Hi @Larme Yes I am using a custom xib for the cell. I tried putting registerclass before delegate and datasource but nothing changed – jordanzee Mar 25 '20 at 11:01
  • If you have a custom xib for the cell, prefer register nib instead of register class. – Larme Mar 25 '20 at 11:05
  • @Larme I changed it to `[photoCaptureView.itemsTableView registerNib:[UINib nibWithNibName:@"BarcodeItemsTableViewCell" bundle:nil] forCellReuseIdentifier:@"BarcodeItemsCell"];` now im getting this error `Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key barcodeLabel.'` – jordanzee Mar 25 '20 at 11:15
  • You have an issue with your xib. `barcodeLabel` doesn't exist in it? Look for that error it's a well known one. – Larme Mar 25 '20 at 11:35
  • replace BarcodeItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; with BarcodeItemsTableViewCell *cell = [photoCaptureView.itemsTableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; – Atmaram Mar 27 '20 at 07:33

2 Answers2

0

I can"t see in your code where in viewdidLoad you set the Datasource and Delegate to self. I assume the Datasource and Delegate methods are in the same class as the viewdidLoad.

What I noticed additionally is that you initialize and define the table as "barcodeItemsTableView" in viewDidLoad and then later you refer to that table in the Datasource and Delegate methods. This, to what I can see of your code, means that the table "barcodeItemsTableView" is not a property of the class and the initialized table "barcodeItemsTableView" only "lives inside viewDidLoad" and will then be discarded again.

You should have an outlet from your table in the view controller.h or define the table as a property and show it programmatically.

MacUserT
  • 1,760
  • 2
  • 18
  • 30
0

The custom cell is registered with an identifier "BarcodeItemsCell"

[photoCaptureView.itemsTableView registerNib:[UINib nibWithNibName:@"BarcodeItemsTableViewCell" bundle:nil] forCellReuseIdentifier:@"BarcodeItemsCell"];

but tried be populated with an identifier named "Cell".

static NSString *cellIdentifier = @"Cell";

BarcodeItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
stcui
  • 106
  • 4