【问题标题】:Second tableView not loading data第二个tableView不加载数据
【发布时间】:2016-11-07 17:01:09
【问题描述】:

我正在开发一个需要两个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;

        }


    }

运行应用程序,numberOfRowsInSectioncellForRowAtIndexPath 都没有被调用,只是 tableView1 被数据填充后 [self.tableView1 reloadData]; 还调用了 numberOfRowsInSectioncellForRowAtIndexPath,但 tableView2 始终为空,[self.tableView2 reloadData]; 在此 tableView2 中不起作用。

根据上面的例子,如何在两个表中显示值?有什么遗漏吗?

【问题讨论】:

  • 你的班级是dataSourcedelegatetableViews 吗?
  • @Adeel 是的,他有,在viewDidLoad
  • 如果您将if (tableView == self.tableView1) 更改为if (tableView == self.tableView2),在tableView:cellForRowAtIndexPathtableView:numberOfRowsInSection: 中,表格是否交换内容(所以tableView1 为空白且tableView2 已填充)?跨度>
  • 是的,表格交换了内容,但内容只显示在tableView1tableView2继续为空
  • 确保您的表格链接到 IB 中的 ivars。

标签: ios objective-c uitableview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我认为这段代码对你有帮助

    @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;
    
        }
    }
    

    【讨论】:

      【解决方案3】:

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

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

      self.tableView2.delegate = self;
      

      【讨论】:

      • 不,他在viewDidLoad中设置了数据源和委托。
      【解决方案4】:

      你设置 heightForRowAtIndexPath 了吗?

      【讨论】:

      • 你可以在评论区提问。这不是回答的方式。
      猜你喜欢
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多