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

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

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

标签:

效果如下:

技术分享

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 "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)layoutUI;
  5 - (UIImageView *)imageViewForCell:(const UITableViewCell *)cell withFileName:(NSString *)fileName;
  6 - (UISwitch *)switchForCell:(const UITableViewCell *)cell;
  7 - (UISlider *)sliderForCell:(const UITableViewCell *)cell;
  8 @end
  9 
 10 @implementation ViewController
 11 #define kMovingX 100
 12 
 13 - (void)viewDidLoad {
 14     [super viewDidLoad];
 15     
 16     [self layoutUI];
 17 }
 18 
 19 - (void)didReceiveMemoryWarning {
 20     [super didReceiveMemoryWarning];
 21     // Dispose of any resources that can be recreated.
 22 }
 23 
 24 - (void)layoutUI {
 25     _arrSection = @[@"姓名", @"必杀技", @"强弱"];
 26     _arrDataSource = @[@[@"C罗"],
 27                        @[@"全能"],
 28                        @[@"速度", @"技术"]];
 29     
 30     self.navigationItem.title = @"在单元格中添加自定义控件";
 31 }
 32 
 33 - (UIImageView *)imageViewForCell:(const UITableViewCell *)cell withFileName:(NSString *)fileName {
 34     UIImage *img = [UIImage imageNamed:fileName];
 35     UIImageView *imgV = [[UIImageView alloc] initWithImage:img];
 36     CGPoint newPoint = cell.contentView.center;
 37     newPoint.x += kMovingX;
 38     imgV.center = newPoint;
 39     imgV.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
 40     return imgV;
 41 }
 42 
 43 - (UISwitch *)switchForCell:(const UITableViewCell *)cell {
 44     UISwitch *swt = [[UISwitch alloc] init];
 45     swt.on = YES;
 46     CGPoint newPoint = cell.contentView.center;
 47     newPoint.x += kMovingX;
 48     swt.center = newPoint;
 49     swt.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
 50     return swt;
 51 }
 52 
 53 - (UISlider *)sliderForCell:(const UITableViewCell *)cell {
 54     UISlider *sld = [[UISlider alloc] init];
 55     sld.value = sld.maximumValue / 2;
 56     sld.frame = CGRectMake(0, 0, cell.bounds.size.width / 2, cell.bounds.size.height);
 57     CGPoint newPoint = cell.contentView.center;
 58     newPoint.x += (kMovingX - 50);
 59     sld.center = newPoint;
 60     sld.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
 61     return sld;
 62 }
 63 
 64 #pragma mark - TableView
 65 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
 66     return _arrSection[section];
 67 }
 68 
 69 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 70     return _arrSection.count;
 71 }
 72 
 73 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 74     return [_arrDataSource[section] count];
 75 }
 76 
 77 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 78     static NSString *cellIdentifier = @"cellIdentifier";
 79     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 80     if (!cell) {
 81         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 82     }
 83     
 84     NSInteger section = indexPath.section;
 85     cell.textLabel.text = _arrDataSource[section][indexPath.row];
 86     //添加自定义控件
 87     UIView *view = nil;
 88     switch (section) {
 89         case 0:
 90             view = [self imageViewForCell:cell withFileName:@"Person"];
 91             break;
 92         case 1:
 93             view = [self switchForCell:cell];
 94             break;
 95         case 2:
 96             view = [self sliderForCell:cell];
 97             break;
 98     }
 99     [cell.contentView addSubview:view];
100     return cell;
101 }
102 
103 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
104     
105 }
106 
107 @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

 

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

标签:

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

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