【发布时间】:2011-10-20 05:23:54
【问题描述】:
如何使用 tabledatasource 和 tabledelegates 访问单个类中的两个或多个 tableView?
【问题讨论】:
-
可以设置table view的tag来区分...
标签: iphone ios ipad uitableview
如何使用 tabledatasource 和 tabledelegates 访问单个类中的两个或多个 tableView?
【问题讨论】:
标签: iphone ios ipad uitableview
在同一个委托方法中,你必须处理所有的 tableview,
例如:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==tableView1)
//value for first tableview
else if(tableview==tableView2)
//value for second tableview
}
【讨论】:
如果您从 XIB 添加它们,那么 KingofBliss 的回答是正确的,否则如果您以编程方式添加它们,您可以在它们上设置标签属性,然后在委托中使用它来区分您正在处理哪个表。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableView tag] == 1)
{
//value for first tableview
} else {
//value for second tableview
}
【讨论】:
UITableView tableView1 = [[UITableView alloc] init]; 当你创建 tableview 给他们标签..
tableView1.tag = 10;
tableView1.delegate = self;
tableView1.dataSource = self;
[self.view addSubview:tableView1];
[tableView1 release];
UITableView tableView2 = [[UITableView alloc] init];
tableView2.tag = 20;
tableView2.delegate = self;
tableView2.dataSource = self;
[self.view addSubview:tableView2];
[tableView2 release];
在委托中
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView.tag == tableView1.tag)
//value for first tableview
else if(tableview.tag==tableView2.tag)
//value for second tableview
}
【讨论】:
我不认为有必要添加两个 UITableView 并区分它们,因为将调用相同的委托函数。只需添加一个 UITabaleView 并对您的数据进行检查,设置标志,例如,如果当前您的 UITableView 填充了 Array1 而现在您想用 Array2 填充它,只需设置标志并调用 [UITableView reloadData] .
你的问题解决了。
至于我的建议,在同一个类中添加两个 UITableView 并不是一个好习惯
【讨论】: