-
Notifications
You must be signed in to change notification settings - Fork 60
/
SortTableViewController.m
69 lines (61 loc) · 2.45 KB
/
SortTableViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// SortTableViewController.m
// Sort
//
// Created by Tim Moose on 5/25/13.
// Copyright (c) 2013 Tractable Labs. All rights reserved.
//
#import "SortTableViewController.h"
@implementation SortTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//initialize the controller with a list of strings to display in the table
self.indexPathController.items = @[
@"Fredricksburg",
@"Jelly Bean",
@"George Washington",
@"Grand Canyon",
@"Bibliography",
@"Keyboard Shortcut",
@"Metadata",
@"Fundamental",
@"Cellar Door"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
//retrieve the string for the given index path from the controller
//and set the cell's text label.
NSString *item = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
cell.textLabel.text = item;
return cell;
}
- (void)sortAlphabetically
{
NSArray *items = self.indexPathController.items;
NSArray *sortedItems = [items sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
self.indexPathController.items = sortedItems;
}
- (void)sortByLength
{
NSArray *items = self.indexPathController.dataModel.items;
NSArray *sortedItems = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSNumber *number1 = [self lengthOfCellLabelWithText:obj1];
NSNumber *number2 = [self lengthOfCellLabelWithText:obj2];
return [number1 compare:number2];
}];
self.indexPathController.items = sortedItems;
}
- (NSNumber *)lengthOfCellLabelWithText:(NSString *)text
{
//configure the prototype cell from the storyboard with the given
//text and return the label's width
UITableViewCell *cell = [self tableView:self.tableView prototypeForCellIdentifier:@"Cell"];
cell.textLabel.text = text;
//size the label to fit. Note that the font metrics are defined in the storyboard
//so we don't need to hard code any here.
[cell.textLabel sizeToFit];
return @(cell.textLabel.bounds.size.width);
}
@end