您的位置:首页 > 其它

简单的TableView

2014-07-24 18:29 316 查看

背景知识

每个表都是UITableView的实例,表中的每一行都是UITableViewCell的实例。

TableView的种类

Grouped table

Plain table without index

Plain table with index

//
//  ViewController.m
//  Contact
//
//  Created by Norcy on 15/4/16.
//  Copyright (c) 2015年 Norcy. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
}
@property (strong, nonatomic) NSDictionary* names;
@property (strong, nonatomic) NSArray* keys;
@end

NSMutableArray* filteredNames;
UISearchDisplayController* searchController;
static NSString* CELL_ID = @"MyCell";

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

//Screen
int screenWidth = [[UIScreen mainScreen] bounds].size.width;
int screenHeight = [[UIScreen mainScreen] bounds].size.height;

//Filter
filteredNames = [[NSMutableArray alloc] init];

//TableView
UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_ID];

tableView.delegate = self;

tableView.dataSource = self;

tableView.tag = 1;

UIEdgeInsets insets = tableView.contentInset;    //设置与屏幕顶部的距离,防止被状态栏挡住

insets.top = 20;

[tableView setContentInset:insets];

//Search Bar
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight / 12)];

tableView.tableHeaderView = searchBar;

searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

searchController.delegate = self;

searchController.searchResultsDataSource = self;

//Names and Keys
NSString* path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];

self.names = [NSDictionary dictionaryWithContentsOfFile:path];

self.keys = [[self.names allKeys] sortedArrayUsingSelector:@selector(compare:)];

[self.view addSubview:tableView];
}

#pragma mark -
#pragma mark 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* curSection = self.names[key];
return [curSection 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:CELL_ID forIndexPath:indexPath];

//    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID];

//    if (cell == nil)
//    {
//        NSLog(@"asd");
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID];
//    }

if (tableView.tag == 1)
{
NSString* key = self.keys[indexPath.section];

NSArray* curSection = self.names[key];

cell.textLabel.text = curSection[indexPath.row];
}
else
cell.textLabel.text = filteredNames[indexPath.row];

return cell;
}

#pragma mark Delegate Methods
//显示索引
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
if (tableView.tag == 1)
return self.keys;
else
return nil;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark Search Delegate Methods
- (void)searchDisplayController:(UISearchDisplayController*)controller didLoadSearchResultsTableView:(UITableView*)tableView
{
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_ID];
}

- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
[filteredNames removeAllObjects];

if (searchString.length > 0)
{
NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary* bindings) {
NSRange range = [evaluatedObject 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


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: