Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

ios - Two UITableView in the same class? datasource and delegate

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;
     }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...