127使用 TableView 自带的单元格样式实现好友列表,另外在单元格中添加辅助按钮
类似的做法如之前这篇随笔:114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)
相比之下:自定义 UITableViewCell 的内容灵活,可根据需求调整展示效果,应用场景更广;一般适用于TableView 自带的单元格样式无法实现的效果。
效果如下:





ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UITableViewController
@property (strong, nonatomic) NSMutableArray *mArrDataList;
@property (strong, nonatomic) NSMutableArray *mArrImageList;
@property (assign, nonatomic) UITableViewCellAccessoryType accessoryType; @end
ViewController.m
#import "ViewController.h" @interface ViewController ()
- (void)layoutUI;
- (void)loadData;
- (void)accesoryTypeDidChange:(UIBarButtonItem *)sender;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; [self layoutUI];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)layoutUI {
[self loadData]; self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"在单元格中添加辅助按钮";
UIBarButtonItem *barBtnAccesoryType = [[UIBarButtonItem alloc] initWithTitle:@"切换辅助按钮类型"
style:UIBarButtonItemStyleDone
target:self
action:@selector(accesoryTypeDidChange:)];
self.navigationItem.rightBarButtonItem = barBtnAccesoryType;
} - (void)loadData {
NSBundle *bundle = [NSBundle mainBundle];
NSURL *urlFriendsInfo = [bundle URLForResource:@"FriendsInfo" withExtension:@"plist"];
NSDictionary *dicFriendsInfo = [NSDictionary dictionaryWithContentsOfURL:urlFriendsInfo];
NSInteger len = [dicFriendsInfo count];
_mArrDataList = [[NSMutableArray alloc] initWithCapacity:len];
_mArrImageList = [[NSMutableArray alloc] initWithCapacity:len];
for (NSInteger i=; i<len; i++) {
NSString *strKey = [NSString stringWithFormat:@"%lu", (unsigned long)(i+)];
NSDictionary *dicData = [dicFriendsInfo objectForKey:strKey];
[_mArrDataList addObject:dicData]; UIImage *img = [UIImage imageNamed:strKey];
[_mArrImageList addObject:img];
} //设置用于单元格的辅助按钮类型的枚举变量
_accessoryType = UITableViewCellAccessoryNone;
} - (void)accesoryTypeDidChange:(UIBarButtonItem *)sender {
switch (_accessoryType) {
case UITableViewCellAccessoryNone:
case UITableViewCellAccessoryDisclosureIndicator:
case UITableViewCellAccessoryDetailDisclosureButton:
case UITableViewCellAccessoryCheckmark: {
_accessoryType++;
break;
}
case UITableViewCellAccessoryDetailButton: {
_accessoryType = UITableViewCellAccessoryNone;
break;
}
}
[self.tableView reloadData];
} #pragma mark - TableView
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"FriendsInfo列表";
} - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_mArrDataList count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
} NSDictionary *rowData = _mArrDataList[indexPath.row];
cell.textLabel.text = [rowData objectForKey:@"name"];
cell.detailTextLabel.text = [rowData objectForKey:@"desc"];
cell.imageView.image = _mArrImageList[indexPath.row]; //设置单元格的辅助按钮类型;默认值是UITableViewCellAccessoryNone
cell.accessoryType = _accessoryType;
return cell; /*
UITableViewCellStyleDefault:带imageView;只含水平居左的textLabel
UITableViewCellStyleValue1:带imageView;含水平居左的textLabel和水平居右的detailTextLabel(左右结构)
UITableViewCellStyleValue2:不带imageView;含水平居左的textLabel和水平居左的detailTextLabel(左右结构)
UITableViewCellStyleSubtitle:带imageView;含垂直居上的textLabel和垂直居下的detailTextLabel(上下结构)
*/ /*
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1, // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2, // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
}; // available in iPhone OS 3.0
*/
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0;
} /**
* 辅助按钮点击委托事件;只针对UITableViewCellAccessoryDetailDisclosureButton和UITableViewCellAccessoryDetailButton类型的有效
*
* @param tableView 表格视图
* @param indexPath 当前点击单元格的索引路径信息(关键包含信息:section分组和row行)
*/
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSMutableString *mStrMessage = [[NSMutableString alloc] initWithFormat:@"您选择了第%ld行的辅助按钮\n类型为:", (indexPath.row + )];
switch (_accessoryType) {
case UITableViewCellAccessoryNone: {
//Do nothing
break;
}
case UITableViewCellAccessoryDisclosureIndicator: { //披露指示器
//Do nothing
break;
}
case UITableViewCellAccessoryDetailDisclosureButton: { //详细按钮和披露指示器
[mStrMessage appendString:@"UITableViewCellAccessoryDetailDisclosureButton 详细按钮和披露指示器"];
break;
}
case UITableViewCellAccessoryCheckmark: { //复选标示
//Do nothing
break;
}
case UITableViewCellAccessoryDetailButton: { //详细按钮
[mStrMessage appendString:@"UITableViewCellAccessoryDetailButton 详细按钮"];
break;
}
} UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示信息"
message:mStrMessage
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"确定", nil];
[alertV show];
} @end
AppDelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController; @end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate ()
@end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] init];
_navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
_window.rootViewController = _navigationController;
//[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
[_window makeKeyAndVisible];
return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
} - (void)applicationDidEnterBackground:(UIApplication *)application {
} - (void)applicationWillEnterForeground:(UIApplication *)application {
} - (void)applicationDidBecomeActive:(UIApplication *)application {
} - (void)applicationWillTerminate:(UIApplication *)application {
} @end
FriendsInfo.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>小明</string>
<key>desc</key>
<string>干啥呢?</string>
<key>location</key>
<string>广州</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>痞子</string>
<key>desc</key>
<string>好好学习,天天向上!</string>
<key>location</key>
<string>广州</string>
</dict>
<key>3</key>
<dict>
<key>name</key>
<string>疯子</string>
<key>desc</key>
<string>倚楼听风雨,淡看江湖路。</string>
<key>location</key>
<string>广州</string>
</dict>
<key>4</key>
<dict>
<key>name</key>
<string>梦醒</string>
<key>desc</key>
<string>书到用时方恨少</string>
<key>location</key>
<string>广州</string>
</dict>
<key>5</key>
<dict>
<key>name</key>
<string>落落</string>
<key>desc</key>
<string>生日快乐!</string>
<key>location</key>
<string>广州</string>
</dict>
<key>6</key>
<dict>
<key>name</key>
<string>丫丫</string>
<key>desc</key>
<string>做个踏实的科研女</string>
<key>location</key>
<string>广州</string>
</dict>
<key>7</key>
<dict>
<key>name</key>
<string>乐天平</string>
<key>desc</key>
<string>在火车上</string>
<key>location</key>
<string>广州</string>
</dict>
<key>8</key>
<dict>
<key>name</key>
<string>北暮</string>
<key>desc</key>
<string>好久不见!</string>
<key>location</key>
<string>广州</string>
</dict>
<key>9</key>
<dict>
<key>name</key>
<string>苹果</string>
<key>desc</key>
<string>喜欢苹果,更喜欢青苹果!</string>
<key>location</key>
<string>广州</string>
</dict>
<key>10</key>
<dict>
<key>name</key>
<string>木头</string>
<key>desc</key>
<string>清心薄欲 静躁作学</string>
<key>location</key>
<string>广州</string>
</dict>
<key>11</key>
<dict>
<key>name</key>
<string>醉清风</string>
<key>desc</key>
<string>一醉解千愁</string>
<key>location</key>
<string>广州</string>
</dict>
<key>12</key>
<dict>
<key>name</key>
<string>浅の斯</string>
<key>desc</key>
<string>想剪短发……剪还是不剪(⊙o⊙)?</string>
<key>location</key>
<string>广州</string>
</dict>
<key>13</key>
<dict>
<key>name</key>
<string>虚伪</string>
<key>desc</key>
<string>讨厌虚伪</string>
<key>location</key>
<string>广州</string>
</dict>
<key>14</key>
<dict>
<key>name</key>
<string>阁楼</string>
<key>desc</key>
<string>窗外的风景。</string>
<key>location</key>
<string>广州</string>
</dict>
</dict>
</plist>
127使用 TableView 自带的单元格样式实现好友列表,另外在单元格中添加辅助按钮的更多相关文章
- 【POI xlsx】使用POI对xlsx的单元格样式进行设置 / 使用POI对xlsx的字体进行设置
涉及到的样式都在代码中有说明: package com.it.poiTest; import java.io.FileNotFoundException; import java.io.FileOut ...
- StringGrid 实例4 本例功能: 1、给每个单元格赋值 2、调整当前单元格位置:上下左右;
实例4 本例功能:1.给每个单元格赋值 2.调整当前单元格位置:上下左右: 运行效果图:
- UITableViewCell 单元格样式
UITableViewCell 单元格样式作用 typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefau ...
- poi的各种单元格样式以及一些常用的配置
之前我做过一个poi到处excel数据的博客,但是,后面使用起来发现,导出的数据单元格样式都不对. 很多没有居中对齐,很多单元格的格式不对,还有就是单元格的大小不对,导致数据显示异常,虽然功能可以使用 ...
- 给bootstrap table设置行列单元格样式
1.根据单元格或者行内其他单元格的内容,给该单元格设置一定的css样式 columns: [{ field: 'index', title: '序号', align:"center" ...
- POI设置excle单元格样式
Java利用POI生成Excel强制换行 使用POI创建一个简单的 myXls.xls 文件 常用的包为 org.apache.poi.hssf.usermodel.*; ...
- 用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法
本篇文章小编为大家介绍,用NPOI创建Excel.合并单元格.设置单元格样式.边框的方法.需要的朋友参考下 今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Ex ...
- 用TableView写带特效的cell
用TableView写带特效的cell 效果: 源码地址: https://github.com/YouXianMing/UI-Component-Collection 分析: 在UIScrollVi ...
- 创建excel,合并单元格,设置单元格样式
package com.huawei.excel; import java.io.File;import java.io.FileOutputStream;import java.util.Date; ...
随机推荐
- C#防止内存泄露的方法
一般程序员()都会这样认为:用C#这样的语言编程的一个好处就是无需再考虑内存的分配和释放.你只需创建对象,然后通过一种叫做垃圾收集的机制来处理这 些对象,也就是说:当它们不再被应用程序需要的时候来自动 ...
- idea中git git pull push需要反复输入密码
在使用idea开发的过程中,在终端terminal中git pull和git push时遇到一个问题,一个是 每次提交都需要输入用户名和密码,,从网上找了下解决方案,记录一下. 解决: 打开git终端 ...
- Mapreduce 进阶
场景描述 订单需要封装成为一个bean 传入reduce,然后实现排序取出top1,或者分组求和 首先要实现排序就要实现comparable接口 要实现分组top1,那么"相同的bean&q ...
- 未能加载文件或程序集”xxxx”或它的某一个依赖项,试图加载格式不正确的程序。
通常是因为应用程序编译的目标平台与引用的DLL类库目标平台不一致造成的,如应用程序目标编译为64位,而引用了32位的DLL. 在Visual Studio修改应用程序目标编译平台即可. 更多关于目标编 ...
- sql优化方法学习和总结
首先要问自己几个问题: 哪些类型的sql会散发出坏味道? sql优化的基本原理是什么,为什么有的sql快有的慢? sql优化和底层的存储引擎关系大么? 怎么看执行过程? 优化建议 1. 缓存查询,sq ...
- <[完整版]中国式价值投资>读书笔记
注重本金安全 股票价格的高级与股票的贵贱没有任何关系 同股同权的应该买便宜的 买未来有可能变得更大的优秀公司股票,只有他们的股价才有可能有持续向上的原动力 如果绝大多数投资者对股票高市盈率不是拒绝而是 ...
- WCF安全:通过 扩展实现用户名密码认证
在webSservice时代,可以通过SOAPHEADER的方式很容易将用户名.密码附加到SOAP header消息头上,用户客户端对调用客户端身份的验证.在WCF 时代,也可以通过Operation ...
- DRUPAL8模版命名规则
DRUPAL8模版命名规则 xiaopang 星期一, 04/08/2013 - 20:04 发布 我想了半个小时,想怎么清楚简单的为大家解释drupal8的模版命名规则,但是很显然,我失败了,但 ...
- Spring Cloud Config 配置中心
请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...
- vim for python
set encoding=utf8 set paste 编辑模式下的 复制粘贴 set expandtab 貌似是一个 tab 替换成4个空格, 而且当你使用 backspace(删除键), 会自 ...