gpt4 book ai didi

objective-c - NSTableView 在单元格中绘制按钮异常 - 它们不应该在那里

转载 作者:行者123 更新时间:2023-12-03 17:47:59 26 4
gpt4 key购买 nike

我有一个以编程方式填充的 NSTableView...对于每一行,我检查一些变量的值并适本地填充单元格。一切工作正常,所有单元格均已正确填写,按钮均按我想要的方式绘制。问题是有时某些行绘制了不应该存在的错误按钮...

对于“status”列,如果 p.status 的值为“Approved”或“Pending”或“Rejected”,则会绘制并填充按钮。这有效。

如果 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;
}

----更新----

我已经能够成功地减轻重叠绘图的影响,但在使用搜索时我仍然得到不稳定的结果...以下是更新:

在我的头文件中:

@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];

最佳答案

Tableview 单元格被重用。如果每次单元格出现时都向单元格添加一个按钮,则当您滚动时,这些按钮将彼此堆叠在一起。

您可以通过运行项目然后查找 Debug View Hierarchy 来验证这一点按钮(它看起来像调试栏中两个相互重叠的垂直矩形)。使用此功能,您可以旋转 View 以查看图层。它对于确定 View 是否没有被绘制、被其他东西隐藏或者在屏幕外非常有用。

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

关于objective-c - NSTableView 在单元格中绘制按钮异常 - 它们不应该在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42519384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com