【代码笔记】iOS-带索引的tableView
2024-08-21 13:23:32
一,效果图。
二,工程图。
三,代码。
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource,UISearchDisplayDelegate,UISearchBarDelegate>
{
UITableView * tableview;
NSMutableArray * Aarray;
UISearchBar * searchBar;
UISearchDisplayController *searchControl;
}
@end
RootViewController.m
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. //初始化背景图
[self initBackGroundView];
}
#pragma -mark -functions
-(void)initBackGroundView
{
Aarray = [[NSMutableArray alloc] initWithObjects:@"+",@"我",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; //tableView
tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460-44-10-10-20) style:UITableViewStylePlain];
tableview.tag = 101;
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview]; //searchView
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
tableview.tableHeaderView = searchBar;
searchBar.showsScopeBar = YES;
searchBar.placeholder = @"在全部关注中搜索..."; //搜索的时候会有左侧滑动的效果
searchControl = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
searchControl.delegate = self;
searchControl.searchResultsDataSource = self;
searchControl.searchResultsDelegate = self; }
#pragma -mark -searchbar
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
;
}
#pragma -mark -UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
}
return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40.0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * strID = @"ID";
UITableViewCell * cell = [tableview dequeueReusableCellWithIdentifier:strID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strID];
} if (indexPath.section == 0 && indexPath.row == 0) {
cell.textLabel.text = @"找朋友";
}else if(indexPath.section ==1 && indexPath.row == 0){
cell.textLabel.text=@"row";
}
return cell;
} -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 1) {
return @"我的资料";
}
return nil;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [NSArray arrayWithObjects:UITableViewIndexSearch,@"+",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#", nil];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
最新文章
- MySQL frm+ibd文件还原data的办法【数据恢复】
- WinPcap4.13无法安装解决方法
- 获取表单选中的值(利用php和js两种方式)
- Linux:常用快捷键
- Rapha&#235;l 是一个小型的 JavaScript 库,用来简化在页面上显示向量图的工作。你可以用它在页面上绘制各种图表、并进行图片的剪切、旋转等操作。
- Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)
- 【转】Android菜单详解——理解android中的Menu--不错
- (转)IIS7 下部署Asp.net应用
- 剑指offer ------ 刷题总结
- 手机自动化测试:appium源码分析之bootstrap四
- OpenCV 读取视频 多种方式
- 怎么选择公司???MVC加jquery-easyui 后端工程师
- Android Studio 查看手机CPU信息
- python3之迭代器&;生成器
- 解决ubuntu系统root用户下Chrome无法启动问题
- LocalBroadcastManager—创建更高效、更安全的广播
- spring Ioc容器之使用XML配置Bean
- javaMail实现收发邮件(四)
- BSGS及扩展BSGS总结(BSGS,map)
- [视频]K8飞刀--WinRAR远程代码执行漏洞利用视频