0

I have created a custom widget as a XIB file where I have multiple UITableViews and a UIButton. In the corresponding swift file which is the owner of this XIB, I have outlets to these TableViews.

I have added this widget to a view inside a view in a UIViewController. Now in the corresponding swift file of this controller, I need to assign dataSource and delegate to each of the tableviews, and an action to the button.

I have been looking online for long, and seems like @IBInspectable vars are the way to go, but seems like I cannot make a var which of type UITableView,UITableViewDelegate or UITableViewDatasource as @IBInspectable.

So how do I use the tableviews and the button? Can anyone direct me to the correct documentation, example, or explain?

rgamber
  • 5,749
  • 10
  • 55
  • 99

1 Answers1

1

No need to use an @IBInspectable. You can simply use each table source within the UITableViewDelegate methods conditionally. Here's one way to go about doing this:

First within your storyboard's UITableViewController, add a prototype cell, then within that prototype cell add a UITableView with its own prototype cell.

Then set both the inner and outer table view cell's reuse identifiers like so:

Outer table view cell: Outer table view cell reuse identifier

Inner table view cell: Inner table view cell reuse identifier

Then link that inner tableview's data source and delegate to the UITableViewController's own data source and delegate:

Link data source and delegate #1 Link data source and delegate #2

Then within your UITableViewController class, you can set your tables' elements conditionally, for example:

- (void)viewDidLoad {
    [super viewDidLoad];
    dataSource1 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
    dataSource2 = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView) {
        return 80;
    } else {
        return 20;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (tableView == self.tableView) {
        return dataSource1.count;
    } else {
        return dataSource2.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell;

    if (tableView == self.tableView) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier2" forIndexPath:indexPath];
    }

    // Configure the cell...
    if (tableView == self.tableView) {
        cell.textLabel.text = [dataSource1 objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [dataSource2 objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor blueColor];
    }
    cell.textLabel.backgroundColor = [UIColor clearColor];

    return cell;
}

Which in this case produces the following result: Final result

Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • So I will have to do this in a `UITableViewController` subclass. How do I connect the `table1` and `table2` from the xib to this? – rgamber Feb 14 '15 at 18:42
  • @rgamber No, no need to do this in a UITableViewController subclass. I'll update my answer to clarify. – Lyndsey Scott Feb 14 '15 at 19:50
  • I appreciate the detailed reply :). I think there is some misunderstanding. I am not doing this from storyboard, but from a xib. I have created a 'widget' which contains multiple TableViews and which I will be reusing in different views. So I need a way to connect the tables in the widget to the table-controllers based on whichever view its being called from. These controllers will be different in different views. Sorry if my question was not clear enough! – rgamber Feb 14 '15 at 20:45
  • My answer will work from either storyboards or xibs so that doesn't matter at all... But the fact that you're reusing your table view widget in multiple views does matter A LOT. To do something like I've suggested, you'd need to set the inner tables' data source and delegate to each current view. I can't really answer any more specifically than that without knowing *way* more about your app structure... – Lyndsey Scott Feb 14 '15 at 20:50
  • I understand! I am reading more on this, though will probably IM you if I am stuck for a super-long time, with more details! Writing the above answer must've taken some effort, so thanks a lot. It does help to better my understanding! – rgamber Feb 14 '15 at 20:52