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

BIDAppDelegate.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @class BIDViewController;
  4.  
  5. @interface BIDAppDelegate : UIResponder <UIApplicationDelegate>
  6.  
  7. @property (strong, nonatomic) UIWindow *window;
  8.  
  9. @property (strong, nonatomic) BIDViewController *viewController;
  10.  
  11. @end

BIDAppDelegate.m

  1. #import "BIDAppDelegate.h"
  2.  
  3. #import "BIDViewController.h"
  4.  
  5. @implementation BIDAppDelegate
  6.  
  7. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  8. {
  9. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  10. // Override point for customization after application launch.
  11. self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
  12. self.window.rootViewController = self.viewController;
  13. [self.window makeKeyAndVisible];
  14. return YES;
  15. }
  16.  
  17. @end

BIDViewController.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface BIDViewController : UIViewController
  4. <UITableViewDataSource, UITableViewDelegate>
  5.  
  6. @property (copy, nonatomic) NSArray *dwarves;
  7.  
  8. @end

BIDViewController.m

  1. #import "BIDViewController.h"
  2.  
  3. @interface BIDViewController ()
  4.  
  5. @end
  6.  
  7. @implementation BIDViewController
  8.  
  9. - (void)viewDidLoad
  10. {
  11. [super viewDidLoad];
  12. // Do any additional setup after loading the view, typically from a nib.
  13. self.dwarves = @[@"Sleepy", @"Sneezy",
  14. @"Bashful", @"Happy", @"Doc", @"Grumpy", @"Dopey", @"Thorin",
  15. @"Dorin", @"Nori", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili",
  16. @"Oin", @"Gloin", @"Bifur", @"Bofur", @"Bombur"];
  17. }
  18.  
  19. #pragma mark -
  20. #pragma mark Table View Data Source Methods
  21. - (NSInteger)tableView:(UITableView *)tableView
  22. numberOfRowsInSection:(NSInteger)section
  23. {
  24. return [self.dwarves count];
  25. }
  26.  
  27. - (UITableViewCell *)tableView:(UITableView *)tableView
  28. cellForRowAtIndexPath:(NSIndexPath *)indexPath // 创建默认的UITableViewCell
  29. {
  30. static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
  31.  
  32. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
  33. SimpleTableIdentifier];
  34. if (cell == nil) {
  35. cell = [[UITableViewCell alloc]
  36. initWithStyle:UITableViewCellStyleDefault
  37. reuseIdentifier:SimpleTableIdentifier];
  38. }
  39.  
  40. UIImage *image = [UIImage imageNamed:@"star.png"];
  41. cell.imageView.image = image;
  42.  
  43. cell.textLabel.text = self.dwarves[indexPath.row];
  44. cell.textLabel.font = [UIFont boldSystemFontOfSize:];
  45.  
  46. if (indexPath.row < ) {
  47. cell.detailTextLabel.text = @"Mr. Disney";
  48. } else {
  49. cell.detailTextLabel.text = @"Mr. Tolkien";
  50. }
  51. return cell;
  52. }
  53.  
  54. #pragma mark -
  55. #pragma mark Table Delegate Methods
  56.  
  57. - (NSInteger)tableView:(UITableView *)tableView
  58. indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath // 类似expandListview
  59. {
  60. return indexPath.row;
  61. }
  62.  
  63. - (NSIndexPath *)tableView:(UITableView *)tableView
  64. willSelectRowAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. if (indexPath.row <= ) { // 前4行点击没有反应
  67. return nil;
  68. } else {
  69. return indexPath;
  70. }
  71. }
  72.  
  73. - (void)tableView:(UITableView *)tableView
  74. didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  75. {
  76. NSString *rowValue = self.dwarves[indexPath.row];
  77. NSString *message = [[NSString alloc] initWithFormat:
  78. @"You selected %@", rowValue];
  79. UIAlertView *alert = [[UIAlertView alloc]
  80. initWithTitle:@"Row Selected!"
  81. message:message
  82. delegate:nil
  83. cancelButtonTitle:@"Yes I Did"
  84. otherButtonTitles:nil];
  85. [alert show];
  86.  
  87. [tableView deselectRowAtIndexPath:indexPath animated:YES]; // 取消选择
  88. }
  89.  
  90. - (CGFloat)tableView:(UITableView *)tableView
  91. heightForRowAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. return ;
  94. }
  95.  
  96. @end

