开发者

Holding NSView instances in an array

开发者 https://www.devze.com 2023-03-25 09:01 出处:网络
Is it possible to store an NSView object in a mutable array? As I understand it, the view will be an object so the array should be able to hold it. Specifically, I want to hold severa开发者_高级运维l

Is it possible to store an NSView object in a mutable array? As I understand it, the view will be an object so the array should be able to hold it. Specifically, I want to hold severa开发者_高级运维l instances of a nib file, which I think would be loaded with an NSNib init, and then addObject to the array.

The idea is to display an NSView in each of the rows of a column in a TableView. I think it can be done because iTunes does something similar (with what I think is an NSImage) in displaying album artwork in a list view.

Still, any knowledge on the subject (or link to an example or tutorial) would be very appreciated.


TableViews usually don't hold an NSView for each item. They hold a number of NSViewTableCells (which are, system-wise, far more lightweight than NSViews), and they re-use these cells. They usually don't have many more cells than necessary to display the visible part of the TableView, AFAIK, and when the view is scrolled, cells that have become "invisible" are re-used.

So the best way to do this is to subclass the cell and to make the TableView display the contents using these. Using NSViews for every entry in a list of, say, my MP3 albums would be extremely expensive.


In Xcode goto File->New File and choose Objective-C class then in the drop down, choose to make it a subclass of UITableViewCell. Name it MyCell (for example)

Next in interface builder, create a new XIB and change its owner to your newly created class. You may also want to delete the default view and add a UITableViewCell. Set the owner's view outlet to this new tableview cell. Then add whatever you want to the UITableViewCell.

Then create a new UIViewController (which it may be helpful to create a new UITableViewController first and then change its type to UIViewController just so you get all the UITableViewDelegate methods added for you) and choose to add a XIB file for the UIViewController. Open the header file for the newly created UIViewController and add UITableViewDelegate protocol so the header may look like this:

@interface MyViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>

On the view for the view controller (in interface builder) add a UITableView and then set its datasource and delegates to the owner. Make sure to import the MyCell.h header file. Then implement the tableViewMethods in particular for your UITableViewCell you would do something like this:

       - (UITableViewCell *)tableView:(UITableView *) tableView
             cellForRowAtIndexPath:(NSIndexPath *) indexPath
    {
        static NSString *MyCellIdentifier = @"MyCellIdentifier";

        MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];

            cell = [nib objectAtIndex:0];   
        }

    //Here you would set properties of the cells items like labels, etc.
return cell;
    }
0

精彩评论

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