[ios]iOS学习之UITableView(三):进阶篇索引,标记和自定义的table

来源:互联网 发布:unity3d 自学网 编辑:程序博客网 时间:2024/05/24 06:28


目录[-]

  • 一、带索引目录的表视图
  • 二、可以进行行标记的表视图
  • 三、定制表视图的每一行内容
  • 代码下载

  • 一、带索引目录的表视图

          ①效果图

                                     图1 带索引的列表

               

           ② 数据源

                本想获取通讯录中得名字,但为了用模拟器调试方便,就写死了数据,所以也只写了部分字母,总之有那么点意思就成

    1@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    2{
    3    NSArray *sectionTitles; // 每个分区的标题
    4    NSArray *contentsArray; // 每行的内容
    5}


    01/** @brief 准备数据源 在viewDidLoad方法中调用*/
    02- (void)readySource
    03{
    04     
    05    sectionTitles       = [[NSArray alloc] initWithObjects:
    06                           @"A",@"C",@"F",@"G",@"H",@"M",@"S",@"T",@"X",@"Z", nil];
    07    contentsArray       = [[NSArray alloc] initWithObjects:
    08                            @[@"阿伟",@"阿姨",@"阿三"],
    09                            @[@"蔡芯",@"成龙",@"陈鑫",@"陈丹",@"成名"],
    10                            @[@"芳仔",@"房祖名",@"方大同",@"芳芳",@"范伟"],
    11                            @[@"郭靖",@"郭美美",@"过儿",@"过山车"],
    12                            @[@"何仙姑",@"和珅",@"郝歌",@"好人"],
    13                            @[@"妈妈",@"毛主席"],
    14                            @[@"孙中山",@"沈冰",@"婶婶"],
    15                            @[@"涛涛",@"淘宝",@"套娃"],
    16                            @[@"小二",@"夏紫薇",@"许巍",@"许晴"],
    17                            @[@"周恩来",@"周杰伦",@"张柏芝",@"张大仙"],nil];
    18}
             ③显示索引
    01// 每个分区的页眉
    02-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    03{
    04    return [sectionTitles objectAtIndex:section];
    05}
    06// 索引目录
    07-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    08{
    09    return sectionTitles;
    10}

            ④点击索引,跳转到点击的分区

    01// 点击目录
    02-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
    03{
    04    // 获取所点目录对应的indexPath值
    05    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
    06     
    07    // 让table滚动到对应的indexPath位置
    08    [tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    09     
    10    return index;
    11}

    二、可以进行行标记的表视图

            ①效果图

                                                图2 可标记的列表

                        

        ②在cellForRow方法中,将Cell的accessoryType设置为None

    1// 定义其辅助样式
    2       cell.accessoryType      = UITableViewCellAccessoryNone;
         ③在didSelectRow方法中
    01// 点击行事件
    02-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    03{
    04    // 获取点击行的cell
    05    UITableViewCell *cell   = [tableView cellForRowAtIndexPath:indexPath];
    06     
    07    // 如果cell已经被标记
    08    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    09        // 取消标记
    10        cell.accessoryType  = UITableViewCellAccessoryNone;
    11    }
    12     
    13    // 如果cell未标记
    14    else{
    15        // 标记cell
    16        cell.accessoryType  = UITableViewCellAccessoryCheckmark;
    17    }
    18    // 取消选中效果
    19    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    20}

    此时,点击行即可选中,取消选中,但是滚动一下视图吧,你会发现下面某些未被点击的行也已经被标记了,这是因为cell的重用机制造成的,在第一篇文章中就这个问题有提到过

    ④解决cell重用问题,在cellForRow方法中,定义cellIdetifier时,将其每一行都定义为不同的值,就不会出现覆盖,重复等现象了

    1NSString *cellIdentifier = [NSString stringWithFormat:@"cellIdentifier%d%d",indexPath.row,indexPath.section];

    三、定制表视图的每一行内容

        ①我们做一个类似网易新闻客户端的新闻列表的table,如图3;简易效果图,如图4

                           图3  网易新闻效果                                 图4 demo效果

          

        ②数据源,在interface中声明

    1NSMutableArray *news_MArray;// 新闻内容数据源

        新建一个model类,命名为"newsModel",存放每一项数据

        newsModel.h如下,.m中没有添加其他代码,如果需要拷贝,可以重载copyWithZone方法,参考http://my.oschina.net/joanfen/blog/135053

    01#import <Foundation/Foundation.h>
    02 
    03typedef NS_ENUM(NSInteger, NEWSReportType){
    04    NEWSReportOrdinary, // 普通新闻
    05    NEWSReportExclusive,// 独家新闻
    06    NEWSReportSpecial,  // 专题新闻
    07};
    08 
    09@interface newsModel : NSObject
    10 
    11@property (nonatomic, copy)NSString *       news_image;     //图片
    12@property (nonatomic, copy)NSString *       news_title;     //标题
    13@property (nonatomic, copy)NSString *       news_summary;   //摘要
    14@property (nonatomic, assign)NSInteger      news_replyNo;   //跟帖数量
    15@property (nonatomic, assign)NEWSReportType reportType;     //报道类型
    16 
    17 
    18@end

        在viewDidLoad方法中

    01news_MArray = [[NSMutableArray alloc] init];
    02for(NSInteger index =0; index<10; index++){
    03    newsModel *model    = [[newsModel alloc] init];
    04    model.news_image    = [NSString stringWithFormat:@"%d.jpg",index+1];
    05    model.news_title    = @"曾在月光之下望烟花";
    06    model.news_summary  = @"曾共看夕阳渐降下 我怎么舍得去放下 要怎么舍得去放下";
    07    model.news_replyNo  = index+196;
    08    model.reportType    = index%3;
    09     
    10    [news_MArray addObject:model];
    11}

        ③行数

    1// 每个分区行数
    2-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    3{
    4    return [news_MArray count];
    5}

        ④自定义cell上控件

        在cellForRow方法中if(cell==nil)前

    1/*****自定义cell******/
    2newsModel *model    = [news_MArray objectAtIndex:indexPath.row];
    3 
    4UIImageView *   image_view;     //1.添加imageView
    5UILabel *       title_label;    //2.添加标题Label
    6UILabel *       summary_label;  //3.添加摘要Label
    7UILabel *       replyNo_label;  //4.添加跟帖数量Label
    8UIButton  *     extra_view;     //5.属于专题或者独家报道,进行标记
    9/********************/

        在if(cell==nil)内

    01/*****自定义cell******/
    02         
    03        //1.添加imageView
    04        CGRect imageViewF   = CGRectMake(5, 5, 85, 65);
    05        image_view          = [[UIImageView alloc] initWithFrame:imageViewF];
    06        [cell addSubview:image_view];
    07         
    08        //2.添加标题Label
    09        CGRect titleLabelF  = CGRectMake(95, 5, 230, 24);
    10        title_label         = [[UILabel alloc] initWithFrame:titleLabelF];
    11        title_label.font    = [UIFont systemFontOfSize:16];//字体大小
    12        [cell addSubview:title_label];
    13         
    14        //3.添加摘要Label
    15        CGRect summaryLabelF  = CGRectMake(97, 27, 210, 40);
    16        summary_label         = [[UILabel alloc] initWithFrame:summaryLabelF];
    17        summary_label.font    = [UIFont systemFontOfSize:12];    // 字体大小
    18        summary_label.textColor     = [UIColor darkGrayColor];  // 文字颜色
    19        summary_label.numberOfLines = 2;
    20        [cell addSubview:summary_label];
    21         
    22        //4.跟帖数量Label
    23        CGRect replyNoLabelF  = CGRectMake(210, 45, 95, 24);
    24        replyNo_label         = [[UILabel alloc] initWithFrame:replyNoLabelF];
    25        replyNo_label.font    = [UIFont systemFontOfSize:12];    // 字体大小
    26        replyNo_label.textColor     = [UIColor darkGrayColor];  // 文字颜色
    27        replyNo_label.textAlignment = NSTextAlignmentRight;      // 文字右对齐
    28         
    29        //5.专题extraView
    30        CGRect extraViewF       = CGRectMake(270, 50, 28, 14);
    31        extra_view              = [[UIButton alloc] initWithFrame:extraViewF];
    32        extra_view.titleLabel.font = [UIFont boldSystemFontOfSize:10];
    33        [extra_view setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    34         
    35        // 普通新闻,只添加跟帖数量
    36        if (model.reportType==NEWSReportOrdinary) {
    37           [cell addSubview:replyNo_label];
    38        }
    39        // 专题新闻,添加专题标志,并添加跟帖数量
    40        else if(model.reportType == NEWSReportSpecial){
    41             
    42            // 设置背景色
    43            extra_view.backgroundColor = [UIColor colorWithRed:120.0/255.0 green:170.0/255.0 blue:245.0/255.0 alpha:1.0];
    44             
    45            [extra_view setTitle:@"独家" forState:UIControlStateNormal];// 设置标题
    46             
    47            [cell addSubview:extra_view];                               // 添加
    48             
    49            replyNo_label.frame = CGRectMake(170, 45, 95, 24);          // 改变跟帖数量Label的坐标
    50             
    51            [cell addSubview:replyNo_label];                            // 添加跟帖数量Label
    52        }
    53        // 独家新闻,只添加独家标志
    54        else if(model.reportType == NEWSReportExclusive){
    55             
    56            extra_view.backgroundColor = [UIColor redColor];            // 设置背景颜色
    57             
    58            [extra_view setTitle:@"专题" forState:UIControlStateNormal]; // 设置标题
    59             
    60            [cell addSubview:extra_view];                               // 添加到cell
    61        }
    62/********************/

        在if(cell==nil)后

    1/*****自定义cell******/
    2    [image_view setImage:[UIImage imageNamed:model.news_image]];// 设置图片
    3    title_label.text    = model.news_title;                     // 设置标题
    4    summary_label.text  = model.news_summary;                   // 设置小标题
    5    replyNo_label.text  = [NSString stringWithFormat:@"%d 跟帖",model.news_replyNo];// 设置跟帖数量
    6/********************/

        ⑤设置行高

    1-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    2{
    3    return 75;
    4}

    代码下载

    http://www.oschina.net/action/code/download?code=33723&id=48636

    0 0
    原创粉丝点击