单元格异常中的NSTableView绘图按钮 - 它们不应该在那里

时间:2017-02-28 21:48:51

标签: objective-c macos cocoa nstableview

我有一个以编程方式填充的NSTableView ...对于每一行,我检查一些变量的值并适当填充单元格。一切都运作良好,所有单元格都正确填写,按钮都按我的要求绘制。问题是,有时某些行会绘制一个错误的按钮,不应该在那里......

对于“状态”列,如果p.status的值为“已批准”或“待定”或“已拒绝”,则会绘制并填充该按钮。这很有效。

如果p.status的值为“Completed”,则不应绘制按钮,而应将文本插入单元格中。这也有效。

但是你可以看到,有时会出现错误的按钮。我已经解决了这个问题两天了,无法解决这个问题。由于边框是黑色而不是灰色,因此在其他按钮下方也会显示出某些内容。

我是否需要首先清除旧图纸的NSTableView,我该怎么做?

enter image description here

这是填充tableView的全部代码。 (我知道,这很长,有点粗壮)

- (NSView *)tableView:(NSTableView *)table_view viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Proposal *p = [list objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
NSString *holdingValue;
NSString *holdingValue2;
NSString *holdingValue4;
NSString *statusString;

NSTableCellView *cell = [table_view makeViewWithIdentifier:identifier owner:self];

NSString * today = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
NSInteger todayInt = [today intValue];

//Add a notes HUD to the item number cell
if ([identifier isEqualToString:@"itemNumber"]){
    cell.textField.stringValue = [p valueForKey:identifier];
    if(p){
        //Create notes button
        NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(10, 3, 35, 20)];
        [cell addSubview: myButton];
        [myButton setBordered:NO];
        [myButton setButtonType:NSMomentaryLightButton];
        [myButton setBezelStyle:NSRoundedBezelStyle];
        [myButton setTarget:self];
        [myButton setAction:@selector(notesHUD:)];

        //Change color of notes image
        if(p.notes.length > 1){
            [myButton setImage:[NSImage imageNamed:@"Note2.png"]];
        } else {
            [myButton setImage:[NSImage imageNamed:@"Note1.png"]];
        }
    }

}else if([identifier isEqualToString:@"status"]){
    cell.textField.stringValue = @"";
    if(p){
        //Create popup button
        NSPopUpButton *button = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(2, 1, 200, 22) pullsDown:NO];
        [button setNeedsDisplay:YES];
        [button setBordered:YES];
        [button setButtonType:NSMomentaryLightButton];
        [button setBezelStyle:NSRoundedBezelStyle];
        [button addItemWithTitle:@"Pending"];
        [button addItemWithTitle:@"Approved"];
        [button addItemWithTitle:@"Rejected"];
        [button addItemWithTitle:@"Completed"];
        [button setAction:@selector(selectedAction:)];

        //Check which button we need
        if ([p.status isEqualToString:@"Approved"]){
            NSString *proposalCreationDate = p.dateStatusChanged;
            NSInteger proposalCreationDateInt = [proposalCreationDate intValue];
            NSInteger secondsBetween = (todayInt-proposalCreationDateInt);
            NSInteger numberOfDays = secondsBetween / 86400;
            NSInteger daysSinceCreation = numberOfDays;
            if (numberOfDays == 1){
                statusString = [NSString stringWithFormat:@"%@%ld%@", @"Approved (for ",(long)daysSinceCreation, @" day)"];
            } else {
                statusString = [NSString stringWithFormat:@"%@%ld%@", @"Approved (for ",(long)daysSinceCreation, @" days)"];
            }
            [cell addSubview:button];
            [button removeItemWithTitle:@"Approved"];
            [button setTitle:statusString];
            [button synchronizeTitleAndSelectedItem];
        }else if ([p.status isEqualToString:@"Pending"]){
            NSString *proposalCreationDate = p.dateCreated;
            NSInteger proposalCreationDateInt = [proposalCreationDate intValue];
            NSInteger secondsBetween = (todayInt-proposalCreationDateInt);
            NSInteger numberOfDays = secondsBetween / 86400;
            NSInteger daysSinceCreation = numberOfDays;
            if (numberOfDays == 1){
                statusString = [NSString stringWithFormat:@"%@%ld%@", @"Pending (for ",(long)daysSinceCreation, @" day)"];
            } else {
                statusString = [NSString stringWithFormat:@"%@%ld%@", @"Pending (for ",(long)daysSinceCreation, @" days)"];
            }
            [cell addSubview:button];
            [button removeItemWithTitle:@"Pending"];
            [button setTitle:statusString];
            [button synchronizeTitleAndSelectedItem];
//    
//Here we don't need a button so we just insert text into the cell but sometimes a button gets drawn here.
//
        }else if ([p.status isEqualToString:@"Completed"]){
            NSString *_date = @"";
            if (p.dateCompleted.length > 2){
                NSTimeInterval epoch = [p.dateCompleted doubleValue];
                NSDate * date = [NSDate dateWithTimeIntervalSince1970:epoch];
                NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
                [_formatter setLocale:[NSLocale currentLocale]];
                [_formatter setDateFormat:@"M/d/y"];
                _date=[_formatter stringFromDate:date];
                statusString = [NSString stringWithFormat:@"%@%@", @"Completed on ",_date];
                cell.textField.stringValue = statusString;
                [button removeFromSuperview];
            } else {
                statusString = @"Completed";
                cell.textField.stringValue = statusString;
                [button removeFromSuperview];
            }
        }else if ([p.status isEqualToString:@"Rejected"]){
            [button selectItemWithTitle:@"Rejected"];
            [cell addSubview:button];
            [button setTitle:@"Rejected"];
            [button synchronizeTitleAndSelectedItem];
        }
    }
}else if ([identifier isEqualToString:@"clientAccessPoint"]){
    holdingValue = [p valueForKey:identifier];
        if (!holdingValue){
            cell.textField.stringValue = @"N/A";
        }else{
            cell.textField.stringValue = [p valueForKey:identifier];
        }
}else if ([identifier isEqualToString:@"uniqueClientID"]){
    holdingValue2 = [p valueForKey:identifier];
        if (!holdingValue2){
            cell.textField.stringValue = @"N/A";
        }else{
            cell.textField.stringValue = [p valueForKey:identifier];
        }
}else if ([identifier isEqualToString:@"dateCreated"]){
    holdingValue4 = [p valueForKey:identifier];
    NSTimeInterval epoch = [holdingValue4 doubleValue];
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:epoch];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setLocale:[NSLocale currentLocale]];
    [_formatter setDateFormat:@"M/d/y"];
    NSString *_date=[_formatter stringFromDate:date];
    cell.textField.stringValue = _date;
}else{
cell.textField.stringValue = [p valueForKey:identifier];
}
return cell;
}

