xcode IOS:两个tableview的tableview委托方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6519673/
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
IOS: tableview delegate methods for two tableview
提问by cyclingIsBetter
I have these delegate method for a tableview inside a class:
我有一个类中的 tableview 的这些委托方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ;
}
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
return cell;
}
if I have a single UITableView it's ok but if I have two UITableView? How Can I organize my code? with tag?
如果我有一个 UITableView 没问题,但如果我有两个 UITableView?如何组织我的代码?带标签?
回答by Pripyat
See how all the delegate methods have a tableView:(UITableView *)tableView
in them?
看看所有的委托方法是如何tableView:(UITableView *)tableView
在其中包含 a的?
You can define your table views in the header file and then just simply go: (assuming your table is called myTable
)
您可以在头文件中定义您的表视图,然后只需简单地去:(假设您的表被称为myTable
)
if (tableView == myTable)
Then you can have as many table views as you like.
然后,您可以拥有任意数量的表格视图。
So for example:
例如:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
}
Becomes:
变成:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == myTable)
{
return [array1 count];
}
if (tableView == myTable2)
{
return [array2 count];
}
return 0;
}
回答by sergio
My suggestion is having your data source act as a table view delegate, instead of your controller.
我的建议是让您的数据源充当表视图委托,而不是您的控制器。
This is a design more closer to the Model-View-Controller pattern and will allow you much more flexibility and avoid checking for the specific value that the tableView
argument has got in your delegate methods.
这是一种更接近模型-视图-控制器模式的设计,它将为您提供更大的灵活性并避免检查tableView
参数在您的委托方法中获得的特定值。
In your case, your delegate/data source would be a class with a member of type NSArray
and also implementing the UITableViewDelegate
protocol.
在您的情况下,您的委托/数据源将是一个具有类型成员NSArray
并实现UITableViewDelegate
协议的类。
回答by AltinkOnline
Yes you can do it with tag. Give your UITableViews the tags 1 and 2.
是的,你可以用标签来做。给你的 UITableViews 标签 1 和 2。
set up an switch:
设置一个开关:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ;
}
switch ([tableView tag]) {
case 1:{
[[cell textLabel] setText:@"First tag"]
break;
}
case 2:{
[[cell textLabel] setText:@"Second tag"]
break;
}
default:
break;
}
return cell;
}
回答by NonatomicRetain
Each of those methods passes in a reference to the table view that's calling it. I usually connect each table to an outlet in interface builder and conditionally return the datasource based on a comparison with tableView in the delegate methods and the outlet names. Doing so with a tag is also possible but messier and more open to complications when editing your view structure.
这些方法中的每一个都传递对调用它的表视图的引用。我通常将每个表连接到界面构建器中的一个插座,并根据与委托方法和插座名称中的 tableView 的比较有条件地返回数据源。使用标签也可以这样做,但在编辑视图结构时更加混乱且更容易出现复杂情况。