Mac tree implementation method (nsoutlineview) 2 (implements simple selection and Dynamic Modification of different data)

Source: Internet
Author: User

Mac tree implementation method (nsoutlineview) 2 (implements simple selection and Dynamic Modification of different data)

Add the following content on the basis of nsoutlineview (mactree Implementation Method) 1:

Protocol added:

<Nsapplicationdelegate, nsoutlineviewdelegate, nsoutlineviewdatasource, nsmenudelegate>

Note that the delegate of the control needs to be bound to the class. We have already bound datasource to the same class.

Mainly to achieve the selection function:

// This function must bind the control's data delegate to this class (that is, not only the control's data source and the class are bound, but also the control delegate and the class are bound to respond to this function)

-(Void) outlineviewselectiondidchange :( nsnotification *) Notification

{

If ([_ outline_view_1selectedrow]! =-1)

{

Nsstring * Item = [_ outline_view_1itematrow: [_ outline_view_1selectedrow];

_ Lable1.stringvalue = item;

}

}

The following is the implementation code

H file

//  TreeGroup_DS.h//  Created by DMD on 24/9/14.#import <Foundation/Foundation.h>@interface TreeGroup_DS : NSObject <NSApplicationDelegate, NSOutlineViewDelegate, NSOutlineViewDataSource, NSMenuDelegate> {    //For NSoutlineView Level 1 Items    NSArray *_topLevelItems;        //For NSoutlineView child items    NSMutableDictionary *_childrenDictionary;        // Control NSOutlineView    IBOutlet NSOutlineView *_outline_view_1;        IBOutlet NSTextField *_Lable1;}@end


M file

//// Treegroup_ds.m // created by DMD on 24/9/14. // 1. implement Dynamic Method refresh, update the data in the tree (undone) # import "treegroup_ds.h" @ implementation treegroup_ds-(ID) Init {If (Self = [Super init]) {[self tree_refresh: 20 rootname: @ "Teacher Liu (3)"];} return self ;}- (void) loadview {}- (ibaction) onclick_bt_refresh :( ID) sender {[self tree_refresh: 3 rootname: @ "Teacher Liu (3)"];}-(ibaction) onclick_bt_refresh2 :( ID) sender {[self Tree_refresh: 5 rootname: @ "Teacher Zhang (5)"];}-(void) get_tree_data :( nsinteger) tree_count rootname :( nsstring *) item_name {// group names _ toplevelitems = [[nsarray arraywithobjects: item_name, nil] retain]; _ childrendictionary = [nsmutabledictionary new]; // Child names of group nsmutablearray * array = [nsmutablearray arraywithobjects: Nil]; nsstring * child_name = nil; For (INT I = 0; I <tree_count; I ++) {Child_name = [nsstring stringwithformat: @ "grade (% d)", I + 1]; [array addobject: child_name];} [_ childrendictionary setobject: array forkey: item_name]; nslog (@ "dictionary: % @", _ childrendictionary);}-(void) tree_refresh :( nsinteger) tree_count rootname :( nsstring *) item_name {[self get_tree_data: tree_count rootname: item_name]; [_ outline_view_1 sizelastcolumntofit]; [_ outline_view_1 reloaddata]; [_ outline_v Iew_1 keys: Yes]; [_ outline_view_1 setrowsizestyle: Keys]; // expand all the root items; disable the expansion animation that normally happens [nsanimationcontext begingrouping]; [[nsanimationcontext currentcontext] setduration: 0]; [_ outline_view_1 expanditem: Nil expandchildren: Yes]; [endgrouping];}-(void) dealloc {[_ toplevelitems release]; [_ Childrendictionary release]; [Super dealloc];} // must be function-(ID) outlineview :( nsoutlineview *) outlineview child :( nsinteger) index ofitem :( ID) item {return [[self _ childrenforitem: item] objectatindex: Index];}-(nsarray *) _ childrenforitem :( ID) item {nsarray * children; if (item = nil) {children = _ toplevelitems;} else {children = [_ childrendictionary objectforkey: item];} return children ;}// Must be function-(bool) outlineview :( nsoutlineview *) outlineview isitemexpandable :( ID) item {If ([outlineview parentforitem: item] = nil) {return yes ;} else {return no ;}// must be function-(nsinteger) outlineview :( nsoutlineview *) outlineview numberofchildrenofitem :( ID) item {return [[self _ childrenforitem: item] Count];} // must be to show item name-(ID) outlineview :( nsoutlineview *) outlineview Objectvaluefortablecolumn :( nstablecolumn *) tablecolumn byitem :( ID) item {ID objectvalue = nil; objectvalue = item; return objectvalue ;} // This function must bind the control's data delegate to this class (that is, not only the control's data source and the class are bound, but also the control delegate and the class are bound to respond to this function) -(void) outlineviewselectiondidchange :( nsnotification *) Notification {If ([_ outline_view_1 selectedrow]! =-1) {nsstring * Item = [_ outline_view_1 itematrow: [_ outline_view_1 selectedrow]; _ lable1.stringvalue = item ;}} @ end


Implementation result:

When you click refresh1, some data is displayed.

When refresh2 is clicked, the another data is displayed.






Test successful!

The above can be met: when the operation does not require small images, and then the data is added and deleted by refreshing all the data.

Summary by DMD






Mac tree implementation method (nsoutlineview) 2 (implements simple selection and Dynamic Modification of different data)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.