xcode 如何在 iphone 的单个视图控制器中使用两个以上的 UITableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11823565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use more than two UITableView in single view controller in iphone
提问by user1567956
I am using two UITableView
s in a UIViewController
, how can I populate the row and cells in both tableviews? When I give for the second tableview it says the duplicate declaration of the number of rows in section etc.
我UITableView
在 a 中使用了两个s UIViewController
,如何在两个 tableviews 中填充行和单元格?当我为第二个 tableview 提供时,它说部分中的行数的重复声明等。
回答by Cyrille
That's why the dataSource/delegate methods have a tableView
parameter. Depending on its value, you can return different numbers/cells/...
这就是 dataSource/delegate 方法有一个tableView
参数的原因。根据其值,您可以返回不同的数字/单元格/...
- (void)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == _myTableViewOutlet1)
return 10;
else
return 20;
}
回答by Kapil Choubisa
All of your UITableViewDelegate
and UITableViewDatasource
methods will be implemented only once. You just need to check for which table view the method is being called.
您的所有方法UITableViewDelegate
和UITableViewDatasource
方法都将只执行一次。您只需要检查正在调用该方法的表视图。
if (tableView == tblView1) {
//Implementation for first tableView
}
else {
//Implementation for second tableView
}
this will work in all of TableView's delegate and datasource methods as tableView
is common parameter in all of your methods
这将适用于所有 TableView 的委托和数据源方法,因为这tableView
是所有方法中的通用参数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
This Linkalso has the solution of your issue.
此链接也有您的问题的解决方案。
Hope this helps
希望这可以帮助
回答by Mahendra
Please have a look.
请看一看。
First create two tableview in interface builder and then connect with two IBOutlet variables and set delegate and datasource for both the tableviews.
首先在界面生成器中创建两个 tableview,然后连接两个 IBOutlet 变量并为这两个 tableview 设置委托和数据源。
In Interface file
在接口文件中
-IBOutlet UITableView *tableView1;
-IBOutlet UITableView *tableView2;
In Implementation file
在实现文件中
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView==tableView1)
{
return 1;
}
else
{
return 2;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==tableView1)
{
return 3;
}
else
{
if (section==0)
{
return 2;
}
else
{
return 3;
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView==tableView1)
{
//cell for first table
}
else
{
//cell for second table
}
return cell;
}
Use this code. hope helps
使用此代码。希望有帮助
回答by Karthi
It is possible,look at the reference code here: http://github.com/vikingosegundo/my-programming-examples
有可能,请看这里的参考代码:http: //github.com/vikingosegundo/my-programming-examples
Please also refer this page : 2 tableview on a single view
另请参阅此页面:单个视图上的 2 个表视图