0

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?

Accatyyc
  • 5,798
  • 4
  • 36
  • 51
  • For every tableview inside your view, should it call "reloadData"? – Tom Jun 08 '12 at 07:13
  • Yes, and it does. I have a custom reloadData in my class which loops through all of them. – Accatyyc Jun 08 '12 at 07:14
  • Are you sure your loop is actually being hit? I would suggest Logging the frame of each `UITableView` to see if it's what you'd expect. – skram Jun 08 '12 at 07:20
  • Yup, I have breakpoints all over. All the tableViews are created in the constructor, and reloadData is called for every tableView. Everything runs as expected except for cellForRowAtIndexPath:, which I figure is because they aren't visible... – Accatyyc Jun 08 '12 at 07:21
  • t.dataSource = self; t.delegate = self; Here you set them to your custom View, but it should go to a Controller supported UITableViewDelegate well officially.. – Tom Jun 08 '12 at 07:25
  • I'm aware of this. The custom controller is an UITableViewDelegate, and so is the root view controller. – Accatyyc Jun 08 '12 at 07:28
  • Sorry I am confused, your custom class is subclass of UIView, and it contains many UITableView(s), commonly if you want to display UITableView(s), you will specify its Controller , in your code, "self" for each tableview point to custom class, so how is it working as a Controller? – Tom Jun 08 '12 at 07:33
  • Heh, I understand the confusion. This is a table view "pipe", it looks to the class using it as one single table view, but it splits up all cells into multiple table views (or columns). Therefore it's a "table view", using it is exactly like using a standard table view but under the hood it does alot more work. – Accatyyc Jun 08 '12 at 07:35
  • Anyway, the problem is solved! Umgre helped me find it. It was because the "width" argument to my constructor was overridden by UIView's standard width. Didn't get any warnings and didn't think about it. Thanks for the help everyone! – Accatyyc Jun 08 '12 at 07:37
  • Thanks. I think I got what you said, just don't know if you specify all delegate or datasource of columns (table views) correctly or not. :) – Tom Jun 08 '12 at 07:41

2 Answers2

1

check each frame of tableviews is properly assigned.

Umgre
  • 667
  • 1
  • 6
  • 15
  • Thanks for quick response. I've changed the height in the original question to 400. Unfortunately, the problem persists whichever height I'm using :( – Accatyyc Jun 08 '12 at 07:16
  • Then check each frame of tableviews is properly assigned. – Umgre Jun 08 '12 at 07:23
  • I think you may be on to something here. The frame of each table view is (0.000000, 0.000000, 0.000000, 400.000000). I'm gonna look further into this. – Accatyyc Jun 08 '12 at 07:27
  • Yep, you helped med find the problem! Thanks! The problem was that I was taking the argument "width" into the constructor, but it was overridden by UIView's standard width. If you change your answer I'll mark it as accepted since you helped me find it :) – Accatyyc Jun 08 '12 at 07:36
0

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;
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • I tried this and it does not make any difference... Care to elaborate what is the difference when doing it this way? – Accatyyc Jun 08 '12 at 07:23
  • Either you should set frame or bounds. I removed bounds wherever used. Have u copied the whole function and pasted it into your project? – Apurv Jun 08 '12 at 07:25
  • Yes. I agree that your way is a better way to do it, and I have changed it into your code. I don't think it was the problem though. – Accatyyc Jun 08 '12 at 07:29