两个UITableView在同一个类?数据源和委托 [英] Two UITableView in the same class? datasource and delegate

查看:158
本文介绍了两个UITableView在同一个类?数据源和委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个类中有两个表,我需要每个表包含不同的数据,但是我有一个委托的麻烦...如何使每个表包含一个单独的委托?谢谢,对不起我的英语。

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; }

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

return [dataTable1 count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


CeldaFamilia * cell = CeldaFamilia *)[aTableView dequeueReusableCellWithIdentifier:@CeldaFamilia];




if(!cell){
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@CeldaFamiliaowner:self options:零];
cell = [nib objectAtIndex:0];

}

cell.propTextFamilia.text = [dataTable1 objectAtIndex:indexPath.row];

返回单元格;
}


解决方案

tableView 中传入的参数。示例:

   - ( NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection :( NSInteger)section {
if(tableView == self.tableView1){
return [dataTable1 count];
} else / * tableView == self.tableView2 * / {
return [dataTable2 count];
}
}

使用此模式,您需要将 if 所有 UITableViewDataSource UITableViewDelegate 方法中的语句



另一种方法是使一个方法返回表视图的数据数组:

   - (NSArray *)dataTableForTableView:(UITableView *)tableView {
if(tableView == self.tableView1){
return dataTable1;
} else / * tableView == self.tableView2 * / {
return dataTable2;
}
}

然后在每个数据源/委托方法。示例:

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [ [self dataTableForTableView:tableView] count];
}

您的 tableView:cellForRowAtIndexPath:方法可能仍然需要一个 if 语句,具体取决于每个表的数据。



但是,我建议您不要使用这些模式。如果您为每个表视图制作单独的数据源/委托,您的代码将更有条理,更易于理解。您可以使用同一个类的两个实例,也可以根据需要使用两个不同的类并使用每个类的一个实例。


I have two tables in the same class, I need each table contains different data but i have a trouble with the delegate... How can make each table contains a separate delegate? Thanks, sorry for my English.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1; }

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

    return [dataTable1 count];
     }

-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    CeldaFamilia *cell = (CeldaFamilia *)[aTableView dequeueReusableCellWithIdentifier:@"CeldaFamilia"];




    if (!cell) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CeldaFamilia" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

    cell.propTextFamilia.text =[dataTable1 objectAtIndex:indexPath.row];

    return cell;
     }

解决方案

You can do this by looking at the tableView argument that was passed in. Example:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView1) {
        return [dataTable1 count];
    } else /* tableView == self.tableView2 */ {
        return [dataTable2 count];
    }
}

With this pattern, you need to put if statements in all of your UITableViewDataSource and UITableViewDelegate methods.

Another way to do it is to make one method that returns the array of data for the table view:

- (NSArray *)dataTableForTableView:(UITableView *)tableView {
    if (tableView == self.tableView1) {
        return dataTable1;
    } else /* tableView == self.tableView2 */ {
        return dataTable2;
    }
}

Then use that function in each of your data source/delegate methods. Example:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self dataTableForTableView:tableView] count];
}

Your tableView:cellForRowAtIndexPath: method might still need to have an if statement, depending on what the data looks like for each table.

However, I recommend you don't use either of those patterns. Your code will be better organized and easier to understand if you make a separate data source/delegate for each table view. You can use two instances of the same class, or you can make two different classes and use one instance of each class, depending on your needs.

这篇关于两个UITableView在同一个类?数据源和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