码迷,mamicode.com
首页 > 其他好文 > 详细

132设置被选中单元格的背景颜色(扩展知识:设置被选中单元格的背景图片)

时间:2015-06-16 16:24:03      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UITableViewController
4 @property (strong, nonatomic) NSArray *arrDataSource;
5 @property (strong, nonatomic) NSArray *arrSelectionStyle;
6 
7 @end

ViewController.m

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 - (void)layoutUI;
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     
12     [self layoutUI];
13 }
14 
15 - (void)didReceiveMemoryWarning {
16     [super didReceiveMemoryWarning];
17     // Dispose of any resources that can be recreated.
18 }
19 
20 - (void)layoutUI {
21     _arrDataSource = @[@"UITableViewCellSelectionStyleNone",
22                        @"UITableViewCellSelectionStyleBlue(失效得用cell.selectedBackgroundView自定义)",
23                        @"UITableViewCellSelectionStyleGray",
24                        @"UITableViewCellSelectionStyleDefault(默认值;iOS7有效,本身定义就是灰色)",
25                        @"自定义被选中的单元格的背景颜色",
26                        @"自定义被选中的单元格的背景图片"];
27     
28     _arrSelectionStyle = @[[NSNumber numberWithInteger:UITableViewCellSelectionStyleNone],
29                            [NSNumber numberWithInteger:UITableViewCellSelectionStyleBlue],
30                            [NSNumber numberWithInteger:UITableViewCellSelectionStyleGray],
31                            [NSNumber numberWithInteger:UITableViewCellSelectionStyleDefault],
32                            [NSNumber numberWithInteger:4],
33                            [NSNumber numberWithInteger:5]];
34     
35     self.navigationItem.title = @"设置被选中单元格的背景颜色";
36 }
37 
38 #pragma mark - TableView
39 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
40     return @"selectionStyle列表";
41 }
42 
43 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
44     return 1;
45 }
46 
47 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
48     return _arrDataSource.count;
49 }
50 
51 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
52     static NSString *cellIdentifier = @"cellIdentifier";
53     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
54     if (!cell) {
55         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
56     }
57     
58     NSInteger row = indexPath.row;
59     cell.textLabel.text = _arrDataSource[row];
60     cell.textLabel.numberOfLines = 2;
61     
62     UIImage *img = nil;
63     //设置被选中单元格的背景颜色或背景图片
64     switch (row) {
65         case 0:
66         case 2:
67         case 3:
68             cell.selectionStyle = [_arrSelectionStyle[row] integerValue];
69             break;
70         case 1:
71         case 4:
72             cell.selectedBackgroundView = [[UIView alloc] init];
73             cell.selectedBackgroundView.backgroundColor = [UIColor blueColor]; //对应的有cell.backgroundColor
74             break;
75         case 5:
76             img = [UIImage imageNamed:@"Frame"];
77             img = [img stretchableImageWithLeftCapWidth:20 topCapHeight:20];
78             cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:img]; //对应的有cell.backgroundView
79             break;
80     }
81     return cell;
82     
83     /*
84      typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
85      UITableViewCellSelectionStyleNone,
86      UITableViewCellSelectionStyleBlue,
87      UITableViewCellSelectionStyleGray,
88      UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
89      };
90      */
91 }
92 
93 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
94     
95 }
96 
97 @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

 

132设置被选中单元格的背景颜色(扩展知识:设置被选中单元格的背景图片)

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4580958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!