自定义UITableViewCell与分组索引实现(一)
来源:互联网 发布:php爬虫教程 编辑:程序博客网 时间:2024/05/21 22:21
1. 最终要实现的效果是下图的效果:
新建一个项目,我这里的命名是Dictionary,选择的模版是view-based。然后在建立一个NewCell的类,父类是tableiviewcell,用来自定义cell的样子。下面开始NewCell.h的代码,只添加了2个label的输出口:@interface NewCell : UITableViewCell {
UILabel *textOne;
UILabel *textTwo;
}
@property (nonatomic ,retain) IBOutlet UILabel *textOne;
@property (nonatomic ,retain) IBOutlet UILabel *textTwo;
@end
在NewCell.m里添加两行代码@synthesize textOne;
@synthesize textTwo;
然后在Dictionary.h里定义字典和数组//添加委托和数据源
@interface DictionaryViewController : UIViewController
{
//定义字典和数组
NSDictionary *dictionary;
NSArray *list;
}
@property (nonatomic ,retain) NSDictionary *dictionary;
@property (nonatomic ,retain) NSArray *list;
@end
然后是Dictionary.m文件#import "DictionaryViewController.h"
#import "NewCell.h"
@implementation DictionaryViewController
@synthesize list;
@synthesize dictionary;
- (void)viewDidLoad {
//加载文件
NSString *path = [ pathForResource:@"sortednames" ofType:@"plist"];
//从加载的文件新建一个字典
NSDictionary *dict = [ initWithContentsOfFile:path];
//把新建的字典分配给disctionary
self.dictionary = dict;
//把字典里的数组按照字母顺序排序
NSArray *array = [ sortedArrayUsingSelector:@selector(compare:)];
//分配给list
self.list = array;
;
}
- (void)viewDidUnload {
self.list = nil;
self.dictionary = nil;
}
- (void)dealloc {
;
;
;
}
#pragma mark -
#pragma mark Data Source Methods
//在tableview中有多少个分组
- (NSInteger)numberOfSecposition:ableView:(UITableView *)tableView
{
//每个数组都有一个分组
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//获取分组
NSString *key = ;
//获取分组里面的数组
NSArray *array =;
return ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//索引路径
NSInteger section = ;
NSInteger row = ;
//获取分组
NSString *key = ;
//获取分组里面的数组
NSArray *array =;
//建立可重用单元标识
static NSString *customCell = @"customCell";
NewCell *cell = (NewCell *);
if (cell == nil) {
//如果没有可重用的单元,我们就从nib里面加载一个,
NSArray *nib = [ loadNibNamed:@"NewCell"
owner:self options:nil];
//迭代nib重的所有对象来查找NewCell类的一个实例
for (id oneObject in nib) {
if (]) {
cell = (NewCell *)oneObject;
}
}
}
//在xib里面链接好以后,分别设置textone和texttwo的text值
NSString *ShowTextTwo = ;
NSString *newText = [ initWithFormat:@"数据在“%@”这个分组里",ShowTextTwo];
cell.textOne.text = ;
cell.textTwo.text = newText;
;
return cell;
}
//获取分组标题并显示
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = ;
return key;
}
//给tableviewcell添加索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return list;
}
//重新设置一下tableviewcell的行高为70
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
@end
新建一个项目,我这里的命名是Dictionary,选择的模版是view-based。然后在建立一个NewCell的类,父类是tableiviewcell,用来自定义cell的样子。下面开始NewCell.h的代码,只添加了2个label的输出口:@interface NewCell : UITableViewCell {
UILabel *textOne;
UILabel *textTwo;
}
@property (nonatomic ,retain) IBOutlet UILabel *textOne;
@property (nonatomic ,retain) IBOutlet UILabel *textTwo;
@end
在NewCell.m里添加两行代码@synthesize textOne;
@synthesize textTwo;
然后在Dictionary.h里定义字典和数组//添加委托和数据源
@interface DictionaryViewController : UIViewController
{
//定义字典和数组
NSDictionary *dictionary;
NSArray *list;
}
@property (nonatomic ,retain) NSDictionary *dictionary;
@property (nonatomic ,retain) NSArray *list;
@end
然后是Dictionary.m文件#import "DictionaryViewController.h"
#import "NewCell.h"
@implementation DictionaryViewController
@synthesize list;
@synthesize dictionary;
- (void)viewDidLoad {
//加载文件
NSString *path = [ pathForResource:@"sortednames" ofType:@"plist"];
//从加载的文件新建一个字典
NSDictionary *dict = [ initWithContentsOfFile:path];
//把新建的字典分配给disctionary
self.dictionary = dict;
//把字典里的数组按照字母顺序排序
NSArray *array = [ sortedArrayUsingSelector:@selector(compare:)];
//分配给list
self.list = array;
;
}
- (void)viewDidUnload {
self.list = nil;
self.dictionary = nil;
}
- (void)dealloc {
;
;
;
}
#pragma mark -
#pragma mark Data Source Methods
//在tableview中有多少个分组
- (NSInteger)numberOfSecposition:ableView:(UITableView *)tableView
{
//每个数组都有一个分组
return ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//获取分组
NSString *key = ;
//获取分组里面的数组
NSArray *array =;
return ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//索引路径
NSInteger section = ;
NSInteger row = ;
//获取分组
NSString *key = ;
//获取分组里面的数组
NSArray *array =;
//建立可重用单元标识
static NSString *customCell = @"customCell";
NewCell *cell = (NewCell *);
if (cell == nil) {
//如果没有可重用的单元,我们就从nib里面加载一个,
NSArray *nib = [ loadNibNamed:@"NewCell"
owner:self options:nil];
//迭代nib重的所有对象来查找NewCell类的一个实例
for (id oneObject in nib) {
if (]) {
cell = (NewCell *)oneObject;
}
}
}
//在xib里面链接好以后,分别设置textone和texttwo的text值
NSString *ShowTextTwo = ;
NSString *newText = [ initWithFormat:@"数据在“%@”这个分组里",ShowTextTwo];
cell.textOne.text = ;
cell.textTwo.text = newText;
;
return cell;
}
//获取分组标题并显示
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = ;
return key;
}
//给tableviewcell添加索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return list;
}
//重新设置一下tableviewcell的行高为70
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
@end
0 0
- 自定义UITableViewCell与分组索引实现(一)
- 198,自定义UITableViewCell(一)
- 自定义UITableViewCell并实现重用
- (七)-自定义UITableViewCell
- Union 实现自定义分组
- MapReduce自定义分组实现
- 自定义UITableViewCell (通过代码实现:每个CELL格式不一样)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的使用)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- 自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
- resin访问 admin页面
- volatile
- Windows事件日志详解--登陆类型
- 东北大学计算机专业研究生入学考试2000年真题
- LO Cockpit data extraction
- 自定义UITableViewCell与分组索引实现(一)
- Stanford机器学习Prolem Set#1:Supervised Learning
- 'module' object has no attribute 'rindex'
- 进制转换
- 百度不收录内页的原因
- IE浏览器缓存Ajax请求之后不更新数据问题
- Ubuntu 12.04 静态ip的设置方法
- Android中的Selector的用法
- 将网络中的图片存为NSData并保存到sqlite的BLOB字段中