2 Uitableviews in One Uiview

2 UITableViews in one UIView

I would keep one datasource & delegate.

This means that all the delegate/datasource methods become more complicated BUT it means that you can retain the one to one relationship between viewController & view.

keep a reference to each of the table views

//vc.h
@property (nonatomic, weak) IBOutlet UITableView* firstTableView;
@property (nonatomic, weak) IBOutlet UITableView* secondTableView;

In the datasource/ delegate methods you need to account for the fact that the method needs to behave differently depending on which table view is in use. e.g.

//vc.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

...

if (tableView == self.firstTableView) {

...

} else { // tableView == self.secondTableView

...
}
}

return cell;

}

Multiple UITableViews on one UIView

so you need some way to tell the two tableViews apart--you could either set the "tag" property to different values, or have a property on your view controller that points to each view

@property (nonatomic, retain) IBOutlet UITableView *tableView1;
@property (nonatomic, retain) IBOutlet UITableView *tableView2;

then hook these up to each view in interface builder...

then in your view controller methods you can do

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.tableView1) {
return 37;
} else if (tableView == self.tableView2) {
return 19;
} else {
// shouldn't get here, use an assert to check for this if you'd like
}
}

Multiple TableViews in One UIView Swift - How to show both

Unfortunately in my case it was due to my constraints as I had put them into a stack view but with the stack view not set to distribute correctly. So I needed to amend that setting in the storyboard. Then both were present and correct as expected.

Multiple UITableViews visible at once in one UIViewController

I think you might try to drag UITableViewController into your view Controller, at least I don't have that problem to add 4 table view into a scroll view.

here is how i added it

1.> Drag the scroll view control into view controller

enter image description here

enter image description here

Your view controller should look like this:

enter image description here

2.> Drag the table view control into the scroll view, and set the size and position of that table view

enter image description here

enter image description here

Your view controller should look like this:

enter image description here

3.> Then drag all the rest 3 table views onto Scroll view

enter image description here
enter image description here

But i would like to suggest a couple of things in your case

  1. no using that much table view in the same view controller, it's a chaos in your codes to maintain all them. There are always better
    options than 4 table view, maybe consider collection view. or even
    separate the use flow.

  2. If i were you, i won't use table view inside Scroll view, they are like scroll view inside scroll view, if you don't design the
    interaction very very well, they become extremely hard to use.

  3. If you still want to use four table view in the same view controller after all, you want to pay extra attentions on your table view datasource and delegate. very carefully handle all the cases.

Hope that helps you.

How do I create two table views in one view controller with two custom UITableViewCells?

The error appears because if for any reason, the table view is non of the two options that you wrote, then it doesn't have any value to return, just add a default value at the end:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == firstTableView,
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOne") as? CustomOneTableViewCell {
return cell
} else if tableView == autoSuggestTableView,
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTwo") as? CustomTwoTableViewCell {
return cell
}

return UITableViewCell()
}

Updated to swift 4.1.2:
I've updated this answer to version 4.1.2, also, because the return value of the method cannot be nil, modified to a default, dummy UITableViewCell.

Is it possible to put two UITableview in a UIView

It looks like you're asking if one object (maybe a view controller?) can have two UITableViews both using it as their delegate. Yes, a view controller can be the delegate for multiple table views-- that's why all of those methods pass in a UITableView* as their first argument; it's for you to use to figure out which one is which. You should keep a couple instance variables (IBOutlets probably) in your view controller so you know which is which and you can act appropriately.

Cheers,
Interdev.

Two TableViews in one View Controller

Yes. Since the data source and delegate methods provide a reference to the tableview, you can simply check if it is equal to the first or the second table you have.

Example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView isEqual:_firstTable]) {
// Do something
}

else { // tableView == _secondTable
// Do something else
}
}

2 diff UITableViews on a single UIView

Yes. Notice the 1st argument of all the delegate/datasource methods. It is the reference of the tableView being handled.

It can be done, but depending on the situation, there might be more elegant ways of doing whatever you are trying to do.



Related Topics



Leave a reply



Submit