在使用批量tableview时使用delegates

我的ViewController中有3个tableView,可不可以实现单独使用UITableViewDelegate方法?比如,可以分别对每个UITableView使用cellForRowAtIndexPath方法,但是不知道对每个tableView单独用numberOfRowsInSection或者numberOfSectionsInTableView有什么不同?可以实现吗?

可以啊
:D

设置3个UITableView变量,放在你的 ViewController.h 文件中:

在你的 ViewController : UIViewController

{
    UITableView* tableView1;
    UITableView* tableView2;
    UITableView* tableView3;
}

并且在你的 ViewController.m 中:

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

        if (tableView == tableView1)
        {
            //Your code
        }
        if (tableView == tableView2)
        {
            //Your code
        }
        if (tableView == tableView3)
        {
            //Your code
        }
    }

对所有的table使用一个dataSource和delegates方法,

- (NSInteger)numberOfRowsInSection:(NSInteger)section;
     {
     return 1;
     }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section;
   {
if (tableView==tabl1) {
        return [arr1 count];
}
if (tableView==tabl2) {
        return [arr2 count];

}
if (tableView==tabl3) {
        return [arr3 count];
}
return 0;

   }

  - (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 ];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor clearColor];
}
if (tableView==tabl1) {
    cell.textLabel.text = [arr1 objectAtIndex:indexPath.row];
}
if (tableView==tabl2) {
    cell.textLabel.text = [arr2 objectAtIndex:indexPath.row];
}
if (tableView==tabl3) {
    cell.textLabel.text = [arr3 objectAtIndex:indexPath.row];
}
return cell;
      }

当然可以。

所以的tableView delegates函数都有tableView作为第一参数,所以你只需要记录三个tableView,在每个delegates函数中检测调用的是哪个tableview就可以:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == firstTableView) {
    ...
    }
    else if (tableView == secondTableView) {
    ...
    }
    else if (tableView == thirdTableView) {
    ...
    }

}