Skip to content

Instantly share code, notes, and snippets.

@cacaodev
Created December 1, 2010 22:03
Show Gist options
  • Save cacaodev/724313 to your computer and use it in GitHub Desktop.
Save cacaodev/724313 to your computer and use it in GitHub Desktop.
Test live variable row height in CPtableView
/*
* AppController.j
* TestTemplate
*
* Created by You on August 10, 2010.
* Copyright 2010, Your Company All rights reserved.
*/
@import <Foundation/CPObject.j>
var tableTestDragType = "tableTestDragType";
var totalCalls = 0;
var totalDur = 0;
@implementation AppController : CPObject
{
CPTableView table;
CPTableColumn columnA;
CPTableColumn columnB;
CPTableColumn columnC;
CPTableColumn columnD;
CPInteger rowHeight;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
rowHeight = 10;
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView],
button = [[CPButton alloc] initWithFrame:CGRectMake(10,10,100,24)];
[button setTitle:"toggle selection style"];
[button sizeToFit];
[button setTarget:self];
[button setAction:@selector(toggleStyle:)];
[contentView addSubview:button];
var scroll = [[CPScrollView alloc] initWithFrame:CGRectMake(100,100,700,400)];
table = [[CPTableView alloc] initWithFrame:CGRectMakeZero()];
[table setDataSource:self];
[table setDelegate:self];
[table setUsesAlternatingRowBackgroundColors:YES];
[table setGridStyleMask:CPTableViewSolidVerticalGridLineMask|CPTableViewSolidHorizontalGridLineMask];
[table setAllowsMultipleSelection:YES];
[table registerForDraggedTypes:[tableTestDragType]];
[table setIntercellSpacing:CGSizeMake(0,0)];
columnA = [[CPTableColumn alloc] initWithIdentifier:"A"];
[table addTableColumn:columnA];
[[columnA headerView] setStringValue:"A"];
[columnA setWidth:175];
columnB = [[CPTableColumn alloc] initWithIdentifier:"B"];
[table addTableColumn:columnB];
[[columnB headerView] setStringValue:"B"];
[columnB setWidth:175]
columnC = [[CPTableColumn alloc] initWithIdentifier:"C"];
[table addTableColumn:columnC];
[[columnC headerView] setStringValue:"C"];
[columnC setWidth:175];
columnD = [[CPTableColumn alloc] initWithIdentifier:"D"];
[table addTableColumn:columnD];
[[columnD headerView] setStringValue:"D"];
[columnD setWidth:175];
columnE = [[CPTableColumn alloc] initWithIdentifier:"E"];
[table addTableColumn:columnE];
[[columnE headerView] setStringValue:"E"];
[columnE setWidth:175];
[scroll setDocumentView:table];
[contentView addSubview:scroll];
// Left slider for a delegate driven row height
var slider = [[CPSlider alloc] initWithFrame:CGRectMake(300,10,200,30)];
[slider setMinValue:5];
[slider setMaxValue:30];
[slider setObjectValue:rowHeight];
[slider setTarget:self];
[slider setAction:@selector(setHeightOfRow:)];
[contentView addSubview:slider];
// Right slider for a simple global -setRowHeight:
var slider2 = [[CPSlider alloc] initWithFrame:CGRectMake(500,10,200,30)];
[slider2 setMinValue:25];
[slider2 setMaxValue:200];
[slider2 setObjectValue:rowHeight];
[slider2 setTarget:self];
[slider2 setAction:@selector(setRowHeight:)];
[contentView addSubview:slider2];
[theWindow orderFront:self];
}
- (int)numberOfRowsInTableView:(id)tableView
{
return 2000;
}
- (id)tableView:(id)tableView objectValueForTableColumn:(CPTableColumn)aColumn row:(int)aRow
{
return "Column " + [aColumn identifier] + " Row " + aRow;
}
- (void)setRowHeight:(id)sender
{
[table setRowHeight:[sender floatValue]];
var rows = table._exposedRows;
[table noteHeightOfRowsWithIndexesChanged:rows];
[table _layoutDataViewsInRows:rows columns:table._exposedColumns];
[table setNeedsLayout];
}
- (void)setHeightOfRow:(id)sender
{
rowHeight = ROUND([sender floatValue]);
var start = new Date();
[table noteHeightOfRowsWithIndexesChanged:table._exposedRows];
// The following has to be called inside -noteHeightOfRowsWithIndexesChanged:
[table _layoutDataViewsInRows:table._exposedRows columns:table._exposedColumns];
[table setNeedsLayout];
// OR:
// [table reloadData];
var end = new Date();
totalCalls++;
totalDur += (end - start);
CPLogConsole("average time pre row: " + ROUND(100*totalDur / totalCalls)/100);
}
- (int)tableView:(CPTableView)aTableView heightOfRow:(int)aRow
{
return aRow % 2 ? rowHeight : 30;
return aRow % 2 ? 1010 - (aRow * 10) : 10 + (aRow * 10);
}
- (BOOL)tableView:(CPTableView)aTableView isGroupRow:(int)aRow
{
return !(aRow % 5);
}
- (void)toggleStyle:(id)sender
{
var style = [table selectionHighlightStyle];
if (style === CPTableViewSelectionHighlightStyleSourceList)
[table setSelectionHighlightStyle:CPTableViewSelectionHighlightStyleRegular];
else
[table setSelectionHighlightStyle:CPTableViewSelectionHighlightStyleSourceList]
}
- (BOOL)tableView:(CPTableView)aTableView writeRowsWithIndexes:(CPIndexSet)rowIndexes toPasteboard:(CPPasteboard)pboard
{
var data = [rowIndexes, [aTableView UID]];
var encodedData = [CPKeyedArchiver archivedDataWithRootObject:data];
[pboard declareTypes:[CPArray arrayWithObject:tableTestDragType] owner:self];
[pboard setData:encodedData forType:tableTestDragType];
return YES;
}
- (CPDragOperation)tableView:(CPTableView)aTableView
validateDrop:(id)info
proposedRow:(CPInteger)row
proposedDropOperation:(CPTableViewDropOperation)operation
{
[aTableView setDropRow:(row) dropOperation:CPTableViewDropOn];
return CPDragOperationMove;
}
- (BOOL)tableView:(CPTableView)aTableView acceptDrop:(id)info row:(int)row dropOperation:(CPTableViewDropOperation)operation
{
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment