Tôi đang cố gắng tạo danh sách nguồn cho một chương trình mới và tôi gặp sự cố nhỏ khi sử dụng NSOutlineView dựa trên chế độ xem. Mã của tôi hoạt động tốt bằng cách sử dụng một NSOutlineView dựa trên tế bào vì vậy tôi hơi bối rối về những gì đang xảy ra.outlineView: objectValueForTableColumn: byItem: không được gọi là
Đây là mã của tôi cho các đại biểu và dữ liệu nguồn:
#pragma mark -
#pragma mark NSOutlineView Delegate
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return (item == nil) ? YES : [(SourceListNode *)item groupItem];
}
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
if ([(SourceListNode *)item groupItem]) {
return [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
return [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
}
#pragma mark -
#pragma mark NSOutlineView Data Source
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return (item == nil) ? [sourceListNodes count] : [(SourceListNode *)item numberOfChildren];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return (item == nil) ? YES : ([(SourceListNode *)item numberOfChildren] > 0);
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return (item == nil) ? [sourceListNodes objectAtIndex:index] : [(SourceListNode *)item childAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
return [(SourceListNode *)item name];
}
Khi tôi chạy này thông qua các chương trình gỡ rối nó thấy rằng outlineView:objectValueForTableColumn:byItem:
không thực thi. Kết quả là một chế độ xem phác thảo không có văn bản. Các ô chính xác được tạo và tôi có thể chọn chúng và mở rộng chúng. Tôi đang thiếu gì?
CẬP NHẬT: Tôi đã xóa danh sách nguồn của mình bằng IB, thêm một danh sách mới và kết nối nó với đối tượng điều khiển của tôi. Kết quả tốt hơn, nhưng các ô tiêu đề có dòng chữ "HEADER CELL" và các ô con có "ô xem bảng".
Tôi đã chạy lại chương trình thông qua trình gỡ lỗi và lần này outlineView:objectValueForTableColumn:byItem:
đã được thực hiện. Nó không cư trú trong văn bản của tế bào.
sau đó tôi cập nhật outlineView:viewForTableColumn:item:
như sau:
- (id)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSTableCellView *result;
if ([(SourceListNode *)item groupItem]) {
result = [outlineView makeViewWithIdentifier:@"HeaderCell" owner:self];
}
else {
result = [outlineView makeViewWithIdentifier:@"DataCell" owner:self];
}
[[result textField] setStringValue:[(SourceListNode *)item name]];
return result;
}
Bây giờ mọi thứ hoạt động như mong đợi.
Vì vậy, trong ngắn hạn, tôi đã giải quyết được vấn đề của mình. Tuy nhiên, bây giờ tôi có một câu hỏi khác. Mục đích của outlineView:objectValueForTableColumn:byItem:
cho chế độ xem phác thảo dạng xem cơ sở là gì? Nó thực hiện, nhưng dường như không làm bất cứ điều gì.
Xin chào John, bạn đã từng tìm ra lý do tại sao objectValueForTableColumn là bắt buộc cho chế độ xem phác thảo dựa trên chế độ xem không? Tôi tự hỏi chính điều đó. – Indoor