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

类似的做法如之前这篇随笔:114自定义 UITableViewCell 实现好友列表(扩展知识:如何使用xib创建自定义的表格视图单元格 KMTableViewCell)

相比之下:自定义 UITableViewCell 的内容灵活,可根据需求调整展示效果,应用场景更广;一般适用于TableView 自带的单元格样式无法实现的效果。

效果如下:

ViewController.h

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

ViewController.m

  1 #import "ViewController.h"
  2
  3 @interface ViewController ()
  4 - (void)layoutUI;
  5 - (void)loadData;
  6 - (void)accesoryTypeDidChange:(UIBarButtonItem *)sender;
  7 @end
  8
  9 @implementation ViewController
 10
 11 - (void)viewDidLoad {
 12     [super viewDidLoad];
 13
 14     [self layoutUI];
 15 }
 16
 17 - (void)didReceiveMemoryWarning {
 18     [super didReceiveMemoryWarning];
 19     // Dispose of any resources that can be recreated.
 20 }
 21
 22
 23 - (void)layoutUI {
 24     [self loadData];
 25
 26     self.view.backgroundColor = [UIColor whiteColor];
 27     self.navigationItem.title = @"在单元格中添加辅助按钮";
 28     UIBarButtonItem *barBtnAccesoryType = [[UIBarButtonItem alloc] initWithTitle:@"切换辅助按钮类型"
 29                                                                            style:UIBarButtonItemStyleDone
 30                                                                           target:self
 31                                                                           action:@selector(accesoryTypeDidChange:)];
 32     self.navigationItem.rightBarButtonItem = barBtnAccesoryType;
 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     _mArrImageList = [[NSMutableArray alloc] initWithCapacity:len];
 42     for (NSInteger i=0; i<len; i++) {
 43         NSString *strKey = [NSString stringWithFormat:@"%lu", (unsigned long)(i+1)];
 44         NSDictionary *dicData = [dicFriendsInfo objectForKey:strKey];
 45         [_mArrDataList addObject:dicData];
 46
 47         UIImage *img = [UIImage imageNamed:strKey];
 48         [_mArrImageList addObject:img];
 49     }
 50
 51     //设置用于单元格的辅助按钮类型的枚举变量
 52     _accessoryType = UITableViewCellAccessoryNone;
 53 }
 54
 55 - (void)accesoryTypeDidChange:(UIBarButtonItem *)sender {
 56     switch (_accessoryType) {
 57         case UITableViewCellAccessoryNone:
 58         case UITableViewCellAccessoryDisclosureIndicator:
 59         case UITableViewCellAccessoryDetailDisclosureButton:
 60         case UITableViewCellAccessoryCheckmark: {
 61             _accessoryType++;
 62             break;
 63         }
 64         case UITableViewCellAccessoryDetailButton: {
 65             _accessoryType = UITableViewCellAccessoryNone;
 66             break;
 67         }
 68     }
 69     [self.tableView reloadData];
 70 }
 71
 72 #pragma mark - TableView
 73 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
 74     return @"FriendsInfo列表";
 75 }
 76
 77 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 78     return 1;
 79 }
 80
 81 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 82     return [_mArrDataList count];
 83 }
 84
 85 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 86     static NSString *cellIdentifier = @"cellIdentifier";
 87     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 88     if (!cell) {
 89         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
 90     }
 91
 92     NSDictionary *rowData = _mArrDataList[indexPath.row];
 93     cell.textLabel.text = [rowData objectForKey:@"name"];
 94     cell.detailTextLabel.text = [rowData objectForKey:@"desc"];
 95     cell.imageView.image = _mArrImageList[indexPath.row];
 96
 97     //设置单元格的辅助按钮类型;默认值是UITableViewCellAccessoryNone
 98     cell.accessoryType = _accessoryType;
 99     return cell;
