1

I have a viewController that contains a programmatically created tableView.

@property (strong, nonatomic) UITableView *numbersTableView;
//...
self.numbersTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width-20, 50)];
self.numbersTableView.delegate = self;
self.numbersTableView.dataSource = self;
self.numbersTableView.tag = 1;
self.numbersTableView.layer.cornerRadius = 5;
[self.numbersTableView setBounces:false];
[self.numbersTableView setHidden:true];
[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:@"NumbersTableViewCell"];
[self.view addSubview:self.numbersTableView];

For the prototype cell I want to use a prototype that I created somewhere else in another viewController and I designed It in storyboard.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 1) { //tableView == self.numbersTableView
        NSString *cellID = @"NumbersTableViewCell";
        AZGCountryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[AZGCountryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.countryName.text = self.numbersArray[indexPath.row].number;
        cell.flagImageView.image = [UIImage imageNamed:self.numbersArray[indexPath.row].country];
        return cell;
    }
}

And my custom cell contains one UIImageView and one UILabel but when I run the code the cells are empty and I can see the UIImageView and UILabel are nil!

#import <UIKit/UIKit.h>

@interface AZGCountryCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *flagImageView;
@property (strong, nonatomic) IBOutlet UILabel *countryName;
@property (strong, nonatomic) IBOutlet UILabel *countryCode;

@end

#import "AZGCountryCell.h"

@implementation AZGCountryCell
@synthesize flagImageView;
@synthesize countryName;

- (void)awakeFromNib {
    [super awakeFromNib];
    self.flagImageView.layer.cornerRadius = self.flagImageView.frame.size.width/2;
    self.flagImageView.layer.masksToBounds = YES;
}

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

@end

Then what should I do to have properly filled cells in my numbersTableView?

Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
  • You need to use it in a cell xib, and register the nib. That's how you link the "UI" and the "code" part. – Larme Oct 23 '19 at 11:59
  • @Larme Actually, my real problem began when I updated my XCode to v.11. The existing project's storyboard is gonna get messed if I make any changes. Therefore I have to do everything programmatically! Are you familiar with this issue? – Arash Etemad Oct 23 '19 at 12:10
  • you should copy that cell in your view controller that u want to use it. – Shahzaib Qureshi Oct 31 '19 at 12:15
  • @ShahzaibQureshi As you can see I described the main problem to `@Larme`, please read it – Arash Etemad Oct 31 '19 at 13:20
  • 1
    If you want to do it programatically, you will have to add the views programatically in your cells awake from nib or you will have to design a nib if you do it with Interface designing. – Shahzaib Qureshi Oct 31 '19 at 13:26
  • @ArashEtemad - you cannot use a Storyboard Prototype cell in a different table.... To do it "programmatically," you'll need to add the subviews and constrain them inside your cell class. If you show your current prototype layout, you can get more help. – DonMag Oct 31 '19 at 14:15

2 Answers2

0

Unfortunately you can't reuse a table cell you've defined in a table view in a storyboard in your separate class.

In order for you to be able to share your cell between the different views, you'll have to extract the view to a separate nib and register that nib go be used on both tables.

Alternatively you can implement the cell's UI in code in the table view cell subclass.

Andrew
  • 3,166
  • 21
  • 32
-1

You getting empty cell because custom tableview cell not registered, use [self.numbersTableView registerNib:[UINib nibWithNibName:@"AZGCountryCell" bundle:nil] forCellReuseIdentifier:@"NumbersTableViewCell"];

instead of

[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:@"NumbersTableViewCell"];