Skip to content

Commit

Permalink
Added basic support for table views.
Browse files Browse the repository at this point in the history
* UITableViewStyle not supported.
* UITableViewCellStyle not supported.
* Multiple sections not supported.
* Not all UITableViewCell properties supported yet.
* Consult the documentation for changes to how the view hierarchy is specified.
  • Loading branch information
jonathanellis committed Mar 1, 2012
1 parent 398189a commit 10530b7
Show file tree
Hide file tree
Showing 18 changed files with 341 additions and 29 deletions.
3 changes: 3 additions & 0 deletions Pegasus/PGAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#import <Foundation/Foundation.h>

@class PGView;

@protocol PGAdapter <NSObject>

+ (NSString *)name;
Expand All @@ -27,5 +29,6 @@

@optional
- (void)setValue:(NSString *)string forVirtualProperty:(NSString *)propertyName;
- (void)addSubview:(PGView *)subview;

@end
26 changes: 26 additions & 0 deletions Pegasus/PGTableView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// PGTableView.h
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGView.h"

@interface PGTableView : PGView <UITableViewDataSource> {
NSMutableArray *cells;
}

@end
80 changes: 80 additions & 0 deletions Pegasus/PGTableView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// PGTableView.m
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGTableView.h"

@implementation PGTableView

+ (NSString *)name {
return @"tableview";
}

+ (NSDictionary *)properties {

NSMutableDictionary *properties =[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"float", @"rowHeight",
@"UITableViewCellSeparatorStyle", @"separatorStyle",
@"UITextAlignment", @"textAlignment",
@"UIColor", @"separatorColor",
// @"UIView", @"backgroundView",
// @"UIView", @"tableHeaderView",
// @"UIView", @"tableFooterView",
@"float", @"sectionHeaderHeight",
@"float", @"sectionFooterHeight",
@"int", @"sectionIndexMinimumDisplayRowCount",
@"BOOL", @"allowsSelection",
@"BOOL", @"allowsMultipleSelection",
@"BOOL", @"allowsSelectionDuringEditing",
@"BOOL", @"allowsMultipleSelectionDuringEditing",
@"BOOL", @"editing",
nil];

[properties addEntriesFromDictionary:[PGView properties]];

return properties;
}

+ (Class)underlyingClass {
return [UITableView class];
}

- (void)addSubview:(PGView *)subview {
if ([subview isKindOfClass:[PGTableViewCell class]]) {
if (!cells) {
cells = [NSMutableArray array];
UITableView *tableView = (UITableView *)view;
tableView.dataSource = self;
}
[cells addObject:subview.view];
} else {
[super addSubview:subview];
}
}

#pragma mark - UITableViewDataSource methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [cells objectAtIndex:indexPath.row];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cells count];
}

@end
24 changes: 24 additions & 0 deletions Pegasus/PGTableViewCell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// PGTableViewCell.h
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGView.h"

@interface PGTableViewCell : PGView

@end
67 changes: 67 additions & 0 deletions Pegasus/PGTableViewCell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// PGTableViewCell.m
// Pegasus
//
// Copyright 2012 Jonathan Ellis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "PGTableViewCell.h"

@implementation PGTableViewCell

+ (NSString *)name {
return @"tableviewcell";
}

+ (NSDictionary *)properties {

NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"#", @"text",
@"#", @"text-font",
// @"#", @"detail-text",
@"#", @"image",
@"BOOL", @"editing",
@"BOOL", @"showsReorderControl",
@"int", @"indentationLevel",
@"float", @"indentationWidth",
nil];

[properties addEntriesFromDictionary:[PGView properties]];

return properties;
}

+ (Class)underlyingClass {
return [UITableViewCell class];
}

- (void)setValue:(NSString *)string forVirtualProperty:(NSString *)property {
[super setValue:string forVirtualProperty:property];

UITableViewCell *tableViewCell = (UITableViewCell *)view;

if ([property isEqualToString:@"text"]) {
tableViewCell.textLabel.text = string;
} else if ([property isEqualToString:@"text-font"]) {
tableViewCell.textLabel.font = [PGTranslators fontWithString:string];
} else if ([property isEqualToString:@"detail-text"]) {
tableViewCell.detailTextLabel.text = string;
} else if ([property isEqualToString:@"image"]) {
tableViewCell.imageView.image = [PGTranslators imageWithString:string];
}

}

@end
30 changes: 30 additions & 0 deletions Pegasus/PGTranslators.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@

+ (SEL)translatorForType:(NSString *)type;

#pragma mark - Translators (Primitives)
+ (int)intWithString:(NSString *)string;
+ (double)doubleWithString:(NSString *)string;
+ (float)floatWithString:(NSString *)string;
+ (BOOL)boolWithString:(NSString *)string;