100
101
102     /*
103      UITableViewCellStyleDefault:带imageView;只含水平居左的textLabel
104      UITableViewCellStyleValue1:带imageView;含水平居左的textLabel和水平居右的detailTextLabel(左右结构)
105      UITableViewCellStyleValue2:不带imageView;含水平居左的textLabel和水平居左的detailTextLabel(左右结构)
106      UITableViewCellStyleSubtitle:带imageView;含垂直居上的textLabel和垂直居下的detailTextLabel(上下结构)
107      */
108
109     /*
110      typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
111      UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
112      UITableViewCellStyleValue1,        // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
113      UITableViewCellStyleValue2,        // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
114      UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
115      };             // available in iPhone OS 3.0
116      */
117 }
118
119 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
120     return 60.0;
121 }
122
123 /**
124  *  辅助按钮点击委托事件;只针对UITableViewCellAccessoryDetailDisclosureButton和UITableViewCellAccessoryDetailButton类型的有效
125  *
126  *  @param tableView 表格视图
127  *  @param indexPath 当前点击单元格的索引路径信息(关键包含信息:section分组和row行)
128  */
129 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
130     NSMutableString *mStrMessage = [[NSMutableString alloc] initWithFormat:@"您选择了第%ld行的辅助按钮\n类型为:", (indexPath.row + 1)];
131     switch (_accessoryType) {
132         case UITableViewCellAccessoryNone: {
133             //Do nothing
134             break;
135         }
136         case UITableViewCellAccessoryDisclosureIndicator: { //披露指示器
137             //Do nothing
138             break;
139         }
140         case UITableViewCellAccessoryDetailDisclosureButton: { //详细按钮和披露指示器
141             [mStrMessage appendString:@"UITableViewCellAccessoryDetailDisclosureButton 详细按钮和披露指示器"];
142             break;
143         }
144         case UITableViewCellAccessoryCheckmark: { //复选标示
145             //Do nothing
146             break;
147         }
148         case UITableViewCellAccessoryDetailButton: { //详细按钮
149             [mStrMessage appendString:@"UITableViewCellAccessoryDetailButton 详细按钮"];
150             break;
151         }
152     }
153
154     UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示信息"
155                                                      message:mStrMessage
156                                                     delegate:nil
157                                            cancelButtonTitle:nil
158                                            otherButtonTitles:@"确定", nil];
159     [alertV show];
160 }
161
162 @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-08-01 16:33:20

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

128在单元格中添加自定义的辅助按钮

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSMutableArray *mArrDataSource; 5 6 @end ViewController.m 1 #import "ViewController.h" 2 3 @interface Vi

130在单元格中添加自定义控件

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSArray *arrSection; 5 @property (strong, nonatomic) NSArray *arrDataSource; 6 7 @end ViewController.m 1 #import &

UITableViewCell 单元格样式

UITableViewCell 单元格样式作用 1 typedef NS_ENUM(NSInteger, UITableViewCellStyle) { 2 UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x) 3 UITableViewCellStyleValue1, // Left ali

NPOI 生成Excel (单元格合并、设置单元格样式:字段,颜色、设置单元格为下拉框并限制输入值、设置单元格只能输入数字等)

NPIO源码地址:https://github.com/tonyqus/npoi NPIO使用参考:源码中的 NPOITest项目 下面代码包括: 1.包含多个Sheet的Excel 2.单元格合并 3.设置单元格样式:字段,颜色 4.设置单元格为下拉框并限制输入值 5.设置单元格只能输入数字 // // GET: /Excel/ public ActionResult Write() { var workbook = new HSSFWorkbook();//从流内容创建Workbook对象

用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法

今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Excel中格式的设置,以及单元格的合并等等.下面就介绍下,使用NPOI类库操作Excel的方法. 1.首先我们先在内存中生成一个Excel文件,代码如下:   HSSFWorkbook book = new HSSFWorkbook();        ISheet sheet = book.CreateSheet("Sheet1"); 2.然后在新创建的sheet里面,创建我们的行和列,代码如下

excel套用单元格样式进行美化

与表格样式的内容相似,单元格样式中也包含字体.边框.填充和数字格式等效果,直接使用能实现快速美化单元格的目的.(常见问题)Excel表格样式的套用与创建如何把内容粘贴到excel单元格时自动套用excel的单元格格式excel怎样取消套用表格格式excel如何自定义套用表格格式怎么新增并设置 [解决方法,教程视频资料如下]资料来源:http://edu.51cto.com/course/15224.html 完整资料:http://edu.51cto.com/lecturer/13162026.

一种适应10KV配网建设的智能通讯管理单元(带加密模块)分布式DTU综合测控通信单元

SDAF-8600智能通讯管理单元(带加密模块)分布式DTU综合测控通信单元装置概述SDAF-8600分布式公共单元是分布式DTU的组成部分,与DTU间隔单元配套使用.负责汇集各个DTU间隔单元遥信数据.遥测数据.保护事件.录波数据.运行状态.电能量数据等相关信息,并通过通信设备远传至主站.DTU公共单元获取主站下发的遥控命令,实现对每个DTU间隔单元进行遥控操作的功能.单元是智昊电气根据国家以及电网公司配电自动化相关技术标准和规范设计,符合环网柜一二次融合配电自动化终端技术规范要求.和DTU间

将两个或更多个单元格的文本合并到一个单元格

将两个或更多个单元格的文本合并到一个单元格 选择要放置合并后数据的单元格. 键入“=”,然后选择要合并的第一个单元格. 键入“&”,然后使用引号(中间有一个空格). 选择要合并的下一个单元格,然后按Enter. 示例公式可能是“=A2&" "&B2” 原文地址:https://www.cnblogs.com/lbsjs/p/11526309.html

EasyExcel使用及自定义设置单元格样式

固定模板方式,首先创建要Excel数据列模板:当然EasyExcel 中也可以动态自定义表头,其实都差不多一样 下面案例中,我采用一个固定模板方式,主要记录下,如何自定义单元格样式 这里是导出方法,主要是绑定样式,指定Excel文件生成的路径 public static String ExcelWrite(ExportParamDto excelData) { String fileName = getPath() + System.currentTimeMillis() + ".xlsx&qu