• ios成长之每日一遍(day 8)


    这几天都有一些任务要跟, 把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: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
  • 相关阅读:
    还在纠结注册.com域名还是.cn域名?
    网站域名被墙的检测查询和解决办法
    阿里云服务器无法远程其他的mysql服务器
    如何维护一个产品
    使用bootstrap+asp.net mvc4+IBatis.Net实现的小程序
    bootstrap IE兼容
    mongodb命令使用
    聊天工具实现winform端实现
    二级域名设置
    org
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3761337.html
Copyright © 2020-2023  润新知