#pragma mark - Translators (Structs)
+ (CGRect)rectWithString:(NSString *)string;
+ (CGSize)sizeWithString:(NSString *)string;
+ (CGPoint)pointWithString:(NSString *)string;
+ (CGAffineTransform)affineTransformWithString:(NSString *)string;
+ (UIEdgeInsets)edgeInsetsWithString:(NSString *)string;

#pragma mark - Translators (Enums)
+ (UITextAlignment)textAlignmentWithString:(NSString *)string;
+ (UITextBorderStyle)textBorderStyleWithString:(NSString *)string;
+ (UIViewAutoresizing)autoresizingWithString:(NSString *)string;
+ (UIViewContentMode)contentModeWithString:(NSString *)string;
+ (UILineBreakMode)lineBreakModeWithString:(NSString *)string;
+ (UITextFieldViewMode)textFieldViewModeFromString:(NSString *)string;
+ (UIScrollViewIndicatorStyle)scrollViewIndicatorStyleWithString:(NSString *)string;
+ (UIProgressViewStyle)progressViewStyleWithString:(NSString *)string;
+ (UITableViewCellSeparatorStyle)tableViewCellSeparatorStyleWithString:(NSString *)string;

#pragma mark - Translators (Objects)
+ (NSString *)stringWithString:(NSString *)string;
+ (UIColor *)colorWithString:(NSString *)string;
+ (UIImage *)imageWithString:(NSString *)string;
+ (UIFont *)fontWithString:(NSString *)string;

+ (NSArray *)componentsForTuple:(NSString *)string;

@end
8 changes: 8 additions & 0 deletions Pegasus/PGTranslators.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ + (SEL)translatorForType:(NSString *)type {
if ([type isEqualToString:@"UITextFieldViewMode"]) return @selector(textFieldViewModeWithString:);
if ([type isEqualToString:@"UIScrollViewIndicatorStyle"]) return @selector(scrollViewIndicatorStyleWithString:);
if ([type isEqualToString:@"UIProgressViewStyle"]) return @selector(progressViewStyleWithString:);
if ([type isEqualToString:@"UITableViewCellSeparatorStyle"]) return @selector(tableViewCellSeparatorStyleWithString:);

// Objects:
if ([type isEqualToString:@"NSString"]) return @selector(stringWithString:);
Expand Down Expand Up @@ -181,6 +182,13 @@ + (UIProgressViewStyle)progressViewStyleWithString:(NSString *)string {
return 0;
}

+ (UITableViewCellSeparatorStyle)tableViewCellSeparatorStyleWithString:(NSString *)string {
if ([string isEqualToString:@"none"]) return UITableViewCellSeparatorStyleNone;
if ([string isEqualToString:@"single-line"]) return UITableViewCellSeparatorStyleSingleLine;
if ([string isEqualToString:@"single-line-etched"]) return UITableViewCellSeparatorStyleSingleLineEtched;
return 0;
}

#pragma mark - Translators (Objects)

+ (NSString *)stringWithString:(NSString *)string {
Expand Down
4 changes: 4 additions & 0 deletions Pegasus/Pegasus.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#import "PGTextField.h"
#import "PGButton.h"
#import "PGScrollView.h"
#import "PGProgressView.h"
#import "PGSwitch.h"
#import "PGTableView.h"
#import "PGTableViewCell.h"

// Layouts
#import "PGLayout.h"
Expand Down
14 changes: 10 additions & 4 deletions Pegasus/Views/PGView.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ + (PGView *)viewWithData:(NSData *)data {
PGView *view = [PGView viewWithElement:rootElement];
if (view) return view;

NSLog(@"Pegasus Error: No corresponding class for root tag '%@'. Ignoring and returning nil!", rootElement.name);
NSLog(@"Pegasus Error: No corresponding class for root element '%@'. Ignoring and returning nil!", rootElement.name);
return nil;
}

Expand All @@ -55,6 +55,8 @@ + (PGView *)viewWithElement:(CXMLElement *)element {
@"PGButton",
@"PGSwitch",
@"PGProgressView",
@"PGTableView",
@"PGTableViewCell",
nil];

// Search for class matching the tag name
Expand Down Expand Up @@ -112,10 +114,9 @@ - (id)initWithElement:(CXMLElement *)element {
PGView *subview = [PGView viewWithElement:childElement];

if (subview) {
[layout addView:subview.view];
[subviews addObject:subview];
[self addSubview:subview];
} else {
NSLog(@"Pegasus Error: No corresponding class for tag '%@'. Ignoring!", childElement.name);
NSLog(@"Pegasus Error: No corresponding class for element '%@'. Ignoring!", childElement.name);
}

}
Expand Down Expand Up @@ -230,5 +231,10 @@ - (void)setValue:(NSString *)string forVirtualProperty:(NSString *)propertyName
}
}

- (void)addSubview:(PGView *)subview {
[layout addView:subview.view];
[subviews addObject:subview];
}


@end
Loading

0 comments on commit 10530b7

Please sign in to comment.