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

时间:2012-11-18 14:06:56

标签: ios xcode uitableview

所以我试图在一个视图中制作两个tableview,但我遇到了一些麻烦。 我已经阅读了一些关于如何做的其他回复,但它们并没有完全帮助我。

在我的.h文件中,我为两个观看了两个视频,称其为myFirstViewTextmySecondViewTex

所以在我的m文件中 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

我希望能够在每个不同的控制器中打印出单独的值,而且我不太确定,因为你只返回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;

这在我的第一个TableView中打印“我爱Jazzy”,在第二个没有打印。为什么会发生这种情况,我该如何解决? 谢谢:D

4 个答案:

答案 0 :(得分:7)

此方法被所有将您的类实例设置为其dataSource的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
}

答案 1 :(得分:4)

我的建议是在两个表视图上设置一个标记,然后在tableview中查看dataSource方法,检查表查看你的标签。例如,你可以在代码中的某些地方执行:

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

此外,如果您有2个不同大小的tableview,您可以这样做:

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

问题1:

tableView = self.mySecondTextView;     
cell2.textLabel.text = @"I love Jazzy :D";
return cell2;

你在这里做的总是返回cell2,设置它的文字“我喜欢jazzy:D”所以它总是会填满第一个表视图,因为在进入之前你不会设置tableView=mySecondTextView进入方法。代表始终定位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方法,视图didload方法或您要显示的其他位置或目标表视图(例如单击某个按钮)中进行设置。但是你不能在这里设置它。