Xcode_7 iOS_9 表视图 Objective-C (10)

来源:互联网 发布:地方性银行排名知乎 编辑:程序博客网 时间:2024/05/19 03:19

1、新建SingleViewApplication项目,在storyboard中把原来的ViewController删掉,添加TableViewController,并且把类修改成原来的ViewController,首先介绍纯代码形式创建cell单元格,刚新建完之后storyboard是这样的(随便设置一个Identifier):




现在我们把原有的cell控件删掉:




2、以上一篇文章为例,导入图片资源和数据列表plist:点击打开链接,其中数据列表如下,大家可以自己修改内容:




3、ViewController.h,这里修改了继承的类,以及添加了继承的协议:

////  ViewController.h//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UITableViewController <UITableViewDataSource>@property (nonatomic, retain) NSArray *listTeam;@end


4、ViewController.m:

////  ViewController.m//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];    self.listTeam = [[NSArray alloc] initWithContentsOfFile:plistPath]; //获取全部数据}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.listTeam count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"CellIdentifier";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];    }    NSUInteger row = [indexPath row];    NSDictionary *rowDict = [self.listTeam objectAtIndex:row];    cell.textLabel.text = [rowDict objectForKey:@"name"];        NSString *imagePath = [rowDict objectForKey:@"image"];    imagePath = [imagePath stringByAppendingString:@".png"];    cell.imageView.image = [UIImage imageNamed:imagePath];    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    return cell;}@end



5、如果不去掉Cell控件代码就少几行:





ViewController.m:

////  ViewController.m//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];    self.listTeam = [[NSArray alloc] initWithContentsOfFile:plistPath]; //获取全部数据}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.listTeam count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"CellIdentifier";//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];//    if (cell == nil) {//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];//    }    NSUInteger row = [indexPath row];    NSDictionary *rowDict = [self.listTeam objectAtIndex:row];    cell.textLabel.text = [rowDict objectForKey:@"name"];        NSString *imagePath = [rowDict objectForKey:@"image"];    imagePath = [imagePath stringByAppendingString:@".png"];    cell.imageView.image = [UIImage imageNamed:imagePath];    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    return cell;}@end



6、自定义单元格,首先在上面的方式中,拖动ImageView和label到之前的Cell控件上,然后新建一个Cocoa Touch Class类,继承于UITableViewCell,这里命名为CustomCell,这时候就会多两个文件,然后把之前的这个Cell控件的类设置为CustomCell,再把imageView和label控件做两个输出口到CustomCell.h上,然后稍微修改一下ViewController.m的代码:

////  ViewController.m//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "ViewController.h"#import "CustomCell.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];    self.listTeam = [[NSArray alloc] initWithContentsOfFile:plistPath]; //获取全部数据}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.listTeam count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"CellIdentifier";    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    NSUInteger row = [indexPath row];    NSDictionary *rowDict = [self.listTeam objectAtIndex:row];    cell.myLabel.text = [rowDict objectForKey:@"name"];        NSString *imagePath = [rowDict objectForKey:@"image"];    imagePath = [imagePath stringByAppendingString:@".png"];    cell.myImageView.image = [UIImage imageNamed:imagePath];    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    return cell;}@end




7、加入搜索条,search Bar控件,在ViewController.h中声明searcher的输出口,并且继承searchBar的协议:

////  ViewController.h//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UITableViewController <UITableViewDataSource, UISearchBarDelegate>@property (nonatomic, strong) NSArray *listTeam;@property (nonatomic, strong) NSMutableArray *listFilterTeam;@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;- (void)filterContentForSearchText:(NSString *)searchText scope:(NSUInteger)scope;@end



8、配置searchBar,并且修改一下team.plist列表:




9、ViewController.m代码 做出相应修改:

////  ViewController.m//  TestProject////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "ViewController.h"#import "CustomCell.h"@interface ViewController () <UISearchBarDelegate>@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.searchBar.delegate = self;    self.searchBar.showsScopeBar = NO;    [self.searchBar sizeToFit];        NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];    self.listTeam = [[NSArray alloc] initWithContentsOfFile:plistPath]; //获取全部数据        [self filterContentForSearchText:@"" scope:-1];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.listFilterTeam count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"CellIdentifier";    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    NSUInteger row = [indexPath row];    NSDictionary *rowDict = [self.listFilterTeam objectAtIndex:row];    cell.myLabel.text = [rowDict objectForKey:@"name"];        NSString *imagePath = [rowDict objectForKey:@"image"];    imagePath = [imagePath stringByAppendingString:@".png"];    cell.myImageView.image = [UIImage imageNamed:imagePath];    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    return cell;}- (void)filterContentForSearchText:(NSString *)searchText scope:(NSUInteger)scope {    if ([searchText length] == 0) {        self.listFilterTeam = [NSMutableArray arrayWithArray:self.listTeam];        return;    }        NSPredicate *scopePredicate;    NSArray *tempArray;        switch (scope) {        case 0:            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];            tempArray = [self.listTeam filteredArrayUsingPredicate:scopePredicate];            self.listFilterTeam = [NSMutableArray arrayWithArray:tempArray];            break;        case 1:            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name1 contains[c] %@", searchText];            tempArray = [self.listTeam filteredArrayUsingPredicate:scopePredicate];            self.listFilterTeam = [NSMutableArray arrayWithArray:tempArray];            break;        default:            self.listFilterTeam = [NSMutableArray arrayWithArray:self.listTeam];            break;    }}- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {    self.searchBar.showsScopeBar = TRUE;    [self.searchBar sizeToFit];    return YES;}- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {    self.searchBar.showsScopeBar = NO;    [self.searchBar sizeToFit];    [self.searchBar resignFirstResponder];}- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {    [self filterContentForSearchText:self.searchBar.text scope:-1];    self.searchBar.showsScopeBar = NO;    [self.searchBar sizeToFit];    [self.searchBar resignFirstResponder];}- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {    [self filterContentForSearchText:self.searchBar.text scope:self.searchBar.selectedScopeButtonIndex];    [self.tableView reloadData];}- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {    [self filterContentForSearchText:self.searchBar.text scope:selectedScope];    [self.tableView reloadData];}@end




10、关于TableVIew和SearchBar的一些协议介绍:




0 0