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

效果如下:

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
时间: 2024-08-30 01:16:10

130在单元格中添加自定义控件的相关文章

127使用 TableView 自带的单元格样式实现好友列表,另外在单元格中添加辅助按钮

类似的做法如之前这篇随笔:114自定义 UITableViewCell 实现好友列表(扩展知识:如何使用xib创建自定义的表格视图单元格 KMTableViewCell) 相比之下:自定义 UITableViewCell 的内容灵活,可根据需求调整展示效果,应用场景更广:一般适用于TableView 自带的单元格样式无法实现的效果. 效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController :

easyui在table单元格中添加进度条

function XR_jd(alue, rowData, rowIndex){ var value; ...... var htmlstr = '<div class="easyui-progressbar progressbar" style="width: 100%; height: 20px;" value="' + value + '" text="' + value + '%">' + '<div

Swift - 可编辑表格样例(可直接编辑单元格中内容、移动删除单元格)

(本文代码已升级至Swift3) 本文演示如何制作一个可以编辑单元格内容的表格(UITableView). 1,效果图 (1)默认状态下,表格不可编辑,当点击单元格的时候会弹出提示框显示选中的内容.           (2)点击导航栏右侧编辑按钮,表格进入可以编辑状态 (3)这时我们可以删除表格项. (4)也可以拖动调整单元格的顺序. (5)然后就是本文的重点,在编辑状态下.直接点击单元格,即可在当前页面下直接编辑修改单元格中的内容. 2,单元格编辑功能讲解 (1)通过自定义 UITableV

134在单元格中自动排列指定的数据

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSMutableArray *mArrDataList; 5 @property (strong, nonatomic) NSMutableArray *mArrSortedCollation; 6 7 @end ViewCo

Swift - 异步加载各网站的favicon图标,并在单元格中显示

下面是一个简单的应用,表格视图的各个单元格自动异步加载各个网站的favicon图标,并显示出来. 主要是复习下如何自定义单元格,单元格中图片的异步加载,以及didSet的用法. 效果图如下: 操作步骤: (1)先创建单元格类 - FaviconTableViewCell.swift 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

Excel 2010单元格中设置表头

大家在做报表的时候,似乎都遇到过这样的问题,就是在一个单元格中如何设置表头: 其中包括一条斜杠的,两条斜杠的,N 条斜杠的,很多斜杠似乎没什么用.如图: 不知道C1能不能用到,这里只是举个例子. 首先单条斜杠的表头如何制作,其实非常简单,就是设置单元格属性中的边框,然后添加斜向斜杠即可.如图: 很简单,然后就可以输入数据,输入数据的时候大家要注意,首先单元格数据位置要设置为靠上,然后用空格和手动换行来控制(alt+enter).效果如图: 非常简单.下面介绍三条斜线的表头如何实现,三条斜线用边框

C#中怎么在EXCEL中的单元格中画斜线啊 ??

Code Snippet 做法: 1,先添加引用COM,找 Excel 2,using Excel = Microsoft.Office.Interop.Excel; 3, 代码 private Excel.Application m_objExcel = null;        private Excel.Workbooks m_objBooks = null;        private Excel._Workbook m_objBook = null;        private E

对表格单元格的添加删除修改

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

table表格单元格中的内容如何强制换行

table表格单元格中的内容如何强制换行:有时候表格单元格中的内容不会换行,那么这个就会严重影响到用户体验,下面就简单介绍一下如何实现单元格中的内容换行.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.51texiao.cn/" />