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; ...
随机推荐
- pandas 常用函数整理
pandas常用函数整理,作为个人笔记. 仅标记函数大概用途做索引用,具体使用方式请参照pandas官方技术文档. 约定 from pandas import Series, DataFrame im ...
- java基础篇---文件上传(commons-FileUpload组件)
上一篇讲解了smartupload组件上传,那么这一篇我们讲解commons-FileUpload组件上传 FileUpload是Apache组织(www.apache.org)提供的免费的上传组件, ...
- Spring框架中InitializingBean执行顺序
本文转自:https://www.cnblogs.com/yql1986/p/4084888.html package org.test.InitializingBean; 2 3 import or ...
- C语言 · C++中map的用法详解
转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义 (1) map<string, int> Map ...
- ubuntu linux下建立stm32开发环境: GCC安装以及工程Makefile建立
http://blog.csdn.net/embbnux/article/details/17616809
- windows已阻止此软件因为无法验证发行者怎么办
出现提示windows已阻止此软件因为无法验证发行者怎么解决?有的时候访问某些网站会出现类似的提示.导致不能正常运行某个插件,遇到这个问题一般是浏览器的安全级别设置太高了,没有允许脚本控件运行 设 ...
- hive常用参数配置设置
hive.exec.mode.local.auto 决定 Hive 是否应该自动地根据输入文件大小,在本地运行(在GateWay运行) true hive.exec.mode.local.auto.i ...
- 利用python的KMeans和PCA包实现聚类算法
题目: 通过给出的驾驶员行为数据(trip.csv),对驾驶员不同时段的驾驶类型进行聚类,聚成普通驾驶类型,激进类型和超冷静型3类 . 利用Python的scikit-learn包中的Kmeans算法 ...
- .net 定时启动任务
.net winform程序,设置每天的零时启动,执行完后自动关闭. 系统计划启动: 如果要定时启动某个程序,可以通过“任务计划”来安排. 如果是XP系统,在开始 --> 所有程序 --> ...
- svn cleanup失败
今天用SVN的时候出现被锁定的情况,既不能更新代码也不能提交. 解决方法如下: 1. 在被锁定的文件夹上点右键,找到并点击Clean up菜单项 2. 在Cleanup对话框中勾选“Break loc ...