[cocoa]_[primary]_[nstableview--data operation and table operation to be aware of issues]

Source: Internet
Author: User

1. First create a nstableview in the MainMenu.lib file, display a table on the interface, and set the table.

2. Create Files TableViewDelegate.h and TABLEVIEWDELEGATE.M (file contents as shown in the file below). and create an object in the MainMenu.lib file (select object from the right side of the window, and pull to the dialog box in window), named: Tableviewdelegate.

3. Set up proxy: TableView to set up and connect to the table view in the picture.

The Staticfield is connected to the Label control in the window.

DataSource and delegate agent to Tableviewdelegate.

(as shown)



The tabular data is consistent with the definition data.

The identifier of each column should be the same as the name of the variable defined in Mqjtabledata.

If the first column identifier is set to: Phoneid, the variable defined in Mqjtabledata is NSString *phoneid.

Other columns, likewise.



MqjTableData.h

#import <Cocoa/Cocoa.h> @interface mqjtabledata:nsobject{    nsstring *phoneid;    NSString *phonename;    NSString *phonenumber;} @property (readwrite,copy) nsstring *phoneid; @property (readwrite,copy) nsstring *phonename; @property (readwrite,copy ) NSString *phonenumber; @end

mqjtabledata.m

#import "MqjTableData.h" @implementation mqjtabledata@synthesize Phoneid; @synthesize phonename; @synthesize PhoneNumber; @end


TableViewDelegate.h

#import <Cocoa/Cocoa.h> @interface tableviewdelegate:nsobject{    nsmutablearray *tabledataarray;    Iboutlet Nstableview *tableview;        Iboutlet Nstextfield *staticfield; } @end

Tableviewdelegate.m

#import "TableViewDelegate.h" #import "MqjTableData.h" @implementation tableviewdelegate-(ID) init{[super Init];    if (self) {tabledataarray = [nsmutablearray new]; } return self;} -(void) awakefromnib{[self loadtabledata];}    -(void) loadtabledata{nsstring *name = @ "AAAA";    NSString *number [email protected] "13645897845";        for (int i = 1; i<=; ++i) {NSAutoreleasePool *pool = [NSAutoreleasePool new];                Mqjtabledata *tabledata =[mqjtabledata New];        [Tabledata setphoneid:[nsstring stringwithformat:@ "%d", I]];        [Tabledata setphonename:[nsstring stringwithformat:@ "%@_%d", Name,i]];                [Tabledata setphonenumber:[nsstring stringwithformat:@ "%lli", [number Longlongvalue]+i]];        [Tabledataarray Addobject:tabledata];        [TableView notenumberofrowschanged];                [Tabledata release];    [Pool drain]; }} #pragma mark Action methods#pragma Mark Table view DataSource methods-(int) numberofrowsiNtableview: (Nstableview *) atableview{return [Tabledataarray count];} -(ID) TableView: (Nstableview *) Atableviewobjectvaluefortablecolumn: (Nstablecolumn *) atablecolumn row: (int) Rowin dex{if (![    Tabledataarray Count]) {return nil;        } nsstring *identifier = [atablecolumn identifier];        Mqjtabledata *TD = [Tabledataarray objectatindex:rowindex]; return [TD Valueforkey:identifier];} -(void) TableView: (Nstableview *) Atableview Setobjectvalue: (ID) anobject fortablecolumn: (Nstablecolumn *)    Atablecolumn row: (int) rowindex{nsstring *identifier = [atablecolumn identifier];    Mqjtabledata *TD = [Tabledataarray objectatindex:rowindex]; [TD Setvalue:anobject Forkey:identifier];} -(void) TableView: (Nstableview *) Thetableview sortdescriptorsdidchange: (Nsarray *) Olddescriptors{[tabledataarray Sortusingdescriptors:[tableview Sortdescriptors]]; [TableView Reloaddata];} -(int) getrowcount{return [Tabledataarray count];} -(void) TableView: (NstaBleview *) Atableview Willdisplaycell: (ID) Acell fortablecolumn: (Nstablecolumn *) atablecolumn row: (NSInteger) RowIndex {if (![    Tabledataarray Count]) {return; }}-(void) Tableviewselectiondidchange: (nsnotification *) anotification//Click on a line this function will automatically respond. {    NSAutoreleasePool *pool = [NSAutoreleasePool new];    NSUInteger SelectItem =[ TableView selectedrow];    if (SelectItem = =-1)     {         return;   }    mqjtabledata *tabledata =[tabledataarray objectAtIndex: selectitem];        nsstring *showlablestring =[nsstring stringwithformat:@ "ID:%@     name:%@    number:%@ ", [Tabledata phoneid],[tabledata Phonename],[tabledata phonenumber]];    [Staticfield setstringvalue:showlablestring];    [Pool drain];  }


Click a row to get the data for that row and display it on the label


[cocoa]_[primary]_[nstableview--data operation and table operation to be aware of issues]

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.