如何处理两个TableView [英] How to deal with two TableView

查看:87
本文介绍了如何处理两个TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个应用程序中有两个UITableView实例,问题是我不知道如何定义每个表需要显示的内容。这是代码:

I have two instances of UITableView in one app, and the problem is that I don't know how to define what each table needs to display. Here's the code:

.h:

{
NSArray *array1;
NSArray *array2;
IBOutlet UITableView *table1;
IBOutlet UITableView *table2;
}

.m:

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

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

- (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] autorelease];
    }

    cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
}

此代码仅适用于一个表。我不知道如何使用array2设置其他表。你有什么想法吗?

This code is working for just one table. I don't know how to set up the other table with array2. Have you got any ideas, people?

推荐答案

你需要做的就是检查哪个 UITableView 您正在委托/数据源方法中进行设置。尝试:

All you need to do is check which UITableView you are setting up in the delegate/datasource methods. Try:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
  if(tableView == table1){
    return 1; // The number of sections in table1;
  }
else if(tableView == table2){
    return 1; // The number of sections in table2;
  }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
  if(tableView == table1){
    return [array1 count];
  }
  else if(tableView == table2){
    return [array2 count];
  }
}

- (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] autorelease];
  }
  if(tableView == table1){
    cell.textLabel.text = [array1 objectAtIndex:indexPath.row];;
  }
  else if(tableView == table2){
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];;
  }
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; {
  UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
}

希望有帮助!

这篇关于如何处理两个TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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