Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improvements to party example

- parties are sorted by date
- event times are shown in main list
  • Loading branch information...
commit 56afdaf6a803d84db45ae4ede36127d2d927f479 1 parent 1ca53ed
Tim Burks timburks authored
60 samples/parties/parties/PartiesViewController.m
View
@@ -14,6 +14,7 @@
@interface PartiesViewController ()
@property (nonatomic, strong) NSDictionary *content;
+@property (nonatomic, strong) NSArray *partiesByStartDate;
@property (nonatomic, strong) NSMutableDictionary *images;
@end
@@ -28,6 +29,35 @@ - (id)initWithStyle:(UITableViewStyle)style
return self;
}
+- (void) setContent:(NSDictionary *)content
+{
+ _content = content;
+
+ NSMutableDictionary *partiesByStartDateDictionary = [NSMutableDictionary dictionary];
+ for (NSDictionary *party in self.content[@"entities"]) {
+ NSString *startDate = [party objectForKey:@"startDate"];
+ NSArray *parts = [startDate componentsSeparatedByString:@" "];
+ NSString *datePart = [parts objectAtIndex:0];
+ NSMutableArray *arrayOfParties = [partiesByStartDateDictionary objectForKey:datePart];
+ if (!arrayOfParties) {
+ arrayOfParties = [NSMutableArray array];
+ [partiesByStartDateDictionary setObject:arrayOfParties forKey:datePart];
+ }
+ [arrayOfParties addObject:party];
+ }
+ NSArray *keys = [[partiesByStartDateDictionary allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
+ return [obj1 compare:obj2];
+ }];
+ NSMutableArray *partiesByStartDate = [NSMutableArray array];
+ for (NSString *key in keys) {
+ [partiesByStartDate addObject:
+ @{@"date":key,
+ @"parties":[partiesByStartDateDictionary objectForKey:key]}];
+ }
+ self.partiesByStartDate = partiesByStartDate;
+ // NSLog(@"%@", self.partiesByStartDate);
+}
+
- (void) fetchAssetForParty:(NSString *) partyName atIndexPath:(NSIndexPath *) indexPath
{
UGConnection *connection = [UGConnection sharedConnection];
@@ -127,12 +157,18 @@ - (void)loadView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
- return 1;
+ return [self.partiesByStartDate count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
- return self.content ? [self.content[@"entities"] count] : 0;
+ if (self.content) {
+ NSDictionary *sectionInfo = [self.partiesByStartDate objectAtIndex:section];
+ NSArray *parties = [sectionInfo objectForKey:@"parties"];
+ return [parties count];
+ } else {
+ return 0;
+ }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
@@ -145,9 +181,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.content) {
- id entity = self.content[@"entities"][[indexPath row]];
+
+ NSDictionary *sectionInfo = [self.partiesByStartDate objectAtIndex:[indexPath section]];
+ NSArray *parties = [sectionInfo objectForKey:@"parties"];
+
+ id entity = parties[[indexPath row]];
cell.textLabel.text = entity[@"title"];
- cell.detailTextLabel.text = entity[@"venue"];
+ cell.textLabel.numberOfLines = 0;
+ cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", entity[@"venue"], entity[@"partytime"]];
+ cell.detailTextLabel.numberOfLines = 0;
NSString *partyName = [entity objectForKey:@"name"];
UIImage *image = [self.images objectForKey:partyName];
@@ -164,11 +206,19 @@ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)ce
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.content) {
- id entity = self.content[@"entities"][[indexPath row]];
+ NSDictionary *sectionInfo = [self.partiesByStartDate objectAtIndex:[indexPath section]];
+ NSArray *parties = [sectionInfo objectForKey:@"parties"];
+ id entity = [parties objectAtIndex:[indexPath row]];
PartyViewController *partyViewController = [[PartyViewController alloc] init];
partyViewController.content = entity;
[self.navigationController pushViewController:partyViewController animated:YES];
}
}
+- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
+{
+ NSDictionary *sectionInfo = [self.partiesByStartDate objectAtIndex:section];
+ return [sectionInfo objectForKey:@"date"];
+}
+
@end
111 samples/parties/parties/PartyViewController.m
View
@@ -41,12 +41,16 @@ - (void) viewWillAppear:(BOOL)animated
- (NSMutableAttributedString *) attributedStringForIndexPath:(NSIndexPath *) indexPath
{
- if ([indexPath row] == 1) {
+ if (([indexPath section] == 0) && ([indexPath row] == 1)) {
NSMutableAttributedString *attributedString = attributed_string([self.content objectForKey:@"title"], 30);
[attributedString appendAttributedString:carriage_return(18)];
[attributedString appendAttributedString:attributed_string([self.content objectForKey:@"venue"], 24)];
[attributedString appendAttributedString:carriage_return(24)];
+ [attributedString appendAttributedString:attributed_string([self.content objectForKey:@"startDate"], 18)];
+ [attributedString appendAttributedString:carriage_return(18)];
+ [attributedString appendAttributedString:carriage_return(18)];
+
id address = [self.content objectForKey:@"address"];
[attributedString appendAttributedString:attributed_string([address objectForKey:@"street"], 18)];
[attributedString appendAttributedString:carriage_return(18)];
@@ -60,14 +64,8 @@ - (NSMutableAttributedString *) attributedStringForIndexPath:(NSIndexPath *) ind
[attributedString appendAttributedString:carriage_return(18)];
[attributedString appendAttributedString:carriage_return(18)];
- [attributedString appendAttributedString:attributed_string([self.content objectForKey:@"partytime"], 18)];
- [attributedString appendAttributedString:carriage_return(18)];
- [attributedString appendAttributedString:attributed_string([self.content objectForKey:@"startDate"], 18)];
- [attributedString appendAttributedString:carriage_return(18)];
- [attributedString appendAttributedString:attributed_string([self.content objectForKey:@"endDate"], 18)];
- [attributedString appendAttributedString:carriage_return(18)];
return attributedString;
- } else if ([indexPath row] == 2) {
+ } else if (([indexPath section] == 1) && ([indexPath row] == 0)) {
NSMutableAttributedString *attributedString = attributed_string([self.content description], 12);
[attributedString appendAttributedString:carriage_return(12)];
return attributedString;
@@ -80,67 +78,72 @@ - (NSMutableAttributedString *) attributedStringForIndexPath:(NSIndexPath *) ind
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
- return 1;
+ return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
- return 3;
+ return (section == 0) ? 2 : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
- switch ([indexPath row]) {
- case 0: {
- if (!self.mapCell) {
- self.mapCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Map"];
- MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.mapCell.bounds];
- mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth + UIViewAutoresizingFlexibleHeight;
- [self.mapCell addSubview:mapView];
- id location = [self.content objectForKey:@"location"];
- double lat = [[location objectForKey:@"latitude"] floatValue];
- double lng = [[location objectForKey:@"longitude"] floatValue];
- [mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(lat,lng),
- MKCoordinateSpanMake(0.002,0.002))];
- CLLocationCoordinate2D annotationCoord;
- annotationCoord.latitude = lat;
- annotationCoord.longitude = lng;
- MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
- annotationPoint.coordinate = annotationCoord;
- annotationPoint.title = [self.content objectForKey:@"title"];
- annotationPoint.subtitle = [self.content objectForKey:@"venue"];
- [mapView addAnnotation:annotationPoint];
- mapView.scrollEnabled = NO;
- mapView.zoomEnabled = NO;
- }
- return self.mapCell;
- }
- default: {
- UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier:@"Cell"];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.textLabel.attributedText = [self attributedStringForIndexPath:indexPath];
- cell.textLabel.numberOfLines = 0;
- return cell;
+ if (([indexPath section] == 0) && ([indexPath row] == 0)) {
+ if (!self.mapCell) {
+ self.mapCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Map"];
+ MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.mapCell.bounds];
+ mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth + UIViewAutoresizingFlexibleHeight;
+ [self.mapCell addSubview:mapView];
+ id location = [self.content objectForKey:@"location"];
+ double lat = [[location objectForKey:@"latitude"] floatValue];
+ double lng = [[location objectForKey:@"longitude"] floatValue];
+ [mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(lat,lng),
+ MKCoordinateSpanMake(0.002,0.002))];
+ CLLocationCoordinate2D annotationCoord;
+ annotationCoord.latitude = lat;
+ annotationCoord.longitude = lng;
+ MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
+ annotationPoint.coordinate = annotationCoord;
+ annotationPoint.title = [self.content objectForKey:@"title"];
+ annotationPoint.subtitle = [self.content objectForKey:@"venue"];
+ [mapView addAnnotation:annotationPoint];
+ mapView.scrollEnabled = NO;
+ mapView.zoomEnabled = NO;
}
+ return self.mapCell;
+ } else {
+ UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
+ reuseIdentifier:@"Cell"];
+ cell.selectionStyle = UITableViewCellSelectionStyleNone;
+ cell.textLabel.attributedText = [self attributedStringForIndexPath:indexPath];
+ cell.textLabel.numberOfLines = 0;
+ return cell;
}
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
- switch ([indexPath row]) {
- case 0:
- return 200;
- default: {
- CGFloat w = tableView.bounds.size.width;
- CGFloat h = 0;
- NSMutableAttributedString *attributedString = [self attributedStringForIndexPath:indexPath];
- CGRect bounds = [attributedString boundingRectWithSize:CGSizeMake(w,h)
- options:NSStringDrawingUsesFontLeading+NSStringDrawingUsesLineFragmentOrigin
- context:nil];
- return bounds.size.height;
- }
+ if (([indexPath section] == 0) && ([indexPath row] == 0)) {
+ return 200;
+ } else {
+ CGFloat w = tableView.bounds.size.width - 20; // use approximate margins
+ CGFloat h = 0;
+ NSMutableAttributedString *attributedString = [self attributedStringForIndexPath:indexPath];
+ CGRect bounds = [attributedString boundingRectWithSize:CGSizeMake(w,h)
+ options:NSStringDrawingUsesFontLeading+NSStringDrawingUsesLineFragmentOrigin
+ context:nil];
+ return bounds.size.height;
+ }
+}
+
+- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
+{
+ if (section == 0) {
+ return nil;
+ } else if (section == 1) {
+ return @"Party Entity Details";
}
+ return nil;
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.