自定义UITableViewCell与分组索引实现(一)

来源:互联网 发布:php爬虫教程 编辑:程序博客网 时间:2024/05/21 22:21
1.     最终要实现的效果是下图的效果:






新建一个项目,我这里的命名是Dictionary,选择的模版是view-based。然后在建立一个NewCell的类,父类是tableiviewcell,用来自定义cell的样子。下面开始NewCell.h的代码,只添加了2label的输出口:@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里面链接好以后,分别设置textonetexttwotext
        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
原创粉丝点击