这几天都有一些任务要跟, 把ios的学习拉后, 看看要抓紧咯, 看看轮到的学习的是UITableView。

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:]; if (indexPath.row < ) {
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 <= ) { // 前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 ;
} @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:];
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:SectionsTableIdentifier]; // 生成或创建tableviewcell filteredNames = [NSMutableArray array];
UISearchBar *searchBar = [[UISearchBar alloc]
initWithFrame:CGRectMake(, , , )];
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 == ) {
return [self.keys count];
} else {
return ;
}
} - (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == ) {
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 == ) {
return self.keys[section];
} else {
return nil;
}
} - (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier];
if (tableView.tag == ) {
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 == ) {
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 > ) {
// 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

ios成长之每日一遍(day 8)的更多相关文章

  1. ios成长之每日一遍(day 5)

    iOS 屏幕方向那点事儿http://zhenby.com/blog/2013/08/20/talk-ios-orientation/ 针对当前的屏幕方向进行对应的代码布局 BIDViewContro ...

  2. ios成长之每日一遍(day 3)

    今天要介绍一下更多的控键使用, 当然也会对上一篇说的控件做一个巩固, 所以这一篇涉及到的控键会包括 UIImage.UITextField.UIButton.UILabel.UISwitch.以及 U ...

  3. ios成长之每日一遍(day 1)

    Hello world开始. 这里不讨论如何创建项目导入项目.由于趁上班时间打酱油所以也不谈细节, 只谈具体项目的实现与关键流程的解析, 只供本人实际程况使用.不喜请移驾. 首先来谈谈 AppDele ...

  4. ios成长之每日一遍(day 7)

    今天到UITabBarController 结合 UIPickView, 这里一共有5个实现, 由浅到易. 其实在IB上面使用UITabBarController很简单, 就像平常拖控件一样拖到界面上 ...

  5. ios成长之每日一遍(day 6)

    toolBar上的View Switcher BIDAppDelegate.h #import <UIKit/UIKit.h> @class BIDSwitchViewController ...

  6. ios成长之每日一遍(day 4)

    今天, 主要讲四种常见的问题, 废话不多说了, 直接开始. 自动布局:这个我发现有一篇文章写得非常好, 直接表明出地http://www.cocoachina.com/applenews/devnew ...

  7. ios成长之每日一遍(day 2)

    接着下来简单说说Label(相当于android的textview)和button的使用, 由于都是与上篇的AppDelegate一致, 所以这一篇就说说ViewController与xib的使用呗. ...

  8. iOS:从头捋一遍VC的生命周期

    一.介绍 UIViewController是iOS开发中的核心控件,没有它那基本上任何功能都无法实现,虽然系统已经做了所有控件的生命维护,但是,了解它的生命周期是如何管理还是非常有必要的.网上有很多教 ...

  9. IOS成长之路-用NSXMLParser实现XML解析

    再次对xml进行解析,又有了些理解,如果有不对的地方,请给小弟指出,谢谢! <?xml version="1.0" encoding="UTF-8"?&g ...

随机推荐

  1. python 包详解

    包 包是一种管理 Python 模块命名空间的形式,采用"点模块名称". 比如一个模块的名称是 A.B, 那么他表示一个包 A中的子模块 B . 就好像使用模块的时候,你不用担心不 ...

  2. JSR教程2——Spring MVC数据校验与国际化

    SpringMVC数据校验采用JSR-303校验. • Spring4.0拥有自己独立的数据校验框架,同时支持JSR303标准的校验框架. • Spring在进行数据绑定时,可同时调用校验框架完成数据 ...

  3. 洛谷P4107 [HEOI2015]兔子与樱花 [贪心,DFS]

    题目传送门 兔子与樱花 题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1 ...

  4. priority_queue<int>q;

    priority_queue<int>q;//默认不递增q.size();//q中有几个元素q.pop();//删除队首q.top();//返回队首元素q.push();//在队列中插入一 ...

  5. 001.WordPress建站部署

    一 WordPress简介 WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站.也可以把 WordPress当作一个内容管理系统( ...

  6. C#简单的通用分页

    通用分页技术分析 需要返回不同的类型的数据--采用泛型实现该操作 需要提供不同的方法 上一页 上一页 第一页 最后一页 跳转到指定页 Demo 代码 using System; using Syste ...

  7. 【WIN10】Segoe MDL2 Assets

    APP下載地址:https://www.microsoft.com/store/apps/9nblggh5k2hf 最近使用文本圖標Segoe MDL2 Assets時,使用字符映射表看,那個圖標真的 ...

  8. HDU.5819.Knights(概率DP)

    题目链接 参考一下这的. \(Description\) 数轴上有n个骑士,分别位于1,2,3,...,n,它们的移动速度相同,初始移动方向已知.当两个骑士相遇时,各有50%的概率获胜,失败的骑士就死 ...

  9. BZOJ4255 : Keep Fit!

    首先旋转坐标系,把每个点可以接收的范围转化成一个正方形. 然后建立k-d tree,并记录下每个点在k-d tree上的位置. 对询问使用莫队算法,修改$O(\log n)$,查询期望$O(\log ...

  10. HNOI 越狱

    题目描述 监狱有连续编号为 1…N的 N 个房间,每个房间关押一个犯人,有 M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. 输入输出格式 ...