[英]Issue with a checkBox in a viewbased NSTableView
我有一個NSDictionary,其中包含所有數據:
我在這樣的基於視圖的表視圖中顯示此數據:
- (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];
}
}
我想要在此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;
}
}
}
現在,當我運行它並單擊復選框時,它什么也沒做(當然,因為我不知道如何使它做某事。我應該把應該做某事的代碼放在哪里?
主要目標是可下載的下載列表,現在將顯示該列表,並且每行標題旁邊都有一個復選框。 我想知道哪些復選框被選中,哪些未被選中。
我嘗試了這個:
[button setAction:@selector(checkBoxAction:)];
- (void)checkBoxAction: (id)sender
{
NSLog(@"I am button : %@ and my state is %ld", sender, (long)[sender state]);
}
但是我不知道如何獲取該按鈕的行,以知道哪個標題與此復選框相關聯。
我也嘗試了tableView
的setObjectValue
方法,但沒有成功。
我希望它的工作方式是:
我有一個“開始下載”按鈕,用於檢查每個復選框是否都已選中,並僅與選中的行一起啟動下一個操作(下載)。
我想避免綁定,因為我也打算使其在iOS上運行,並且我不想為iOS使用不同的代碼。
您可以使用NSTableView
方法-rowForView:
獲取特定視圖所在的行。
您的情況是這樣的:
- (void)checkBoxAction:(id)sender
{
NSInteger row = [_downloadTable rowForView:sender];
NSLog(@"The button at row %ld was clicked.", row);
}
這是NSTableView
的文檔: https : //developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/Reference/Reference.html
您可以嘗試使用按鈕的“標記”屬性將每個按鈕設置為表格視圖中的數字(位置)。 看這里!!!
[EDIT1]
如果人們確實決定閱讀鏈接的文章,您將意識到答案確實存在。
嘗試添加:
[button setTag:row];
[button addTarget:self action:@selector(checkBoxAction:) forControlEvents:UIControlEventTouchUpInside];
在viewForTableColumn例程的else中:
在您的checkBoxAction例程中:
- (void)checkBoxAction: (id)sender{
NSLog(@"I am button : %@ and my state is %@", sender.tag, [sender state]);
}
我還認為,一旦開始深入研究代碼,您將要開始使用TableViewCell對象的自動出隊功能。 我相信您將發現自己在內存分配/分配問題中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.