【问题标题】:Working with two tables - how to get the correct rows [closed]使用两个表 - 如何获得正确的行 [关闭]
【发布时间】:2013-05-31 15:39:14
【问题描述】:

我有两个基于单元格的表格视图(项目和类别)。我正在尝试为每个获取正确的行索引,以便可以使用它们的数组。

到目前为止我的代码是这样的:

- (id) tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
         row:(NSInteger)row {
    //NSLog(@"%s", __FUNCTION__);

    if (tableView.tag == 0) {
        currentItem = [itemMutableArray objectAtIndex:row];
        NSString *itemTitle1 = [currentItem valueForKey:@"title"];
        return itemTitle1;
    } else {
        currentCategory  = [categoryMutableArray objectAtIndex:row];
        NSString *catName = [currentCategory valueForKey:@"name"];
        NSLog (@"currentCategory is %@", catName);
        return catName;
    }
}

但是对于每个表,这会返回类似:

 currentCategory is Cat One
 currentCategory is Cat Three
 currentCategory is Cat One
 currentCategory is Cat Three
 currentCategory is Cat One
 currentCategory is Cat Three
 currentCategory is Cat Two
 currentCategory is Cat One
 currentCategory is Cat Three
...

是我使用了错误的方法,还是什么?谢谢。

【问题讨论】:

  • 我希望能够识别每个表的具体行。
  • 我们需要知道您的数组的内容以及日志应该说什么。

标签: cocoa osx-mountain-lion


【解决方案1】:

如果您在同一个控制器中有多个表,最好为它们设置插座。

那么你可以使用 as:

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    if(tableView==myFirstTableViewOutlet){
      .....
    }
    else if(tableView==mySecondTableViewOutlet){
      .....
    }
}

【讨论】:

  • 谢谢 Anoop,我会试试这个..
  • 做到了.. 非常感谢
  • 知道为什么 tableview.tag 不起作用,但选择插座可以吗?
  • @DavidDelMonte:坦率地说,我从未尝试过使用标签。我想标签应该可以工作。再次检查您是否已为表格设置标签而不是滚动视图,同时确保两个表格具有不同的标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 2012-05-27
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多