在基于视图的NSTableView中使用checkBox发出问题

Its*_*ret 8 objective-c interface-builder nstableview nsbutton

我有一个包含所有数据的NSDictionary:

  • 一个标题(对这个问题不重要)
  • 一个链接(对这个问题不重要)
  • 一个NSDictionary数组,再次包含1个标题和1个链接

我在基于视图的表视图中显示这些数据,如下所示:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv
{
    if (tv == _downloadTable) 
    //I use this "if" because I have another tableView that has nothing to do
    //with this one
    {
        return [[myDictionary objectForKey:@"myArray"] count];
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要2列tableView,一列用于显示标题,另一列用复选框,这样可以让我知道哪一行被选中.

- (NSView *)tableView:(NSTableView *)tv viewForTableColumn :(NSTableColumn *)tableColumn row :(NSInteger)row 
{
    if (tv == _downloadTable) 
    {
        if (tableColumn == _downloadTableTitleColumn) 
        {
            if ([[[myDictionary         objectForKey:@"myArray"]objectAtIndex:row]objectForKey:@"title"]) 
            {
            NSString *title = [[[myDictionary objectForKey:@"myArray"]objectAtIndex:row]objectForKey:@"title"];
            NSTableCellView *result = [tv makeViewWithIdentifier:tableColumn.identifier owner:self];
            result.textField.stringValue = title;
            return result;
            }
        }
       if (tableColumn == _downloadTableCheckColumn) 
       {
           NSLog(@"CheckBox"); //I wanted to see exactly when that was called
                               //But it didn't help me :(
           NSButton *button = [[NSButton alloc]init];
           [button setButtonType:NSSwitchButton];
           [button setTitle:@""];
           return button;
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我运行它并单击复选框时它什么都不做(当然因为我不知道如何让它做某事.我应该把代码应该放在哪里?

主要目标是可编辑的下载列表,现在显示列表,每行的标题旁边都有复选框.我想知道哪个checkBox被检查,哪个不是.

我试过这个:

[button setAction:@selector(checkBoxAction:)];

- (void)checkBoxAction: (id)sender
{
   NSLog(@"I am button : %@ and my state is %ld", sender, (long)[sender state]);
}
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何获取该按钮的行,以了解哪个标题与此checkBox相关联.

我也尝试setObjectValuetableView没有成功的方法.

我希望它的工作方式是:

我有一个"开始下载"按钮,检查是否选中了每个复选框,并仅使用选中的行启动下一个操作(下载).

我想避免绑定,因为我打算让它在iOS上运行,我不想为iOS提供不同的代码.

tru*_*cks 1

您可以尝试使用按钮的标签属性为您在表格视图中放置为数字(位置)的每个按钮设置它。看这里!!!

检测 UITableView 中哪个 UIButton 被按下

[编辑1]

如果人们真的决定阅读链接的帖子,您就会意识到答案实际上就在那里。

尝试添加:

[button setTag:row];
[button addTarget:self action:@selector(checkBoxAction:) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

在 viewForTableColumn 例程的 else 内:

在您的 checkBoxAction 例程中:

- (void)checkBoxAction: (id)sender{
   NSLog(@"I am button : %@ and my state is %@", sender.tag, [sender state]);
}
Run Code Online (Sandbox Code Playgroud)

我还认为,一旦您开始进一步深入研究代码,您就会想要开始使用 TableViewCell 对象的自动出队功能。我相信您会发现自己陷入内存分配/释放问题。