Skip to content

Instantly share code, notes, and snippets.

@evadne
Created March 12, 2011 10:57
Show Gist options
  • Save evadne/867189 to your computer and use it in GitHub Desktop.
Save evadne/867189 to your computer and use it in GitHub Desktop.
Reduced code from a custom CPTableView controlling CPViewController
@implementation IRSampleTableViewController : MPCompanionedViewController
// Assume that you work within -loadView, and assume arrayController is an CPArrayController somewhere
- (int) numberOfRowsInTableView:(CPTableView)inTableView {
return [[arrayController contentArray] count] || 0;
}
- (id) tableView:(CPTableView)inTableView objectValueForTableColumn:(CPTableColumn)inTableColumn row:(int)inRow {
return [[[arrayController arrangedObjects] objectAtIndex:inRow] objectForKey:[inTableColumn identifier]];
}
- (void) tableView:(CPTableView)inTableView sortDescriptorsDidChange:(CPArray)inOldDescriptors {
[arrayController setSortDescriptors:[CPArray arrayWithObjects:[[inTableView sortDescriptors] objectAtIndex:0]]];
[inTableView reloadData];
}
- (void) loadView {
[super loadView];
canReloadData = NO;
var reloadButton = [CPButton buttonWithTitle:@"Reload"];
[reloadButton sizeToFit];
[reloadButton setTarget:self];
[reloadButton setAction:@selector(handleButtonReloadData:)];
[reloadButton bind:@"enabled" toObject:self withKeyPath:@"canReloadData" options:null];
[buttonBar addRightButton:reloadButton];
historyView = [[CPTableView alloc] initWithFrame:[scrollView bounds]];
[historyView setDataSource:self];
[historyView setDelegate:self];
[historyView setRowHeight:20];
[historyView setGridStyleMask:CPTableViewSolidHorizontalGridLineMask | CPTableViewSolidVerticalGridLineMask];
[[{
identifier: "timestamp",
title: "Sent On",
headerAlignment: CPCenterTextAlignment,
textAlignment: CPCenterTextAlignment,
hasSortDescriptor: YES,
widths: [144, 144, 144]
}, {
identifier: "deviceCount",
title: "Devices",
headerAlignment: CPCenterTextAlignment,
textAlignment: CPRightTextAlignment,
contentInset: CGInsetMake(2, 6, 2, 2),
hasSortDescriptor: YES,
widths: [64, 64, 64]
}, {
identifier: "messageBody",
title: "Message",
hasSortDescriptor: NO,
lineBreakMode: CPLineBreakByTruncatingTail,
widths: [256, 512 - 144 - 64, nil]
}] enumerate:function (inItem) {
var column = [[CPTableColumn alloc] initWithIdentifier:inItem.identifier],
dataView = [IRTextField labelWithTitle:nil];
[[column headerView] setStringValue:inItem.title];
if (inItem.hasSortDescriptor == YES)
[column setSortDescriptorPrototype:[[CPSortDescriptor alloc] initWithKey:inItem.identifier ascending:YES]];
if (inItem.lineBreakMode != nil)
[dataView setValue:inItem.lineBreakMode forThemeAttribute:@"line-break-mode"];
if (inItem.headerAlignment != nil)
[[column headerView] setValue:inItem.headerAlignment forThemeAttribute:@"text-alignment"];
if (inItem.textAlignment != nil)
[dataView setValue:inItem.textAlignment forThemeAttribute:@"alignment"];
if (inItem.contentInset != nil) {
[dataView setValue:[CPColor clearColor] forThemeAttribute:@"bezel-color" inState:CPThemeStateTableDataView];
[dataView setValue:inItem.contentInset forThemeAttribute:@"content-inset" inState:CPThemeStateTableDataView];
}
if (inItem.widths[0] != nil) [column setMinWidth:inItem.widths[0]];
if (inItem.widths[1] != nil) [column setWidth:inItem.widths[1]];
if (inItem.widths[2] != nil) [column setMaxWidth:inItem.widths[2]];
[column setDataView:dataView];
[historyView addTableColumn:column];
}];
[historyView setColumnAutoresizingStyle:CPTableViewLastColumnOnlyAutoresizingStyle];
[historyView sizeLastColumnToFit];
[scrollView setDocumentView:historyView];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment