ios成長の毎日1回(day 8)
22615 ワード
この数日すべていくつかの任务がついて、iosの学习を引き延ばした后に、见て急いで、番になった学习を见てUITableViewです.
BIDAppDelegate.h
BIDAppDelegate.m
BIDViewController.h
BIDViewController.m
sectionのあるtableview
BIDAppDelegate.h
#import <UIKit/UIKit.h>
@class BIDViewController;
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BIDViewController *viewController;
@end
BIDAppDelegate.m
#import "BIDAppDelegate.h"
#import "BIDViewController.h"
@implementation BIDAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
BIDViewController.h
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
@property (copy, nonatomic) NSArray *dwarves;
@end
BIDViewController.m
#import "BIDViewController.h"
@interface BIDViewController ()
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dwarves = @[@"Sleepy", @"Sneezy",
@"Bashful", @"Happy", @"Doc", @"Grumpy", @"Dopey", @"Thorin",
@"Dorin", @"Nori", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili",
@"Oin", @"Gloin", @"Bifur", @"Bofur", @"Bombur"];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.dwarves count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath // UITableViewCell
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
UIImage *image = [UIImage imageNamed:@"star.png"];
cell.imageView.image = image;
cell.textLabel.text = self.dwarves[indexPath.row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
if (indexPath.row < 7) {
cell.detailTextLabel.text = @"Mr. Disney";
} else {
cell.detailTextLabel.text = @"Mr. Tolkien";
}
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath // expandListview
{
return indexPath.row;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row <= 3) { // 4
return nil;
} else {
return indexPath;
}
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *rowValue = self.dwarves[indexPath.row];
NSString *message = [[NSString alloc] initWithFormat:
@"You selected %@", rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:@"Yes I Did"
otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
@end
sectionのあるtableview
#import "BIDViewController.h"
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
@implementation BIDViewController {
NSMutableArray *filteredNames;
UISearchDisplayController *searchController;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:SectionsTableIdentifier]; // tableviewcell
filteredNames = [NSMutableArray array];
UISearchBar *searchBar = [[UISearchBar alloc]
initWithFrame:CGRectMake(0, 0, 320, 44)];
tableView.tableHeaderView = searchBar; // tableview header view
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:self]; // serachbar controller
searchController.delegate = self; // UISearchDisplayDelegate
searchController.searchResultsDataSource = self; //
NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"
ofType:@"plist"];
self.names = [NSDictionary dictionaryWithContentsOfFile:path];
self.keys = [[self.names allKeys] sortedArrayUsingSelector:
@selector(compare:)];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == 1) {
return [self.keys count];
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == 1) {
NSString *key = self.keys[section];
NSArray *nameSection = self.names[key];
return [nameSection count];
} else {
return [filteredNames count];
}
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
if (tableView.tag == 1) {
return self.keys[section];
} else {
return nil;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier];
if (tableView.tag == 1) {
NSString *key = self.keys[indexPath.section]; // section
NSArray *nameSection = self.names[key];
cell.textLabel.text = nameSection[indexPath.row];
} else {
cell.textLabel.text = filteredNames[indexPath.row];
}
return cell;
}
// tableview section
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (tableView.tag == 1) {
return self.keys;
} else {
return nil;
}
}
#pragma mark -
#pragma mark Search Display Delegate Methods
// UISearchDisplayDelegate, tableview
- (void)searchDisplayController:(UISearchDisplayController *)controller
didLoadSearchResultsTableView:(UITableView *)tableView
{
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:SectionsTableIdentifier];
}
// UISearchDisplayDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[filteredNames removeAllObjects];
if (searchString.length > 0) {
// NSPredicate
NSPredicate *predicate =
[NSPredicate
predicateWithBlock:^BOOL(NSString *name, NSDictionary *b) {
NSRange range = [name rangeOfString:searchString
options:NSCaseInsensitiveSearch];
return range.location != NSNotFound;
}];
for (NSString *key in self.keys) {
NSArray *matches = [self.names[key]
filteredArrayUsingPredicate: predicate];//
[filteredNames addObjectsFromArray:matches];
}
}
return YES;
}
@end