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的一些协议介绍:
- Xcode_7 iOS_9 表视图 Objective-C (10)
- Xcode_7 iOS_9 静态表 Objective-C (12)
- Xcode_7 iOS_9 屏幕滚动视图 Objective-C (6)
- Xcode_7 iOS_9 集合视图_CollectionViewController Objective-C (9)
- Xcode_7 iOS_9 模态视图 Objective-C (15)
- Xcode_7 iOS_9 Switch_SegmentControl_Slider 控件 Objective-C (2)
- Xcode_7 iOS_9 日期选择器 Objective-C (7)
- Xcode_7 iOS_9 普通选择器 Objective-C (8)
- Xcode_7 iOS_9 索引栏 Objective-C (11)
- Xcode_7 iOS_9 分页刷新 Objective-C (14)
- Xcode_7 iOS_9 ActiveIndicatorView_ProgressView_警告窗_操作表 控件 Objective-C (3)
- Xcode_7 iOS_9 textField和textView 关闭键盘 Objective-C (1)
- Xcode_7 iOS_9 工具栏_导航栏 Objective-C (4)
- Xcode_7 iOS_9 获取设备型号 Objective-C (5)
- Xcode_7 iOS_9 修改单元格_移动单元格 Objective-C (13)
- Xcode_7 iOS_9 平铺导航_标签导航 Objective-C (16)
- Xcode_7 iOS_9 树形结构导航 Objective-C (17)
- Objective-C 表视图-自定义单元格
- 为什么ConcurrentHashMap是弱一致的
- POJ 2253 Frogger 【Floyd】
- 字符,字节和编码
- struts2实现收入校验源代码
- 浮士德html5图片裁剪器2016开源版
- Xcode_7 iOS_9 表视图 Objective-C (10)
- 面向对象程序设计的基本特点
- 在ScrollView上添加button谁先响应?
- Python迭代器(Iterator)
- 【GO】GO在linux的安装
- ubuntu14.04增加一个用户并且给予sudo的权限
- Android View总结
- java常用的文件读写操作
- LocalActivityManager