nurbs999
nurbs999

Reputation: 87

Objective-C custom UITableView class doesn't load

In XCode 5 I startet with the Tab-Bar example and added a UITableView to my ViewController in storyboard. I'd like to use a custom class for that UITableView, so I added a new class to my project and set the class of my UITableView to be that new class in storyboard. I also set the cellIdentifier. The problem is that the app is still using the default UITableView class. Could you please tell me what I'm missing?

Here is my custom UITableView class

#import <UIKit/UIKit.h>
@interface CustomTableView : UITableView <UITableViewDelegate, UITableViewDataSource>
@end

@implementation CustomTableView

- (CustomTableView*) init {
  self = [super init];
  if (self) {
    self.delegate = self;
    self.dataSource = self;
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self reloadData];
  }
  return self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 20;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  return @"header title";
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString* staticCellIdentifier = @"customIdentifier";
  UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:staticCellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:staticCellIdentifier];
  }
  cell.textLabel.text = @"cellTitle";
  return cell;
}
@end

Upvotes: 0

Views: 1501

Answers (1)

Dima
Dima

Reputation: 23634

You need to implement initWithCoder: which is the designated initializer interface builder uses.

Something like this:

- (CustomTableView*) initWithCoder:(NSCoder *)aDecoder{
  self = [super initWithCoder:(NSCoder *)aDecoder];
  if (self) {
    self.delegate = self;
    self.dataSource = self;
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self reloadData];
  }
  return self;
}

Upvotes: 3

Related Questions