本文介绍了如何在1个viewController中管理2个tableview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道在一个viewController中管理几个tableView的简单方法?
这是到目前为止我一直在做的:

   - (NSInteger)numberOfSectionsInTableView :( UITableView *) tableView 
{
if(tableView == self.tableView1)
return 1;
else if(tableView == self.tableView2)
return 2;


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView == self.tableView1)
return @bla;
else if(tableView == self.tableView2)
return @blabla;


- (NSString *)tableView:(UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath
{
if(tableView == self.tableView1)
...
else if(tableView == self.tableView2)
...
}

我发现真的很烦人,我必须为每个委托方法使用if / else语句。此外,当有很多tableViews时,很难阅读。此外,我有与NSURLConnection相同的问题,等等...一旦我有几个对象响应相同的委托协议,事情就会变得混乱。



什么是使事情更简单的最好方法?
谢谢

解决方案

您可以使用选择器和表视图的某种标识符( UIView 标签,例如)。这样的东西:

   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
return [self performSelector:NSSelectorFromString([NSString stringWithFormat:@tableView%d:titleForHeaderInSection:,tableView.tag])];
}

当然,您需要为每个表视图使用一种方法。假设你的两个表有一个标签100和101,那么你将会有 tableView100:titleForHeaderInSection tableView101:titleForHeaderInSection


Does anyone know a simple way to manage several tableViews in one viewController?Here is how I've been doing it so far:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
if(tableView == self.tableView1) 
return 1;
else if(tableView == self.tableView2) 
return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(tableView == self.tableView1) 
return @"bla";
else if(tableView == self.tableView2) 
return @"blabla";
}

-(NSString *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.tableView1) 
...
else if(tableView == self.tableView2) 
...
}

I find it really annoying that I have to use an if/else statement for EVERY SINGLE delegate method. Plus, it is really hard to read when there are many tableViews. Besides, I have the same problem with NSURLConnection, etc... As soon as I have several objects that respond to the same delegate protocol, things get messy.

What is the best way to make things simpler?Thanks

解决方案

You could use selectors and some kind of identifier for the table views (the UIView tag, for example). Something like this:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  return [self performSelector:NSSelectorFromString([NSString stringWithFormat:@"tableView%d:titleForHeaderInSection:", tableView.tag])];
}

Of course you will need to have one method for each of your table views. Suppose your two tables have a the tags 100 and 101, you will have then tableView100:titleForHeaderInSection and tableView101:titleForHeaderInSection.

这篇关于如何在1个viewController中管理2个tableview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 14:00