问题
I am trying to implement 3 tables in a single segue in a storyboard. When one table is selected it will unhidden a view with another table and likewise one more. The following code i have used for one table the cell format of each table is different and rows also vary. So how can i DIFFERENTIATE between each table by coding to set different number of rows for each table and so on?
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell2";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1==nil)
{
cell1=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
temp=[array objectAtIndex:indexPath.row];
UILabel *Label1 = (UILabel *)[cell1 viewWithTag:4];
Label1.text = temp.Title;
UILabel *Label2 = (UILabel *)[cell1 viewWithTag:6];
Label2.text = temp.Title;
UITextField *textfield1 = (UITextField *)[cell1 viewWithTag:5];
textfield1.text =temp.description;
UILabel *Label3 = (UILabel *)[cell1 viewWithTag:7];
Label3.text = temp.Title;
return cell1;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.showlist=[[ShowList alloc]initWithNibName:@"ShowList" bundle:nil];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ShowlistIndex=indexPath.row;
_secondview.hidden=NO;
}
回答1:
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.
回答2:
You can keep track of the different tableviews using class properties e.g.:
@property (nonatomic, strong) UITableView *tableView1;
@property (nonatomic, strong) UITableView *tableView2;
@property (nonatomic, strong) UITableView *tableView3;
In the delegate methods you can check for the correct tableView e.g.:
if (tableView == self.tableView1) {
// add code for tableView1
} else if (tableView == self.tableView2) {
// add code for tableView2
} else if (tableView == self.tableView3) {
// add code for tableView3
} else {
// unknown tableView
}
回答3:
You have the tableview reference in each of your delegate methods right? You can find out which tableview you are currently walking through based on that..
Assuming..
IBOutlet UITableView *tableView1;
IBOutlet UITableView *tableView1;
IBOutlet UITableView *tableView1;
Ex:
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == tableView1)
return 1;
if(tableView == tableView2)
return 5;
return 10;
}
You can do the same for the other delegate methods.. I hope I understood your question correctly..
回答4:
you nedd to create 3 uitableview outlets. and then you can identifies tableviews by specifyin tag. for example tableview1.tag=1, tableview.tag=2 etc.
then in tableview methods you can use it. for example.
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView.tag==1){
return 3;
}
else if(tableView=2)
{return 3;}
}
may be this help.
回答5:
Swift 4.1
4.1 is a version which i wrote the code. It might be worked in the other version of swift. I'm not sure.
Firstly , right click to the tableViewContent1 and tableViewContent2 and drag it to dataSource and delegate into View Controller like this image below.
Secondly , you can implement as my code below. * I've already set the both label in tableViewContent1 and tableViewContent2 with tag = 1000 .
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableViewContent1: UITableView!
@IBOutlet weak var tableViewContent2: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController :
UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == tableViewContent1 {
// in order to make the tableViewContent1 get 5 rows.
return 5
}
else{
// in order to make the tableViewContent2 get 3 rows.
return 3
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableViewContent1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "content1", for: indexPath) as! TableViewCellFirstCustom
let label : UILabel = cell.viewWithTag(1000) as! UILabel{
lab.text = "to change label in content1."
}
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "content2", for: indexPath)
let label : UILabel = cell.viewWithTag(1000) as! UILabel
label.text = "to change label in content2."
return cell
}
}
}
after that you will get result like image below. It worked for me.
来源:https://stackoverflow.com/questions/19928295/ios-program-to-use-multiple-uitableview-in-a-single-uiviewcontroller