博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios成长之每日一遍(day 8)
阅读量:6153 次
发布时间:2019-06-21

本文共 7702 字,大约阅读时间需要 25 分钟。

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

BIDAppDelegate.h

#import 
@class BIDViewController;@interface BIDAppDelegate : UIResponder
@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 
@interface BIDViewController : UIViewController
@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 *)tableViewindentationLevelForRowAtIndexPath:(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 *)tableViewdidSelectRowAtIndexPath:(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 *)tableViewheightForRowAtIndexPath:(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 *)tableViewtitleForHeaderInSection:(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 *)controllershouldReloadTableForSearchString:(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

 

转载地址:http://evffa.baihongyu.com/

你可能感兴趣的文章
STM32启动过程--启动文件--分析
查看>>
垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想
查看>>
淘宝的几个架构图
查看>>
linux后台运行程序
查看>>
Python异步IO --- 轻松管理10k+并发连接
查看>>
Oracle中drop user和drop user cascade的区别
查看>>
登记申请汇总
查看>>
Android Jni调用浅述
查看>>
CodeCombat森林关卡Python代码
查看>>
第一个应用程序HelloWorld
查看>>
(二)Spring Boot 起步入门(翻译自Spring Boot官方教程文档)1.5.9.RELEASE
查看>>
Java并发编程73道面试题及答案
查看>>
企业级负载平衡简介(转)
查看>>
Shell基础之-正则表达式
查看>>
JavaScript异步之Generator、async、await
查看>>
讲讲吸顶效果与react-sticky
查看>>
c++面向对象的一些问题1 0
查看>>
售前工程师的成长---一个老员工的经验之谈
查看>>
Get到的优秀博客网址
查看>>
老男孩教育每日一题-第107天-简述你对***的理解,常见的有哪几种?
查看>>