Skip to content

Instantly share code, notes, and snippets.

@lprhodes
Created March 15, 2011 04:48
Show Gist options
  • Save lprhodes/870328 to your computer and use it in GitHub Desktop.
Save lprhodes/870328 to your computer and use it in GitHub Desktop.
An error with the CPTableView not displaying when first shown
@import <Foundation/CPObject.j>
@implementation QuestionListViewController : CPView
{
CPView view;
CPDictionary questions;
CPRect bounds;
CPScrollView tableViewScrollView;
CPTableView tableView;
}
- (id)initWithFrame:(CGRect)aFrame
{
view = [[CPView alloc] initWithFrame:aFrame];
bounds = aFrame;
[self setupQuestionList];
[self resetContentView];
[self setData];
return self;
}
- (void)setupQuestionList {
tableViewScrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(20, 50, 650, CGRectGetHeight(bounds)-80)];
[tableViewScrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[tableViewScrollView setAutohidesScrollers:NO];
[tableView setUsesAlternatingRowBackgroundColors:YES];
[tableView setAllowsMultipleSelection:YES];
tableView = [[CPTableView alloc] initWithFrame:[view bounds]];
[tableView setDataSource:self];
[tableView setDelegate:self];
var column = [[CPTableColumn alloc] initWithIdentifier:@"Name"];
[column setWidth:180];
[[column headerView] setStringValue:@"Reference"];
[tableView addTableColumn:column];
var column = [[CPTableColumn alloc] initWithIdentifier:@"Question"];
[column setWidth:300];
[[column headerView] setStringValue:@"Question"];
var dataView = [column dataView];
[dataView setLineBreakMode:CPLineBreakByWordWrapping];
[column setDataView:dataView];
[tableView addTableColumn:column];
var column = [[CPTableColumn alloc] initWithIdentifier:@"AnswerType"];
[[column headerView] setStringValue:@"Type"];
[tableView addTableColumn:column];
[tableViewScrollView setDocumentView:tableView];
[view addSubview:tableViewScrollView];
[tableView reloadData];
}
-(void)resetContentView {
[tableViewScrollView setHidden:YES];
}
/*
Question Data Methods
*/
-(void)setData {
var request = [CPURLRequest requestWithURL:"/questions.html"];
var connection = [[CPURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]
}
- (void)connection:(CPURLConnection)connection didReceiveData:(CPString)sData
{
var result = JSON.parse(sData);
questions = [CPDictionary dictionaryWithJSObject:result recursively:YES];
connection = nil;
[tableView reloadData];
[tableViewScrollView setHidden:NO];
}
- (void)connection:(CPJSONPConnection)aConnection didFailWithError:(CPString)error {
alert(error); //a network error occurred
}
/*
Table View Methods
*/
- (int)numberOfRowsInTableView:(CPTableView)aTableView
{
return [questions count];
}
- (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)aTableColumn row:(int)aRow
{
var question = [questions objectForKey:[CPNumber numberWithInt:aRow]];
return [question objectForKey:[aTableColumn identifier]];
}
- (float)tableView:(CPTableView)aTableView heightOfRow:(int)aRow
{
var question = [questions objectForKey:[CPNumber numberWithInt:aRow]];
var questionText = [question objectForKey:@"Question"];
var size = [questionText sizeWithFont:[CPFont systemFontOfSize:14] inWidth:300];
return size.height + 30;
}
[{"ID":"31","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"32","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"33","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"34","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"35","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"36","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"37","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"38","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"39","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"40","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"41","Name":"New Question","Question":"This is my question and it's quite a bit longer than the others","AnswerType":"1","SurveyID":"2"},{"ID":"42","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"43","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"44","Name":"New Question","Question":"New Question","AnswerType":"1","SurveyID":"2"},{"ID":"56","Name":"My Question","Question":"What is..?","AnswerType":"1","SurveyID":"2"},{"ID":"57","Name":"My Question","Question":"What is..?","AnswerType":"1","SurveyID":"2"},{"ID":"58","Name":"My Question","Question":"What is..?","AnswerType":"1","SurveyID":"2"},{"ID":"59","Name":"My Question","Question":"What is..?","AnswerType":"1","SurveyID":"2"},{"ID":"60","Name":"My Question","Question":"What is..?","AnswerType":"1","SurveyID":"2"},{"ID":"61","Name":"My Question","Question":"What is..?","AnswerType":"1","SurveyID":"2"},{"ID":"62","Name":"My Question","Question":"What is..?","AnswerType":"1","SurveyID":"2"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment