Accatyyc
Accatyyc

Reputation: 5818

UIView subclass is not visible

I'm a creating a subclass of UIView (MultiColumnTableView) which will hold a couple of table views. However, when I add my custom view as a subview to the view controller it is never visible, and I really can't figure out why?

I'm adding it with this code in my root view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _multiColumnTableView = [[MultiColumnTableView alloc] initWithNumberOfColums:3 columnWidth:200 frame:CGRectMake(100, 100, 0, 400)];
    _multiColumnTableView.dataSource = self;
    _multiColumnTableView.delegate = self;


    [self.view addSubview:_multiColumnTableView];
    [_multiColumnTableView reloadData];
}

The custom class initializer contains the following code:

- (id)initWithNumberOfColums:(NSInteger)columns columnWidth:(float)width frame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.numberOfColumns = columns;
        self.columnWidth = columnWidth;

        self.bounds = CGRectMake(0, 0, columnWidth * numberOfColumns, self.bounds.size.height);

        _tableViews = [NSMutableArray new];

        // Create all the desired colums (= tableViews)
        for (int i = 0; i < numberOfColumns; i++) {
            UITableView *t = [[UITableView alloc] initWithFrame:CGRectMake(columnWidth * i, 0, columnWidth, self.bounds.size.height)];
            [_tableViews addObject:t];
            t.tag = i;
            t.backgroundColor = [UIColor blueColor];
            t.dataSource = self;
            t.delegate = self;
            [self addSubview:t];
        }

    }
    return self;
}

I'm expecting to see some blue table views, but they are not visible, and therefore they never call cellForRowAtIndexPath:, but they do call numberOfRowsInSection. My custom subview is added to the root view. When counting the subviews it returns 1. Can anyone see the problem?

Upvotes: 0

Views: 215

Answers (2)

Umgre
Umgre

Reputation: 667

check each frame of tableviews is properly assigned.

Upvotes: 1

Apurv
Apurv

Reputation: 17186

do following changes inside constructor.

- (id)initWithNumberOfColums:(NSInteger)columns columnWidth:(float)width frame:(CGRect)frame
{
     self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, columns*width, frame.size.height)];
     if (self) {
          self.numberOfColumns = columns;
          self.columnWidth = columnWidth;


          _tableViews = [NSMutableArray new];

               // Create all the desired colums (= tableViews)
          for (int i = 0; i < numberOfColumns; i++) {
               UITableView *t = [[UITableView alloc] initWithFrame:CGRectMake(columnWidth * i, 0, columnWidth, frame.size.height)];
               [_tableViews addObject:t];
               t.tag = i;
               t.backgroundColor = [UIColor blueColor];
               t.dataSource = self;
               t.delegate = self;
               [self addSubview:t];
          }

     }
     return self;
}

Upvotes: 0

Related Questions