类似的做法如之前这篇随笔: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>