iOS程序在单个UIViewController中使用多个UITableView
我正在尝试在故事板中的一个segue中实现3个表格。 当select一个表时,它将取消隐藏另一个表的视图,同样也有一个表。 下面的代码我用于一个表格的每个单元格的格式是不同的,行也有所不同。 那么如何通过编码来为每个表格区分不同的行数呢?
-(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; }
你应该在.h
文件中声明你的tableViews
。
@property (weak, nonatomic) UITableView *firstTableView; @property (weak, nonatomic) UITableView *secondTableView; @property (weak, nonatomic) UITableView *thirdTableView;
然后所有的委托方法都有指向巫婆对象的variables调用这个方法,所以你可以检查:
-(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; }
其他委托方法以相同的方式工作。
您可以使用类属性跟踪不同的tableview,例如:
@property (nonatomic, strong) UITableView *tableView1; @property (nonatomic, strong) UITableView *tableView2; @property (nonatomic, strong) UITableView *tableView3;
在委托方法,你可以检查正确的tableView例如:
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 }
你有每个委托方法的tableview引用正确吗? 你可以找出你正在步行通过哪个表视图..
假设..
IBOutlet UITableView *tableView1; IBOutlet UITableView *tableView1; IBOutlet UITableView *tableView1;
例如:
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { if(tableView == tableView1) return 1; if(tableView == tableView2) return 5; return 10; }
你可以做同样的其他委托方法..我希望我正确理解你的问题..
你nedd创build3个uitableview网点。 然后你可以通过指定标签来识别表格。 例如tableview1.tag = 1,tableview.tag = 2等
那么在tableview方法中你可以使用它。 例如。
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(tableView.tag==1){ return 3; } else if(tableView=2) {return 3;} }
可能是这个帮助。