开发者

Populating TableView line by line in Obj-C

开发者 https://www.devze.com 2023-03-12 22:18 出处:网络
I\'m looking for a way to populate a table view from one single document, namely I want to load a .po file.

I'm looking for a way to populate a table view from one single document, namely I want to load a .po file. I would like开发者_如何学Python each line of my table view to load one line of text from the PO file.

Ideally, I would like to have one line in the first column, and the corresponding translation in the second column (to get a clear view of the contents).

I have not worked much with table views yet so please forgive my ignorance! I have done my research but I find the apple documentation confusing and very unclear -- and didn't find much online...

Thanks in advance for any help!


bbum is correct, you don't push data to your table, you provide it and the table displays it. Friday I did a quick mock-up on putting a text file displayed line by line, so maybe some of the code can help some.

Get a table view connected with an outlet to it's data source, then you can do something like this:

// Class variable in your table delegate object
    NSArray* lineList;
    IBOutlet NSTableView* table;

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [lineList count];
}


- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    return [lineList objectAtIndex:row];
}

// Be sure to use the proper encoding for your text file
// do something like this to load your text file.
- (void) loadData:(NSString*)ourPath
{
    NSError* err = nil;
    NSString* fullFileText = [NSString stringWithContentsOfFile:ourPath encoding:NSMacOSRomanStringEncoding error:&err];
    if (err)
        NSLog(@"Err: %@, %d", [err localizedDescription], [err code]);

    if (fullFileText)
    {
        lineList = [[fullFileText componentsSeparatedByString:@"\n"] retain];
        [table reloadData];
    }
}

In your case you may want to hold an array of dictionaries, using a different key for both versions of your text. That way you can have two columns. The NSTableColumn will tell you which column you will be drawing into when tableView:objectValueForTableColumn: gets called. The other option you have here is making a custom cell that has two fields in it but that's probably overkill for what you're asking.

Note also that there are a number of other optional delegate calls you can add for more flexibility of how you show your data.

Additionally for more dynamic complex tasks I've found that bindings are better. They can be confusing if you're not comfortable with them though. For simple tables it's often just as easy to go this route. Good luck!


You don't push data to a table view, it pulls data from you. This can be done with either bindings or by implementing the table view data sour (which are a little bit different, but mostly the same, between the NS* and UI* platforms).

The NSTableView and UITableView documentation both have links to examples and programming guides. Read those and if you still don't get it, ask a specific question.

0

精彩评论

暂无评论...
验证码 换一张
取 消