Files
cappuccino/Tests/Manual/TableTest/CPTableViewViewBased/AppController.j
T
cacaodev 67f7649f6c CPTableView with Lion's view-based API
Added : -makeViewWithIdentifier:owner:
	  -rowForView: columnForView: less performant tahn cocoa because we cycle through all visible rows. But these methods are generally used once when editing manually.
  Added identifier property to CPView.

  retro and forward (in IB) compatible with the existing API. You can use IB table view based without the new API. Existing table views / Nib won't break.

  CPTableView view based example. Featuring: IB made data views, different views in the same column, subviews binding in IB

When the view is found automatically in the cib, instantiate with _delegate as the owner (cocoa behavior).
2012-04-13 09:19:23 +02:00

116 lines
3.3 KiB
Plaintext

/*
* AppController.j
* CPTableViewViewBased
*
* Created by cacaodev.
* Copyright 2011, Your Company All rights reserved.
*/
@import <Foundation/CPObject.j>
@implementation AppController : CPObject
{
CPTableView tableView;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
[self buildModel];
tableView = [[CPTableView alloc] initWithFrame:CGRectMakeZero()];
[tableView setDataSource:self];
[tableView setDelegate:self];
var columnA = [[CPTableColumn alloc] initWithIdentifier:"A"];
[tableView addTableColumn:columnA];
[[columnA headerView] setStringValue:"A"];
[columnA setWidth:175];
var columnB = [[CPTableColumn alloc] initWithIdentifier:"B"];
[tableView addTableColumn:columnB];
[[columnB headerView] setStringValue:"B"];
[columnB setWidth:175]
var columnC = [[CPTableColumn alloc] initWithIdentifier:"C"];
[tableView addTableColumn:columnC];
[[columnC headerView] setStringValue:"C"];
[columnC setWidth:175];
var columnD = [[CPTableColumn alloc] initWithIdentifier:"D"];
[tableView addTableColumn:columnD];
[[columnD headerView] setStringValue:"D"];
[columnD setWidth:175];
var columnE = [[CPTableColumn alloc] initWithIdentifier:"E"];
[tableView addTableColumn:columnE];
[[columnE headerView] setStringValue:"E"];
[columnE setWidth:175];
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(100,100,700,400)];
[scrollView setDocumentView:tableView];
[contentView addSubview:scrollView];
[theWindow orderFront:self];
}
- (void)buildModel // For the editable view aka the slider
{
content = [];
var count = 10000;
while (count--)
content.push({A:10,B:10,C:10,D:10,E:10});
}
- (IBAction)sliderAction:(id)sender
{
var row = [tableView rowForView:sender];
var column = [tableView columnForView:sender];
var identifier = [[[tableView tableColumns] objectAtIndex:column] identifier];
//CPLogConsole(_cmd + sender + " row = " + row + " column = " + column);
content[row][identifier] = [sender value];
}
- (int)numberOfRowsInTableView:(id)aTableView
{
return content.length;
}
- (void)tableView:(CPTableView)aTableView dataViewForTableColumn:(CPTableColumn)aTableColumn row:(int)aRow
{
var n = (aRow % 3),
viewKind = "view_kind_" + n,
tableColumnId = [aTableColumn identifier];
var view = [aTableView makeViewWithIdentifier:viewKind owner:self];
if (view == nil)
{
if (n == 0)
view = [[CPTableCellView alloc] initWithFrame:CGRectMakeZero()];
else if (n == 1)
{
view = [[CPSlider alloc] initWithFrame:CGRectMakeZero()];
[view setMinValue:0];
[view setMaxValue:20];
[view setTarget:self];
[view setAction:@selector(sliderAction:)];
}
else
view = [[CPLevelIndicator alloc] initWithFrame:CGRectMakeZero()];
[view setIdentifier:viewKind];
}
if (n == 0)
[view setObjectValue:("Column " + tableColumnId + " Row " + aRow)];
else if (n == 1)
[view setValue:content[aRow][tableColumnId]];
return view;
}
@end