Ios 一个控制器中的两个表视图

Ios 一个控制器中的两个表视图,ios,xcode,uitableview,Ios,Xcode,Uitableview,所以我试图在一个视图中创建两个表视图,但遇到了一些问题。 我读过一些关于如何做的其他回应,但它们并没有完全帮助我 在我的.h文件中,我为两个视图创建了两个出口,分别称为myFirstViewText和mySecondViewTex 因此,在我的m文件中-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath{ 我希望能够在每个不同的控制器中打印出不同的值,但我不太

所以我试图在一个视图中创建两个表视图,但遇到了一些问题。 我读过一些关于如何做的其他回应,但它们并没有完全帮助我

在我的.h文件中,我为两个视图创建了两个出口,分别称为
myFirstViewText
mySecondViewTex

因此,在我的m文件中-
(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath{

我希望能够在每个不同的控制器中打印出不同的值,但我不太确定,因为您只返回1个单元格

到目前为止,我已经做到了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Rx";
static NSString *CellIdentifier2 = @"allergies";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
UITableViewCell *cell2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier2];
if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (!cell2) {
    cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
if (tableView == self.myFirstTextView ) {
       cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
if (tableView == self.mySecondTextView) {
      cell.textLabel.text = @"BYE JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
tableView = self.mySecondTextView;
cell2.textLabel.text = @"I love Jazzy :D";
return cell2;
这在我的第一个桌面视图中打印“我喜欢爵士乐”,而在第二个桌面视图中没有打印任何内容。为什么会发生这种情况,我如何修复它?
谢谢:D

此方法由所有将类的实例设置为其数据源的TableView调用

这意味着您需要检查哪个tableView请求的单元格号是某某

因此,您的方法基本上应该如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == rxTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    else if (tableView == allergiesTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    return nil; //because you don't expect any other tableView to call this method
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
    if (tableView.tag == 1)
         return 1;
    else if (tableView.tag == 2)
         return 2;
  }

一种我不太常看到的实现方法是实际使用NSObject子类作为每个表的委托和数据源,而不是视图控制器。它消除了对“if(tableView==someTable)…”代码的需要,并且可能使视图控制器和UITableView代码都可维护和可读


您将为每个表创建一个NSObject子类。这些子类将包含所需的UITableView数据源和委托方法的实现。然后,您将在视图控制器中实例化其中一个,并将每个实例连接到相应的表。

我的建议是在两个表视图上设置一个标记,然后在tableview数据源方法检查表视图所处的标记。例如,在代码中可以执行以下操作:

myFirstTableView.tag = 1;
mySecondTableView.tag = 2;
等你回来的时候

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView.tag == 1) 
   cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row];

   if (tableView.tag == 2) 
   cell.textLabel.text = @"BYE JAZZY";
 }
另外,如果您有两个不同大小的表视图,则可以通过以下方式实现:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == rxTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    else if (tableView == allergiesTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    return nil; //because you don't expect any other tableView to call this method
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
    if (tableView.tag == 1)
         return 1;
    else if (tableView.tag == 2)
         return 2;
  }
问题1:

tableView = self.mySecondTextView;     
cell2.textLabel.text = @"I love Jazzy :D";
return cell2;
您在这里所做的是始终返回
cell2
,设置其文本“I love jazzy:D”,这样它将始终填充第一个表视图,因为您在进入方法之前不会设置
tableView=mysecondextview
。代理始终以
firstTableView
为目标

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
问题2:

if (tableView == self.myFirstTextView ) {
    cell.textLabel.text = @"HI JAZZY";
    //[RxDict objectAtIndex:indexPath.row];

    //for printing in first table view return cell here like

    return cell;
}
if (tableView == self.mySecondTextView) {
    cell.textLabel.text = @"BYE JAZZY";
    //[RxDict objectAtIndex:indexPath.row];

    //for printing in second tableview return cell two here

    return cell2;
}

在进入此函数之前,请确保设置了要设置目标的表。您可以在
numberofsection
方法、view
didload
方法或您要显示或设置目标表视图的其他地方(如单击某个按钮)进行设置。但您不能在此处设置。

为什么这个问题被否决?请注意,返回nil将导致程序崩溃…您最好返回一个空白单元格或提醒用户出现无法恢复的错误,然后让他们重新启动应用程序。@lnafziger您是对的,这将导致应用程序崩溃。我认为这是一件好事。您不需要这样做不要期望任何您不知道的TableView请求数据,而且您无论如何也无法提供数据。如果不能像这里这样静态检查某些内容,那么尽早崩溃是好的。无论如何,这无关紧要-返回的内容应该无关紧要,因为永远不应该到达该行。实际上,在我看来,崩溃是不好的n您可以提供一个受控关闭。但仍然值得向那些不知道何时将其包含在代码中的人指出。:)