---- ---- UPDATE

我已经能够成功地缓解重叠绘图,但是当使用搜索时我仍然得到了不稳定的结果......这是更新:

在我的标题文件中:

@public
IBOutlet NSPopUpButton *button;
IBOutlet NSButton *notesButton;

在我的实施文件中,我按如下方式创建按钮:

- (void) awakeFromNib{
//Create popup button
button = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(2, 1, 200, 22) pullsDown:NO];
[button setNeedsDisplay:YES];
[button setBordered:YES];
[button setButtonType:NSMomentaryLightButton];
[button setBezelStyle:NSRoundedBezelStyle];
[button setAction:@selector(selectedAction:)];
}

然后我就这样对待他们:

[cell addSubview:button];
[button addItemWithTitle:@"Pending"];
[button addItemWithTitle:@"Rejected"];
[button addItemWithTitle:@"Completed"];
[button setTitle:@"Approved"];
[button synchronizeTitleAndSelectedItem];
[button setHidden:NO];

1 个答案:

答案 0 :(得分:1)

Tableview单元格可以重复使用。如果每次出现时向单元格添加一个按钮,滚动时按钮将叠加在一起。

您可以通过运行项目然后找到Debug View Hierarchy按钮(它在调试栏中看起来像两个垂直的矩形)来验证这一点。使用此选项可以旋转视图以查看图层。它非常有用于确定视图是否正在绘制,被其他内容隐藏,还是在屏幕外。

要解决此问题,只需在awakeFromNib中创建按钮,然后隐藏和取消隐藏它,并根据需要更改文字。