有section的tableview

  1. #import "BIDViewController.h"
  2.  
  3. static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
  4.  
  5. @implementation BIDViewController {
  6. NSMutableArray *filteredNames;
  7. UISearchDisplayController *searchController;
  8. }
  9.  
  10. - (void)viewDidLoad
  11. {
  12. [super viewDidLoad];
  13.  
  14. UITableView *tableView = (id)[self.view viewWithTag:];
  15. [tableView registerClass:[UITableViewCell class]
  16. forCellReuseIdentifier:SectionsTableIdentifier]; // 生成或创建tableviewcell
  17.  
  18. filteredNames = [NSMutableArray array];
  19. UISearchBar *searchBar = [[UISearchBar alloc]
  20. initWithFrame:CGRectMake(, , , )];
  21. tableView.tableHeaderView = searchBar; // 向tableview的header添加view
  22. searchController = [[UISearchDisplayController alloc]
  23. initWithSearchBar:searchBar
  24. contentsController:self]; // serachbar需要controller作控制
  25. searchController.delegate = self; // 需要实现UISearchDisplayDelegate
  26. searchController.searchResultsDataSource = self; // 搜索数据源
  27.  
  28. NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"
  29. ofType:@"plist"];
  30. self.names = [NSDictionary dictionaryWithContentsOfFile:path];
  31.  
  32. self.keys = [[self.names allKeys] sortedArrayUsingSelector:
  33. @selector(compare:)];
  34. }
  35.  
  36. #pragma mark -
  37. #pragma mark Table View Data Source Methods
  38. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  39. {
  40. if (tableView.tag == ) {
  41. return [self.keys count];
  42. } else {
  43. return ;
  44. }
  45. }
  46.  
  47. - (NSInteger)tableView:(UITableView *)tableView
  48. numberOfRowsInSection:(NSInteger)section
  49. {
  50. if (tableView.tag == ) {
  51. NSString *key = self.keys[section];
  52. NSArray *nameSection = self.names[key];
  53. return [nameSection count];
  54. } else {
  55. return [filteredNames count];
  56. }
  57. }
  58.  
  59. - (NSString *)tableView:(UITableView *)tableView
  60. titleForHeaderInSection:(NSInteger)section
  61. {
  62. if (tableView.tag == ) {
  63. return self.keys[section];
  64. } else {
  65. return nil;
  66. }
  67. }
  68.  
  69. - (UITableViewCell *)tableView:(UITableView *)tableView
  70. cellForRowAtIndexPath:(NSIndexPath *)indexPath
  71. {
  72.  
  73. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
  74. SectionsTableIdentifier];
  75. if (tableView.tag == ) {
  76. NSString *key = self.keys[indexPath.section]; // 返回行数对应的section
  77. NSArray *nameSection = self.names[key];
  78.  
  79. cell.textLabel.text = nameSection[indexPath.row];
  80. } else {
  81. cell.textLabel.text = filteredNames[indexPath.row];
  82. }
  83. return cell;
  84. }
  85.  
  86. // 指定tableview section的标题
  87. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
  88. {
  89. if (tableView.tag == ) {
  90. return self.keys;
  91. } else {
  92. return nil;
  93. }
  94. }
  95.  
  96. #pragma mark -
  97. #pragma mark Search Display Delegate Methods
  98. // 告诉UISearchDisplayDelegate,控制器已经加载tableview
  99. - (void)searchDisplayController:(UISearchDisplayController *)controller
  100. didLoadSearchResultsTableView:(UITableView *)tableView
  101. {
  102. [tableView registerClass:[UITableViewCell class]
  103. forCellReuseIdentifier:SectionsTableIdentifier];
  104. }
  105.  
  106. // 请求UISearchDisplayDelegate是否应该根据搜索字重载
  107. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller
  108. shouldReloadTableForSearchString:(NSString *)searchString
  109. {
  110. [filteredNames removeAllObjects];
  111. if (searchString.length > ) {
  112. // NSPredicate 指定数据被获取或者过滤的方式
  113. NSPredicate *predicate =
  114. [NSPredicate
  115. predicateWithBlock:^BOOL(NSString *name, NSDictionary *b) {
  116. NSRange range = [name rangeOfString:searchString
  117. options:NSCaseInsensitiveSearch];
  118. return range.location != NSNotFound;
  119. }];
  120. for (NSString *key in self.keys) {
  121. NSArray *matches = [self.names[key]
  122. filteredArrayUsingPredicate: predicate];// 过滤
  123. [filteredNames addObjectsFromArray:matches];
  124. }
  125. }
  126. return YES;
  127. }
  128.  
  129. @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. 【OOB】MSHTML!CPaste­Command::Convert­Bitmapto­Png heap-based buffer overflow学习

    IE 11 MSHTML!CPaste­Command::Convert­Bitmapto­Png heap-based buffer overflow学习 MS14-056, CVE-2014-41 ...

  2. C/C++有效对齐值的确定

    先来看看什么是对齐.现代计算机中内存空间都是按照字节(byte)划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型 ...

  3. php实现https(tls/ssl)双向认证

    php实现https(tls/ssl)双向认证 通常情况下,在部署https的时候,是基于ssl单向认证的,也就是说只要客户端认证服务器,而服务器不需要认证客户端. 但在一些安全性较高的场景,如银行, ...

  4. 【Java】 大话数据结构(10) 查找算法(1)(顺序、二分、插值、斐波那契查找)

    本文根据<大话数据结构>一书,实现了Java版的顺序查找.折半查找.插值查找.斐波那契查找. 注:为与书一致,记录均从下标为1开始. 顺序表查找 顺序查找  顺序查找(Sequential ...

  5. php返回上一页

    echo "<script>alert('没有获取到订单信息');history.go(-1);</script>";

  6. webpack1.x环境配置与打包基础【附带各种 "坑" 与解决方案!持续更新中...】

    首先介绍传统模块化开发的主流方案: 1.基与CMD的sea.js,玉伯提出的解决方案,据说原来京东团队在使用.用时才定义,就近加载. 2.基于AMD的require.js,之前在用.提前声明与定义.国 ...

  7. php版本CKFinder3.4.4自定义上传文件位置

    1.修改文件上传路径: 编辑ckfinder目录下config.php,70行设置为:    'baseUrl'      => '/uploads/'.date('Ymd').'/'; 这样上 ...

  8. BZOJ.2246.[SDOI2011]迷宫探险(DP 记忆化搜索 概率)

    题目链接 求最大的存活概率,DP+记忆化. 用f[s][x][y][hp]表示在s状态,(x,y)点,血量为hp时的存活概率. s是个三进制数,记录每个陷阱无害/有害/未知. 转移时比较容易,主要是在 ...

  9. BZOJ2612 : [Poi2003]Sums

    设d[i]表示能拼出的x中满足x%a[0]=i的最小的x,其中d[0]=0. 若d[x%a[0]]<=x,则一定可以拼出x,否则一定不可以. 建出带权有向图,点的标号从0到a[0]-1,i号点向 ...

  10. CentOS 7开机不执行/etc/rc.local的解决方法

    该死的CentOS 7居然开机不执行/etc/rc.local!!!!! 解决: chmod +x /etc/rc.d/rc.local 问题分析: 其实在/etc/rc.lacal文件上已经说明了, ...