我的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) {
...
}
}