• 134在单元格中自动排列指定的数据


    效果如下:

    ViewController.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface ViewController : UITableViewController
    4 @property (strong, nonatomic) NSMutableArray *mArrDataList;
    5 @property (strong, nonatomic) NSMutableArray *mArrSortedCollation;
    6 
    7 @end

    ViewController.m

      1 #import "ViewController.h"
      2 #import "Friend.h"
      3 
      4 #define collation [UILocalizedIndexedCollation currentCollation]
      5 @interface ViewController ()
      6 - (void)layoutUI;
      7 - (void)loadData;
      8 - (void)loadCollationData;
      9 @end
     10 
     11 @implementation ViewController
     12 
     13 - (void)viewDidLoad {
     14     [super viewDidLoad];
     15     
     16     [self layoutUI];
     17 }
     18 
     19 - (void)didReceiveMemoryWarning {
     20     [super didReceiveMemoryWarning];
     21     // Dispose of any resources that can be recreated.
     22 }
     23 
     24 - (void)layoutUI {
     25     [self loadData];
     26     [self loadCollationData];
     27     
     28     self.view.backgroundColor = [UIColor whiteColor];
     29     self.navigationItem.title = @"在单元格中自动排列指定的数据";
     30 }
     31 
     32 /**
     33  *  加载数据
     34  */
     35 - (void)loadData {
     36     NSBundle *bundle = [NSBundle mainBundle];
     37     NSURL *urlFriendsInfo = [bundle URLForResource:@"FriendsInfo" withExtension:@"plist"];
     38     NSDictionary *dicFriendsInfo = [NSDictionary dictionaryWithContentsOfURL:urlFriendsInfo];
     39     NSInteger len = [dicFriendsInfo count];
     40     _mArrDataList = [[NSMutableArray alloc] initWithCapacity:len];
     41     for (NSInteger i=0; i<len; i++) {
     42         NSString *strKey = [NSString stringWithFormat:@"%lu", (unsigned long)(i+1)];
     43         NSDictionary *dicData = [dicFriendsInfo objectForKey:strKey];
     44         
     45         Friend *friend = [[Friend alloc] initName:[dicData objectForKey:@"name"]
     46                                          withDesc:[dicData objectForKey:@"desc"]
     47                                      withLocation:[dicData objectForKey:@"location"]
     48                                       withImgIcon:[UIImage imageNamed:strKey]];
     49         [_mArrDataList addObject:friend];
     50     }
     51 }
     52 
     53 /**
     54  *  加载排序规则数据
     55  */
     56 - (void)loadCollationData {
     57     //UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
     58     NSUInteger sectionCount = [collation sectionTitles].count; //分组总数为27;即A-Z, #。([collation sectionTitles]和[collation sectionIndexTitles]的值是相同的,指针地址不同)
     59     NSLog(@"排序规则分组标题是%@", [collation sectionTitles]);
     60     
     61     //创建包含2层嵌套内容的可变长数组mArrCollation
     62     NSMutableArray *mArrCollation = [[NSMutableArray alloc] initWithCapacity:sectionCount]; //第1层
     63     for (NSUInteger i=0; i< sectionCount; i++) {
     64         [mArrCollation addObject:[[NSMutableArray alloc] initWithCapacity:1]]; //第2层
     65     }
     66     
     67     for (Friend *friend in _mArrDataList) {
     68         NSInteger section = [collation sectionForObject:friend
     69                                 collationStringSelector:@selector(name)]; //注意这里使用排序规则的方法,需为对象;不能为NSDictionary(无法找到适合的selector)
     70         [mArrCollation[section] addObject:friend];
     71     }
     72     
     73     //将mArrCollation的第2层数据进行排序并逐个分组添加到新的可变长数组_mArrSortedCollation
     74     _mArrSortedCollation = [[NSMutableArray alloc] initWithCapacity:sectionCount];
     75     for (NSMutableArray *mArrSection in mArrCollation) {
     76         NSArray *arrSortedSection = [collation sortedArrayFromArray:mArrSection
     77                                             collationStringSelector:@selector(name)]; //注意这里使用排序规则的方法,需为对象;不能为NSDictionary(无法找到适合的selector)
     78         [_mArrSortedCollation addObject:arrSortedSection];
     79     }
     80 }
     81 
     82 #pragma mark - TableView
     83 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
     84     NSString *strTitle = nil;
     85     if ([_mArrSortedCollation[section] count] > 0) {
     86         strTitle = [collation sectionTitles][section];
     87     }
     88     return strTitle;
     89 }
     90 
     91 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     92     return _mArrSortedCollation.count;
     93 }
     94 
     95 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     96     return [_mArrSortedCollation[section] count];
     97 }
     98 
     99 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    100     static NSString *cellIdentifier = @"cellIdentifier";
    101     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    102     if (!cell) {
    103         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    104     }
    105     
    106     Friend *friend = _mArrSortedCollation[indexPath.section][indexPath.row];
    107     cell.textLabel.text = friend.name;
    108     cell.detailTextLabel.text = friend.desc;
    109     cell.imageView.image = friend.imgIcon;
    110     
    111     return cell;
    112     
    113     
    114     /*
    115      UITableViewCellStyleDefault:带imageView;只含水平居左的textLabel
    116      UITableViewCellStyleValue1:带imageView;含水平居左的textLabel和水平居右的detailTextLabel(左右结构)
    117      UITableViewCellStyleValue2:不带imageView;含水平居左的textLabel和水平居左的detailTextLabel(左右结构)
    118      UITableViewCellStyleSubtitle:带imageView;含垂直居上的textLabel和垂直居下的detailTextLabel(上下结构)
    119      */
    120     
    121     /*
    122      typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    123      UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
    124      UITableViewCellStyleValue1,        // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
    125      UITableViewCellStyleValue2,        // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
    126      UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
    127      };             // available in iPhone OS 3.0
    128      */
    129 }
    130 
    131 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    132     return 60.0;
    133 }
    134 
    135 #pragma mark - TableView, sectionIndexTitle
    136 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    137     return [collation sectionIndexTitles];
    138 }
    139 
    140 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    141     return [collation sectionForSectionIndexTitleAtIndex:index];
    142 }
    143 
    144 @end

    Friend.h

     1 #import <Foundation/Foundation.h>
     2 #import <UIKit/UIKit.h>
     3 
     4 @interface Friend : NSObject
     5 @property (copy, nonatomic) NSString *name;
     6 @property (copy, nonatomic) NSString *desc;
     7 @property (copy, nonatomic) NSString *location;
     8 @property (strong, nonatomic) UIImage *imgIcon;
     9 - (id)initName:(NSString *)name withDesc:(NSString *)desc withLocation:(NSString *)location withImgIcon:(UIImage *)imgIcon;
    10 
    11 @end

    Friend.m

     1 #import "Friend.h"
     2 
     3 @implementation Friend
     4 
     5 - (id)initName:(NSString *)name withDesc:(NSString *)desc withLocation:(NSString *)location withImgIcon:(UIImage *)imgIcon {
     6     if (self = [super init]) {
     7         self.name = name;
     8         self.desc = desc;
     9         self.location = location;
    10         self.imgIcon = imgIcon;
    11     }
    12     return self;
    13 }
    14 
    15 @end

    AppDelegate.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    4 @property (strong, nonatomic) UIWindow *window;
    5 @property (strong, nonatomic) UINavigationController *navigationController;
    6 
    7 @end

    AppDelegate.m

     1 #import "AppDelegate.h"
     2 #import "ViewController.h"
     3 
     4 @interface AppDelegate ()
     5 @end
     6 
     7 @implementation AppDelegate
     8 
     9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    11     ViewController *viewController = [[ViewController alloc] init];
    12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    13     _window.rootViewController = _navigationController;
    14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
    15     [_window makeKeyAndVisible];
    16     return YES;
    17 }
    18 
    19 - (void)applicationWillResignActive:(UIApplication *)application {
    20 }
    21 
    22 - (void)applicationDidEnterBackground:(UIApplication *)application {
    23 }
    24 
    25 - (void)applicationWillEnterForeground:(UIApplication *)application {
    26 }
    27 
    28 - (void)applicationDidBecomeActive:(UIApplication *)application {
    29 }
    30 
    31 - (void)applicationWillTerminate:(UIApplication *)application {
    32 }
    33 
    34 @end

    FriendsInfo.plist

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
      3 <plist version="1.0">
      4 <dict>
      5     <key>1</key>
      6     <dict>
      7         <key>name</key>
      8         <string>小明</string>
      9         <key>desc</key>
     10         <string>干啥呢?</string>
     11         <key>location</key>
     12         <string>广州</string>
     13     </dict>
     14     <key>2</key>
     15     <dict>
     16         <key>name</key>
     17         <string>痞子</string>
     18         <key>desc</key>
     19         <string>好好学习,天天向上!</string>
     20         <key>location</key>
     21         <string>广州</string>
     22     </dict>
     23     <key>3</key>
     24     <dict>
     25         <key>name</key>
     26         <string>疯子</string>
     27         <key>desc</key>
     28         <string>倚楼听风雨,淡看江湖路。</string>
     29         <key>location</key>
     30         <string>广州</string>
     31     </dict>
     32     <key>4</key>
     33     <dict>
     34         <key>name</key>
     35         <string>梦醒</string>
     36         <key>desc</key>
     37         <string>书到用时方恨少</string>
     38         <key>location</key>
     39         <string>广州</string>
     40     </dict>
     41     <key>5</key>
     42     <dict>
     43         <key>name</key>
     44         <string>落落</string>
     45         <key>desc</key>
     46         <string>生日快乐!</string>
     47         <key>location</key>
     48         <string>广州</string>
     49     </dict>
     50     <key>6</key>
     51     <dict>
     52         <key>name</key>
     53         <string>丫丫</string>
     54         <key>desc</key>
     55         <string>做个踏实的科研女</string>
     56         <key>location</key>
     57         <string>广州</string>
     58     </dict>
     59     <key>7</key>
     60     <dict>
     61         <key>name</key>
     62         <string>乐天平</string>
     63         <key>desc</key>
     64         <string>在火车上</string>
     65         <key>location</key>
     66         <string>广州</string>
     67     </dict>
     68     <key>8</key>
     69     <dict>
     70         <key>name</key>
     71         <string>北暮</string>
     72         <key>desc</key>
     73         <string>好久不见!</string>
     74         <key>location</key>
     75         <string>广州</string>
     76     </dict>
     77     <key>9</key>
     78     <dict>
     79         <key>name</key>
     80         <string>苹果</string>
     81         <key>desc</key>
     82         <string>喜欢苹果,更喜欢青苹果!</string>
     83         <key>location</key>
     84         <string>广州</string>
     85     </dict>
     86     <key>10</key>
     87     <dict>
     88         <key>name</key>
     89         <string>木头</string>
     90         <key>desc</key>
     91         <string>清心薄欲 静躁作学</string>
     92         <key>location</key>
     93         <string>广州</string>
     94     </dict>
     95     <key>11</key>
     96     <dict>
     97         <key>name</key>
     98         <string>醉清风</string>
     99         <key>desc</key>
    100         <string>一醉解千愁</string>
    101         <key>location</key>
    102         <string>广州</string>
    103     </dict>
    104     <key>12</key>
    105     <dict>
    106         <key>name</key>
    107         <string>浅の斯</string>
    108         <key>desc</key>
    109         <string>想剪短发……剪还是不剪(⊙o⊙)?</string>
    110         <key>location</key>
    111         <string>广州</string>
    112     </dict>
    113     <key>13</key>
    114     <dict>
    115         <key>name</key>
    116         <string>虚伪</string>
    117         <key>desc</key>
    118         <string>讨厌虚伪</string>
    119         <key>location</key>
    120         <string>广州</string>
    121     </dict>
    122     <key>14</key>
    123     <dict>
    124         <key>name</key>
    125         <string>阁楼</string>
    126         <key>desc</key>
    127         <string>窗外的风景。</string>
    128         <key>location</key>
    129         <string>广州</string>
    130     </dict>
    131 </dict>
    132 </plist>
  • 相关阅读:
    融资担保公司
    典当公司
    保险代理、经纪公司互联网保险
    财产、人身、养老保险公司
    105家基金子公司
    LogStash Download
    cmd使用管理员权限运行,启动路径不是当前目录
    Module controller in JMeter
    Elasticsearch-->Get Started--> Exploring Your Data
    What are the differences between Flyweight and Object Pool patterns?
  • 原文地址:https://www.cnblogs.com/huangjianwu/p/4581385.html
Copyright © 2020-2023  润新知