-1

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

-(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;
     }
4

3 回答 3

3

您可以通过查看tableView传入的参数来做到这一点。示例:

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

使用这种模式,您需要在所有的和方法中放置if语句。UITableViewDataSourceUITableViewDelegate

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

- (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语句,具体取决于每个表的数据是什么样的。

但是,我建议您不要使用其中任何一种模式。如果您为每个表视图创建单独的数据源/委托,您的代码将更好地组织并且更容易理解。您可以使用同一类的两个实例,也可以创建两个不同的类并使用每个类的一个实例,具体取决于您的需要。

于 2013-08-18T20:18:55.780 回答
2

如果您将每个 tableView 的标签设置为不同的数字,您可以使用它来区分它们。

例如,你可以做

tableView1.tag = 1;
tableView2.tag = 2;

然后在您的委托和数据源方法中,您可以执行以下操作:

if (tableView.tag == 1) {
    //first table data
}
else if (tableView.tag == 2) {
    //second table data
}
于 2013-08-18T20:18:57.403 回答
-1

您可以为第二个表(secondTable)添加其他类并在其中实现数据源和委托表视图协议。在您的视图控制器中,其中包含 viewDidLoad 中的 2 个表: SecondTable *second=[SecondTable alloc] init]; second.delegate=自己;second.datasource=秒;

你的第二个表将是 SecondTable 类的对象,而你总是喜欢第一个表。

于 2013-08-18T20:32:40.337 回答