Multiple Uitableview in Single Viewcontroller

Multiple UITableview in Single Viewcontroller

It will be the same as you do it with one table view, but you should check which tableview is currently using.

myTableView1.dataSource = self;
...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == myTableView1) {
// your code 1
}
else
if (tableView == myTableView2) {
// your code 2
}
else
if (tableView == myTableView3) {
// your code 3
}
}

Edit:

About brightness:

How to change brightness in iOS 5 app?

And about UISlider it has minimunValue and maximumValue properties.

- (void) sliderChanged:(UISlider*)sender{
UISlider *slider = (UISlider*)sender;
[[UIScreen mainScreen] setBrightness:slider.value];
}

Edit:

slider.tag = 1;
[cell addSubview:slider];

...
// when you need..
indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySecion];
UISlider* slider = (UISlider*) [[self.tableView cellForRowAtIndexPath:indexPath] viewWithTag:1];

Multiple UITableView in a single ViewController with custom UITableViewCell

simply return your cell in your if statement like so :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// get rid of the `var cell: UITableViewCell!`

if tableView == tableViewUser {
cell = tableViewUser.dequeuReusableCell(withIdentifier: "userCell", for: indexPath) as! TreeViewCell
cell.setNodeData(node: self.displayArray[indexPath.row])
return cell
}

if tableView == tableViewSearch {
cell = tableViewUser.dequeuReusableCell(withIdentifier: "searchCell", for: indexPath)
cell.textLabel.text = "123456"
return cell
}

return UITableViewCell() // dont worry, you will never go there
}

Also uncomment the register part on your tableViewUser didSet

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.

iOS program to use multiple UITableView in a single UIViewController

You should declare your tableViews in .h file.

@property (weak, nonatomic) UITableView *firstTableView;
@property (weak, nonatomic) UITableView *secondTableView;
@property (weak, nonatomic) UITableView *thirdTableView;

And then all the delegate methods have variable with pointing witch object call this method, so you can check:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.firstTableView)
return 3;
else if(tableView == self.secondTableView)
return 4;
else if(tableView == self.thirdTableView)
return 100;
}

The other delegate methods work in the same way.

Two tableviews in one view controller, one table view not showing up

If you do not need any section in pickerTableView, then also it has to have at least one section.

So return 1 in method numberOfSections when tableview == self. pickerTableView.

func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.tableView {
return Data.userModels.count
} else {
return 1
}
}


Related Topics



Leave a reply



Submit