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>
时间: 2024-12-29 21:18:05

134在单元格中自动排列指定的数据的相关文章

Swift - 异步加载各网站的favicon图标,并在单元格中显示

下面是一个简单的应用,表格视图的各个单元格自动异步加载各个网站的favicon图标,并显示出来. 主要是复习下如何自定义单元格,单元格中图片的异步加载,以及didSet的用法. 效果图如下: 操作步骤: (1)先创建单元格类 - FaviconTableViewCell.swift 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

DataGridView单元格内容自动匹配下拉显示

页面显示数据使用的控件是ComponentFactory.Krypton.Toolkit中的KryptonDataGridView控件.在指定“商品”单元格中需要根据用户输入内容自动匹配数据库中商品信息,并且单元格处于编辑模式时显示一个查询图标的按钮,点击该按钮也将显示数据库中所有商品信息. KryptonDataGridView显示控件此处命名为kDGVIndentDetail; 用于下拉显示匹配内容的DataGridView命名为dgv; 1.建立一个DataGridView类型的页面变量用

Swift - 可编辑表格样例(可直接编辑单元格中内容、移动删除单元格)

(本文代码已升级至Swift3) 本文演示如何制作一个可以编辑单元格内容的表格(UITableView). 1,效果图 (1)默认状态下,表格不可编辑,当点击单元格的时候会弹出提示框显示选中的内容.           (2)点击导航栏右侧编辑按钮,表格进入可以编辑状态 (3)这时我们可以删除表格项. (4)也可以拖动调整单元格的顺序. (5)然后就是本文的重点,在编辑状态下.直接点击单元格,即可在当前页面下直接编辑修改单元格中的内容. 2,单元格编辑功能讲解 (1)通过自定义 UITableV

Rdlc技巧,rdlc报表中获取文本框或某个表格单元格中的内容

在RDLC报表中,随意摆放的控件,排列看起来很整齐,但是生成WEB后看就不是很友好了,导出PDF可能又是一个样, 解决这个办法就是把这些摆放在一个容器内,比如Table 内 用ReportItems这个属性来取value值.细节如下 =ReportItems!textbox1.Value + ReportItems!textbox12.Value 注意,页头页脚可以取Body里的值,但是Body里不可以取页头与页脚的值 Rdlc技巧,rdlc报表中获取文本框或某个表格单元格中的内容

127使用 TableView 自带的单元格样式实现好友列表,另外在单元格中添加辅助按钮

类似的做法如之前这篇随笔:114自定义 UITableViewCell 实现好友列表(扩展知识:如何使用xib创建自定义的表格视图单元格 KMTableViewCell) 相比之下:自定义 UITableViewCell 的内容灵活,可根据需求调整展示效果,应用场景更广:一般适用于TableView 自带的单元格样式无法实现的效果. 效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController :

Java 在Excel单元格中应用一种/多种字体样式

在Excel表格中,设置单元格字体样式时,可以对单元格内的所有字符应用同一样式,即获取指定单元,应用样式即可:另外也可以对单元格内的不同字符内容应用不同字体样式,即获取单元格中的字符位置,应用样式:本文将通过Java代码示例介绍具体实现方法. 使用工具:Free Spire.XLS for Java (免费版) 注:可通过官网下载包,解压,并将lib文件夹中的Spire.Xls.jar文件导入Java程序:或通过maven仓库导入.导入效果如下: Java代码示例 import com.spir

table表格单元格中的内容如何强制换行

table表格单元格中的内容如何强制换行:有时候表格单元格中的内容不会换行,那么这个就会严重影响到用户体验,下面就简单介绍一下如何实现单元格中的内容换行.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.51texiao.cn/" />

uitable单元格高度自动适配

uitable单元格高度自动适配的前提是你要开启auto layout选项,不明白或者不熟悉的同学先看看这里 http://lvwenhan.com/ios/430.html 按照很多用例上的步骤,不想却踩了坑: 1,禁止实现代理函数 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 2,然后实现 - (CGFloat)tableView:(UITable

单元格中的数字改为文本格式

先选中所有要修改的文本数字单元格→选择Excel 菜单中“数据”菜单→“分列”(如下图) 接着出现下面的对话框: 一直选下一步→下一步→列数据格式选“常规”即可.(如下图) 以上方法,同样如果需要把数字格式转化成文本格式数字,操作中最后一步列数据格式选“文本”就可以了. 另外,我们在使用Excel时是否发现单击文本格式的单元格的时候,单元格的左上方都有一个感叹号,(如下图) 它也可以帮助我们将文本格式的数字转换为常规格式的数字啊?怎么应用它呢?接着看吧! 1.鼠标指向那个小框时,后出现一个向下的