第二个tableView没有加载数据

时间:2016-11-03 17:25:43

标签: ios objective-c uitableview

我正在开发一个在同一uitables中需要两个View的应用,因此我将两个tableViews拖到.xib文件中并添加以下代码。

@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@property (weak, nonatomic) IBOutlet UITableView *tableView2;

    @implementation SimpleTableViewController
    {
        NSArray *tableData1;
        NSArray *tableData2;
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.tableView1.dataSource = self;
        self.tableView1.delegate = self;

        self.tableView2.dataSource = self;
        self.tableView2.delegate = self;

        // Initialize table data
        tableData1 = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
        tableData2 = [NSArray arrayWithObjects:@"Egg Benedict2", @"1", @"2", @"3", @"4", @"5", nil];
    }


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == self.tableView1) {
              return [tableData1 count];
        }
        else {
              return [tableData2 count];
        }
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView == self.tableView1) {
           //...code...
           return cell;
        }
        else  {
             static NSString *simpleTableIdentifier = @"SimpleTableItem";

             UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

             if (cell == nil) {
                 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
              }

        cell.textLabel.text = [tableData2 objectAtIndex:indexPath.row];
        return cell;

        }


    }

正在运行应用numberOfRowsInSection cellForRowAtIndexPath未被调用,只有tableView1填充了数据     [self.tableView1 reloadData];也会numberOfRowsInSectioncellForRowAtIndexPath被调用,但tableView2始终为空,[self.tableView2 reloadData];无效tableView2

根据上面的例子,如何在两个表中显示值?缺少什么?

4 个答案:

答案 0 :(得分:0)

tableView:cellForRowAtIndexPathtableView:numberOfRowsInSection判断哪个tableview喜欢:

- (NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
  if (tableView == tableView1) {
    return 1;
  } else {
    return 1;
  }
}

答案 1 :(得分:0)

我认为此代码对您有帮助

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *tableData1;
    NSArray *tableData2;
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    tableview1.dataSource = self;
    tableview1.delegate = self;

    tableview2.dataSource = self;
    tableview2.delegate = self;
    tableData1 = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
    tableData2 = [NSArray arrayWithObjects:@"Egg Benedict2", @"1", @"2", @"3", @"4", @"5", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 1) {
        return [tableData1 count];
    }
    else {
        return [tableData2 count];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1) {
        static NSString *simpleTableIdentifier = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [tableData1 objectAtIndex:indexPath.row];
        return cell;
    }
    else  {
        static NSString *simpleTableIdentifier = @"cell2";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [tableData2 objectAtIndex:indexPath.row];
        return cell;

    }
}

答案 2 :(得分:-1)

我认为您错过了为tableView 2设置委托。

您可以通过连接到文件的所有者来设置xib中的委托,也可以在viewDidLoad中设置

self.tableView2.delegate = self;

答案 3 :(得分:-1)

您是否设置了heightForRowAtIndexPath?