UITableBarController

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#import "AppDelegate.h"
#import "RootViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "ViewController5.h"
#import "ViewController6.h"
@interface AppDelegate ()<UITabBarControllerDelegate>
@end
/**
系统代理的使用:
1.遵守协议
2.设置代理
3.实现代理方法
*/
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];

[self createTabBarController];

[self.window makeKeyAndVisible];
return YES;
}
//创建UITabBarController
-(void)createTabBarController{
// 初始化UITabBarController
UITabBarController *tabBarVc = [[UITabBarController alloc]init];

// 设置代理
tabBarVc.delegate = self;

self.window.rootViewController = tabBarVc;

// 设置tabBar的背景色
// tabBarVc.tabBar.barTintColor = [UIColor yellowColor];

ViewController1 *vc1 = [[ViewController1 alloc]init];
ViewController2 *vc2 = [[ViewController2 alloc]init];
ViewController3 *vc3 = [[ViewController3 alloc]init];
ViewController4 *vc4 = [[ViewController4 alloc]init];
ViewController5 *vc5 = [[ViewController5 alloc]init];
ViewController6 *vc6 = [[ViewController6 alloc]init];

// 系统item样式
// UITabBarSystemItemMore,
// UITabBarSystemItemFavorites,
// UITabBarSystemItemFeatured,
// UITabBarSystemItemTopRated,
// UITabBarSystemItemRecents,
// UITabBarSystemItemContacts,
// UITabBarSystemItemHistory,
// UITabBarSystemItemBookmarks,
// UITabBarSystemItemSearch,
// UITabBarSystemItemDownloads,
// UITabBarSystemItemMostRecent,
// UITabBarSystemItemMostViewed,


UITabBarItem *item1 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:11];
vc1.tabBarItem = item1;

UITabBarItem *item2 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:12];
vc2.tabBarItem = item2;

UITabBarItem *item3 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:13];
vc3.tabBarItem = item3;

UITabBarItem *item4 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:14];
vc4.tabBarItem = item4;

UITabBarItem *item5 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:15];
vc5.tabBarItem = item5;

UITabBarItem *item6 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:16];
vc6.tabBarItem = item6;

// 镂空处颜色
tabBarVc.tabBar.tintColor = [UIColor redColor];

// 透明度
tabBarVc.tabBar.translucent = NO;

// 最多只显示5个,多出来的会自动生成一个More,它是一个导航控制器
tabBarVc.viewControllers = @[vc1,vc2,vc3,vc4,vc5,vc6];

// 设置选中哪一个控制器
// tabBarVc.selectedIndex = 3;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUInteger index = [defaults integerForKey:@"index"];
NSLog(@"%ld",index);
// 设置选中第index个控制器
tabBarVc.selectedIndex = index;

// 排序
[self order];
}
#pragma mark -
#pragma mark UITabBarController delegate
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSLog(@"将要选中视图控制器");
if (viewController.tabBarItem.tag == 13) {
return NO;
}
// YES:可以选中
// NO:不能选中
return YES;
}
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(@"已经选中指定的视图控制器");

NSArray *array = tabBarController.viewControllers;
NSLog(@"array = %@",array);
NSUInteger index = [array indexOfObject:viewController];

// 初始化NSUserDefaults (单例)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// 设置index
[defaults setInteger:index forKey:@"index"];
// 同步
[defaults synchronize];
}
-(void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers{
NSLog(@"willBeginCustomizingViewControllers");
}
-(void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
NSLog(@"willEndCustomizingViewControllers");
}
-(void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
NSLog(@"didEndCustomizingViewControllers");

NSMutableArray *array = [NSMutableArray array];
// changed用来判断是否交换
if (changed == YES) {
for (UIViewController *vc in viewControllers) {
[array addObject:vc.title];
}
}
NSLog(@"%@",array);

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:array forKey:@"order"];
[defaults synchronize];
}

-(void)order{

// 获取UITabBarController
UITabBarController * tbc = (UITabBarController*)self.window.rootViewController;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// 取得之前排序的数组
NSMutableArray *titleArray = [defaults objectForKey:@"order"];

// 如果排序的数组是nil,返回
if (titleArray == nil) {
return;
}

NSMutableArray *orderArray = [NSMutableArray array];
for (NSString *titler in titleArray) {
for (UIViewController *vc in tbc.viewControllers) {
if ([titler isEqualToString:vc.title]) {
[orderArray addObject:vc];
break;
}
}
}
tbc.viewControllers = orderArray;
}

@end


#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSStringFromClass([self class]);
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];

#define Random arc4random()%256/255.0

self.view.backgroundColor = [UIColor colorWithRed:Random green:Random blue:Random alpha:0.9f];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
NSString *name = NSStringFromClass([self class]);
label.text = name;

[self.view addSubview:label];
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#import "AppDelegate.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "ViewController4_1.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];

[self createUITabBarController];
[self customTabBar];


[self.window makeKeyAndVisible];

return YES;
}
//创建UITabBarController
-(void)createUITabBarController{
// 初始化UITabBarController
UITabBarController *tabBarVc = [[UITabBarController alloc]init];
self.window.rootViewController = tabBarVc;


ViewController1 *vc1 = [[ViewController1 alloc]init];
ViewController2 *vc2 = [[ViewController2 alloc]init];
ViewController3 *vc3 = [[ViewController3 alloc]init];

ViewController4 *vc4 = [[ViewController4 alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc4];

// 把导航控制器放到分栏控制器中
// tabBarVc.viewControllers = @[vc1,vc2,vc3,nav];

// 初始化UITabBarItem
// Title:名字
// image:图片
// tag:标识
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:@"微信" image:[UIImage imageNamed:@"tabbar_mainframe" ] tag:11];
vc1.tabBarItem = item1;

UIImage *image2 = [UIImage imageNamed:@"tabbar_contactsHL"];
// 对图片进行处理(显示原有尺寸)
image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//
//Title:名称
//image:正常状态image
//selectedImage:选中状态image(需要对图片进行处理,否则会不显示选中图片)
UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"通讯录" image:[UIImage imageNamed:@"tabbar_contacts"] selectedImage:image2];
vc2.tabBarItem = item2;

UIImage *image3 = [UIImage imageNamed:@"tabbar_discoverHL"];
image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item3 = [[UITabBarItem alloc]initWithTitle:@"朋友圈" image:[UIImage imageNamed:@"tabbar_discover"] selectedImage:image3];
vc3.tabBarItem = item3;

item3.badgeValue = @"99+";


UIImage *image4 = [UIImage imageNamed:@"tabbar_meHL"];
image4 = [image4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UITabBarItem *item4 = [[UITabBarItem alloc]initWithTitle:@"我" image:[UIImage imageNamed:@"tabbar_me"] selectedImage:image4];
vc4.tabBarItem = item4;

tabBarVc.viewControllers = @[vc1,vc2,vc3,nav];
}
//设置TabBar
-(void)customTabBar{
// 获取UITabBarController
UITabBarController *tabBarVc = (UITabBarController*)self.window.rootViewController;

// 获取UITabBar
UITabBar *tabBar = tabBarVc.tabBar;

// 设置tabBar样式
tabBar.barStyle = UIBarStyleDefault;
// UIBarStyleDefault
// UIBarStyleBlack

// 设置透明度
tabBar.translucent = YES;

// 设置镂空颜色(字体颜色)
[tabBar setTintColor:[UIColor colorWithRed:0/255.0 green:176/255.0 blue:15/255.0 alpha:1.0]];

// 设置tabBar背景色
[tabBar setBarTintColor:[UIColor redColor]];

// 设置背景图片(当设置背景图片时,会覆盖背景色)
[tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_bg"]];

}

@end


#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSStringFromClass([self class]);
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];

#define Random arc4random()%256/255.0

self.view.backgroundColor = [UIColor colorWithRed:Random green:Random blue:Random alpha:0.9f];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

NSString *name = NSStringFromClass([self class]);
label.text = name;

[self.view addSubview:label];
}
@end

#import "ViewController4.h"
#import "ViewController4_1.h"
@interface ViewController4 ()

@end

@implementation ViewController4

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{
ViewController4_1 *vc = [[ViewController4_1 alloc]init];
[self.navigationController pushViewController:vc animated:YES];

NSLog(@"%f",self.tabBarController.tabBar.frame.size.height);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



@end

UIWebView

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 初始化
UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
// 设置背景色
webView.backgroundColor = [UIColor redColor];

// 设置代理
webView.delegate = self;


// 添加到视图
[self.view addSubview:webView];

// 网址
NSString *path = @"http://www.baidu.com";
// 封装url,统一资源定位符,将字符串封装成可以识别的网址
NSURL *url = [NSURL URLWithString:path];
// 请求类
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 加载请求
[webView loadRequest:request];
}

#pragma mark - delegate
//即将加载
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
// YES:加载网页信息
// NO:不回网页信息
NSLog(@"即将加载");
return YES;
}
//已经开始加载
-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"已经开始加载");
}
//加载成功
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"加载成功");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"加载失败");
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
}
-(void)createUITableView{
// 初始化并设置样式和frame
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 设置背景色
_tableView.backgroundColor = [UIColor orangeColor];

// 设置代理
_tableView.dataSource = self;
_tableView.delegate = self;

[self.view addSubview:_tableView];

// 设置tableView的头视图
UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
headerImageView.image = [UIImage imageNamed:@"1.jpg"];
_tableView.tableHeaderView = headerImageView;
// 设置tableView的脚视图
UIImageView *footerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
footerImageView.image = [UIImage imageNamed:@"2.jpg"];
_tableView.tableFooterView = footerImageView;
}
#pragma mark -
#pragma mark dataSource
//返回多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//返回每组有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 10;
}
return 15;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// 1.标识
static NSString *identifier = @"cellID";

// 2.从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果从复用池中取不到cell,就创建新的
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

cell.textLabel.text = [NSString stringWithFormat:@"第%ld组-第%ld行",indexPath.section,indexPath.row];

return cell;
}
#pragma mark -
#pragma mark delegate
////设置头标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组头标题";
}
return @"第二组头标题";
}
//设置头标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 默认22
return 40;
}
//设置分组脚标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"第一组脚标题";
}
return @"第二组脚标题";
}
//设置脚标题的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
//返回自定义分组的头标和脚标,会把前面设置的标题覆盖
//必须设置头标题或头标题高度,否则本方法不生效
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
[button setTitle:@"分组标题" forState:UIControlStateNormal];
button.backgroundColor = [UIColor purpleColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void)onClick{
NSLog(@"点点点点点");
}
//必须设置脚标题或脚标题高度,否则本方法不生效
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
imageView.image = [UIImage imageNamed:@"6.jpg"];
return imageView;
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
// 表格视图
UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
// [_tableView performSelector:@selector(reloadData) withObject:nil afterDelay:5];
}
//创建UITableView
-(void)createUITableView{

// 初始化UITableView
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// UITableViewStylePlain, 平铺样式
// UITableViewStyleGrouped 分组样式

// 背景色
_tableView.backgroundColor = [UIColor orangeColor];

// 设置数据源代理
_tableView.dataSource = self;
// 设置代理
_tableView.delegate = self;

[self.view addSubview:_tableView];
}
#pragma mark -
#pragma mark dataSource
//返回多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@"numberOfSectionsInTableView");
return 2;
}
//每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"numberOfRowsInSection");
if (section == 0) {
return 10;
}
return 20;

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"cellForRowAtIndexPath");
// indexPath
// 表示当cell在表格视图中的位置
// indexPath.section 代表的是cell所在的分组
// indexPath.row 代表的是cell所在分组的行的位置


// 1.表格的标识
static NSString *identifier = @"cellID";

// 2.按照标识从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果在复用池中找不以Cell,就创建新的Cell
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// UITableViewCellStyleDefault,
// UITableViewCellStyleValue1,
// UITableViewCellStyleValue2,
// UITableViewCellStyleSubtitle
}
// 设置展示标题
cell.textLabel.text = [NSString stringWithFormat:@"第%ld组",indexPath.section];
// 设置展示详细标题
cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
// 设置展示图片
cell.imageView.image = [UIImage imageNamed:@"1"];

// 附件按钮类型
cell.accessoryType = UITableViewCellAccessoryDetailButton;

// UITableViewCellAccessoryNone, 无样式
// UITableViewCellAccessoryDisclosureIndicator箭头
//UITableViewCellAccessoryDetailDisclosureButton 箭头+信息按钮
// UITableViewCellAccessoryCheckmark,对勾
// UITableViewCellAccessoryDetailButton 信息按钮

//=============================================
// UIView *accessView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
// accessView.backgroundColor = [UIColor blackColor];
//// 设置自定义的附件视图
// cell.accessoryView = accessView;
//=============================================


// 选中样式
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
// UITableViewCellSelectionStyleNone,
// UITableViewCellSelectionStyleBlue,
// UITableViewCellSelectionStyleGray,
// UITableViewCellSelectionStyleDefault

// 设置选中背景View
UIView *BackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
BackgroundView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = BackgroundView;

// UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
// imageView.image = [UIImage imageNamed:@"1"];
// cell.selectedBackgroundView = imageView;

return cell;
}

#pragma mark -
#pragma mark delegate
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击了附件按钮");
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 默认行高 44
return 80;
}
//设置分组的头标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组组标题";
}
return @"第二组组标题";
}
//设置脚标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"第一组脚标";
}
return @"第二组脚标";
}
//组头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 默认高度是22
return 40;
}
//组尾的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
// 默认高度是22
return 40;
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
NSMutableArray *_dataArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
[self createDataArray];

self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//编辑按钮触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
NSLog(@"%d",editing);
// 重新调用父类的方法,处于Done字样时,editing=YES,处于Edit样式时,editing = NO.
[super setEditing:editing animated:YES];

// 设置tableView的可编辑性
[_tableView setEditing:editing animated:YES];
}
-(void)createUITableView{
// 初始化UITableView 设置样式和frame
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 设置背景色
_tableView.backgroundColor = [UIColor orangeColor];
// 设置代理
_tableView.dataSource = self;
_tableView.delegate = self;
// 添加到视图
[self.view addSubview:_tableView];
}
-(void)createDataArray{

_dataArray = [NSMutableArray array];

// 数据源中要添加两组数据:删除的、增加的
NSMutableArray *deleteArray = [NSMutableArray array];
for (int i = 0 ; i < 10; i ++) {
NSString *deleteStr = [NSString stringWithFormat:@"被删除的第%d行",i];
[deleteArray addObject:deleteStr];
}
NSMutableArray *insertArray = [NSMutableArray array];
for (int i = 0 ; i < 10; i ++) {
NSString *insertStr = [NSString stringWithFormat:@"增加的第%d行",i];
[insertArray addObject:insertStr];
}

[_dataArray addObject:deleteArray];
[_dataArray addObject:insertArray];
NSLog(@"%@",_dataArray);
}
#pragma mark -
#pragma mark dataSource
//返回有多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataArray.count;
}
//每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSMutableArray *array = [_dataArray objectAtIndex:section];

return array.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.定义标识
static NSString *identifier = @"cellID";
// 2.从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果取不到cell,就创建新的cell
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

// cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];

// 赋值
cell.textLabel.text = [[_dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];

return cell;
}
#pragma mark -
#pragma mark delegate
//选中了哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"选中了哪一行");
NSLog(@"第%ld组-第%ld行",indexPath.section,indexPath.row);
}
//反选
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"didDeselectRowAtIndexPath");
NSLog(@"----第%ld组-第%ld行",indexPath.section,indexPath.row);
}
//设置tableView 的编辑状态
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

// UITableViewCellEditingStyleNone,无效果
// UITableViewCellEditingStyleDelete,删除
// UITableViewCellEditingStyleInsert 增加

if (indexPath.section == 0) {
return UITableViewCellEditingStyleDelete;
}else{
return UITableViewCellEditingStyleInsert;
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {
// 删除当前对应行的数据源
[[_dataArray objectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row];

// [tableView reloadData];
// 刷新表
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}else{
NSString *insertStr = @"我是新来的";
// 数据源添加新数据
[[_dataArray objectAtIndex:indexPath.section] insertObject:insertStr atIndex:indexPath.row];
// 刷表
// [tableView reloadData];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
//是否允许某一行被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.row % 2 == 0) {
// return YES;
// }
// return NO;
return YES;
}
//移动要实现的方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
// sourceIndexPath 将要移动的cell的位置信息
// destinationIndexPath 目的位置信息

// 根据sourceIndexPath.section来获得第section组的数据
NSMutableArray *sourceArray = _dataArray[sourceIndexPath.section];
// 取出要移动cell所在行的数据
NSString *sourceStr = sourceArray[sourceIndexPath.row];
// 删除要移动cell所在行的数据
[sourceArray removeObjectAtIndex:sourceIndexPath.row];

// 根据destinationIndexPath.section来获取要插入的数组
NSMutableArray *destinationArray = _dataArray[destinationIndexPath.section];

[destinationArray insertObject:sourceStr atIndex:destinationIndexPath.row];
}
//能否移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return NO;
}
return YES;
}
@end

UISwitch

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
40
41
42
43

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 初始化并设置frame
UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
// 最小值
stepper.minimumValue = 0;

// 最大值
stepper.maximumValue = 10;
// 设置当前值
stepper.value = 4;
// 设置每次+2或-2,默认是1
stepper.stepValue = 2;

// 设置颜色
[stepper setTintColor:[UIColor orangeColor]];
// 添加事件
[stepper addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

// 处理事件是否一直回调,默认是YES
stepper.continuous = YES;

// 设置能否重复操作,默认是YES
stepper.autorepeat = YES;

// 设置是否能循环,默认是NO
stepper.wraps = YES;

[self.view addSubview:stepper];
}
-(void)valueChange:(UIStepper*)stepper{
NSLog(@"%lf",stepper.value);
}
@end

UIView传统动画方式

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

// [self createUIView];

[self creatBlockAnimation];
}
#pragma mark -
#pragma mark UIView的Block动画
-(void)creatBlockAnimation{
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
myView.backgroundColor = [UIColor lightGrayColor];
myView.tag = 10;
[self.view addSubview:myView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(200, 50, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"开始" forState:UIControlStateNormal];

[button addTarget:self action:@selector(blockAnimation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)blockAnimation{
UIView *myView = [self.view viewWithTag:10];
// Duration:动画持续时间
// block:你想要的操作
// [UIView animateWithDuration:1 animations:^{
// myView.frame = CGRectMake(50, 200, 50, 50);
// }];


//Duration:动画持续时间
//animations:你想要的操作
//completion:动画结束后的操作
[UIView animateWithDuration:1 animations:^{
myView.frame = CGRectMake(50, 200, 50, 50);
} completion:^(BOOL finished) {
NSLog(@"动画结束");
}];







}
#pragma mark -
#pragma mark 传统的UIView动画
-(void)createUIView{
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
myView.backgroundColor = [UIColor yellowColor];
myView.tag = 10;
[self.view addSubview:myView];


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(200, 50, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"开始" forState:UIControlStateNormal];

[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{
UIView *myView = [self.view viewWithTag:10];

// 开始动画,第一个参数是动画名称,第二个传nil
[UIView beginAnimations:@"animation" context:nil];
// 设置动画代理
[UIView setAnimationDelegate:self];
// 设置动画延迟时间
[UIView setAnimationDelay:1];
// 设置动画持续时间
[UIView setAnimationDuration:2];

//设置动画的线性表现
/*
UIViewAnimationCurveEaseInOut 在开头和结尾都减速处理
UIViewAnimationCurveEaseIn 在开头做减速处理
UIViewAnimationCurveEaseOut 在结尾做减速处理
UIViewAnimationCurveLinear 这个应该是均分的处理
*/
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// 设置动画的重复次数
[UIView setAnimationRepeatCount:2];

[UIView setAnimationRepeatAutoreverses:YES];
// 动画将要开始时调用的方法
[UIView setAnimationWillStartSelector:@selector(animationWillStart)];
// 动画已经结束时调用的方法
[UIView setAnimationDidStopSelector:@selector(animationDidStop)];

myView.frame = CGRectMake(50, 200, 100, 100);

// 提交动画
[UIView commitAnimations];
}
-(void)animationWillStart{
NSLog(@"动画将要开始");
}
-(void)animationDidStop{
NSLog(@"动画已经结束");
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
}
-(void)createUITableView{
// 初始化并设置样式和frame
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 设置背景色
_tableView.backgroundColor = [UIColor orangeColor];

// 设置代理
_tableView.dataSource = self;
_tableView.delegate = self;

[self.view addSubview:_tableView];

// 设置tableView的头视图
UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
headerImageView.image = [UIImage imageNamed:@"1.jpg"];
_tableView.tableHeaderView = headerImageView;
// 设置tableView的脚视图
UIImageView *footerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
footerImageView.image = [UIImage imageNamed:@"2.jpg"];
_tableView.tableFooterView = footerImageView;
}
#pragma mark -
#pragma mark dataSource
//返回多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//返回每组有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 10;
}
return 15;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// 1.标识
static NSString *identifier = @"cellID";

// 2.从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果从复用池中取不到cell,就创建新的
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

cell.textLabel.text = [NSString stringWithFormat:@"第%ld组-第%ld行",indexPath.section,indexPath.row];

return cell;
}
#pragma mark -
#pragma mark delegate
////设置头标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组头标题";
}
return @"第二组头标题";
}
//设置头标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 默认22
return 40;
}
//设置分组脚标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"第一组脚标题";
}
return @"第二组脚标题";
}
//设置脚标题的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
//返回自定义分组的头标和脚标,会把前面设置的标题覆盖
//必须设置头标题或头标题高度,否则本方法不生效
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
[button setTitle:@"分组标题" forState:UIControlStateNormal];
button.backgroundColor = [UIColor purpleColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void)onClick{
NSLog(@"点点点点点");
}
//必须设置脚标题或脚标题高度,否则本方法不生效
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
imageView.image = [UIImage imageNamed:@"6.jpg"];
return imageView;
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
// 表格视图
UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
// [_tableView performSelector:@selector(reloadData) withObject:nil afterDelay:5];
}
//创建UITableView
-(void)createUITableView{

// 初始化UITableView
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// UITableViewStylePlain, 平铺样式
// UITableViewStyleGrouped 分组样式

// 背景色
_tableView.backgroundColor = [UIColor orangeColor];

// 设置数据源代理
_tableView.dataSource = self;
// 设置代理
_tableView.delegate = self;

[self.view addSubview:_tableView];
}
#pragma mark -
#pragma mark dataSource
//返回多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@"numberOfSectionsInTableView");
return 2;
}
//每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"numberOfRowsInSection");
if (section == 0) {
return 10;
}
return 20;

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"cellForRowAtIndexPath");
// indexPath
// 表示当cell在表格视图中的位置
// indexPath.section 代表的是cell所在的分组
// indexPath.row 代表的是cell所在分组的行的位置


// 1.表格的标识
static NSString *identifier = @"cellID";

// 2.按照标识从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果在复用池中找不以Cell,就创建新的Cell
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// UITableViewCellStyleDefault,
// UITableViewCellStyleValue1,
// UITableViewCellStyleValue2,
// UITableViewCellStyleSubtitle
}
// 设置展示标题
cell.textLabel.text = [NSString stringWithFormat:@"第%ld组",indexPath.section];
// 设置展示详细标题
cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
// 设置展示图片
cell.imageView.image = [UIImage imageNamed:@"1"];

// 附件按钮类型
cell.accessoryType = UITableViewCellAccessoryDetailButton;

// UITableViewCellAccessoryNone, 无样式
// UITableViewCellAccessoryDisclosureIndicator箭头
//UITableViewCellAccessoryDetailDisclosureButton 箭头+信息按钮
// UITableViewCellAccessoryCheckmark,对勾
// UITableViewCellAccessoryDetailButton 信息按钮

//=============================================
// UIView *accessView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
// accessView.backgroundColor = [UIColor blackColor];
//// 设置自定义的附件视图
// cell.accessoryView = accessView;
//=============================================


// 选中样式
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
// UITableViewCellSelectionStyleNone,
// UITableViewCellSelectionStyleBlue,
// UITableViewCellSelectionStyleGray,
// UITableViewCellSelectionStyleDefault

// 设置选中背景View
UIView *BackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
BackgroundView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = BackgroundView;

// UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
// imageView.image = [UIImage imageNamed:@"1"];
// cell.selectedBackgroundView = imageView;

return cell;
}

#pragma mark -
#pragma mark delegate
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击了附件按钮");
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 默认行高 44
return 80;
}
//设置分组的头标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组组标题";
}
return @"第二组组标题";
}
//设置脚标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"第一组脚标";
}
return @"第二组脚标";
}
//组头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 默认高度是22
return 40;
}
//组尾的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
// 默认高度是22
return 40;
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
NSMutableArray *_dataArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
[self createDataArray];

self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//编辑按钮触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
NSLog(@"%d",editing);
// 重新调用父类的方法,处于Done字样时,editing=YES,处于Edit样式时,editing = NO.
[super setEditing:editing animated:YES];

// 设置tableView的可编辑性
[_tableView setEditing:editing animated:YES];
}
-(void)createUITableView{
// 初始化UITableView 设置样式和frame
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 设置背景色
_tableView.backgroundColor = [UIColor orangeColor];
// 设置代理
_tableView.dataSource = self;
_tableView.delegate = self;
// 添加到视图
[self.view addSubview:_tableView];
}
-(void)createDataArray{

_dataArray = [NSMutableArray array];

// 数据源中要添加两组数据:删除的、增加的
NSMutableArray *deleteArray = [NSMutableArray array];
for (int i = 0 ; i < 10; i ++) {
NSString *deleteStr = [NSString stringWithFormat:@"被删除的第%d行",i];
[deleteArray addObject:deleteStr];
}
NSMutableArray *insertArray = [NSMutableArray array];
for (int i = 0 ; i < 10; i ++) {
NSString *insertStr = [NSString stringWithFormat:@"增加的第%d行",i];
[insertArray addObject:insertStr];
}

[_dataArray addObject:deleteArray];
[_dataArray addObject:insertArray];
NSLog(@"%@",_dataArray);
}
#pragma mark -
#pragma mark dataSource
//返回有多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataArray.count;
}
//每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSMutableArray *array = [_dataArray objectAtIndex:section];

return array.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.定义标识
static NSString *identifier = @"cellID";
// 2.从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果取不到cell,就创建新的cell
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

// cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];

// 赋值
cell.textLabel.text = [[_dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];

return cell;
}
#pragma mark -
#pragma mark delegate
//选中了哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"选中了哪一行");
NSLog(@"第%ld组-第%ld行",indexPath.section,indexPath.row);
}
//反选
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"didDeselectRowAtIndexPath");
NSLog(@"----第%ld组-第%ld行",indexPath.section,indexPath.row);
}
//设置tableView 的编辑状态
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

// UITableViewCellEditingStyleNone,无效果
// UITableViewCellEditingStyleDelete,删除
// UITableViewCellEditingStyleInsert 增加

if (indexPath.section == 0) {
return UITableViewCellEditingStyleDelete;
}else{
return UITableViewCellEditingStyleInsert;
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {
// 删除当前对应行的数据源
[[_dataArray objectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row];

// [tableView reloadData];
// 刷新表
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}else{
NSString *insertStr = @"我是新来的";
// 数据源添加新数据
[[_dataArray objectAtIndex:indexPath.section] insertObject:insertStr atIndex:indexPath.row];
// 刷表
// [tableView reloadData];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
//是否允许某一行被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.row % 2 == 0) {
// return YES;
// }
// return NO;
return YES;
}
//移动要实现的方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
// sourceIndexPath 将要移动的cell的位置信息
// destinationIndexPath 目的位置信息

// 根据sourceIndexPath.section来获得第section组的数据
NSMutableArray *sourceArray = _dataArray[sourceIndexPath.section];
// 取出要移动cell所在行的数据
NSString *sourceStr = sourceArray[sourceIndexPath.row];
// 删除要移动cell所在行的数据
[sourceArray removeObjectAtIndex:sourceIndexPath.row];

// 根据destinationIndexPath.section来获取要插入的数组
NSMutableArray *destinationArray = _dataArray[destinationIndexPath.section];

[destinationArray insertObject:sourceStr atIndex:destinationIndexPath.row];
}
//能否移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return NO;
}
return YES;
}
@end

UITableView

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//遵守协议
{
// 表格视图
UITableView *_tableView;


}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self createUITableView];
}
//创建UITableView
-(void)createUITableView{


// UITableViewStylePlain,
// UITableViewStyleGrouped
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

// dataSource处理数据源相关
_tableView.dataSource = self;
// delegate处理移动、点击等
_tableView.delegate = self;

// 设置背景色
_tableView.backgroundColor = [UIColor orangeColor];

// 设置分隔线颜色
_tableView.separatorColor = [UIColor blueColor];

[self.view addSubview:_tableView];

}
#pragma mark -
#pragma mark dataSource delegate
//返回有多少个分组
//如果不实现该方法,就返回一组(默认是1)
//这个不是必须要实现的
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

//每组有多少行
//必须实现
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
//表格视图的信息
//必须实现
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// 1.表格的标识
static NSString *identifier = @"cellID";
// 2.按照标识在tableView的复用池中查找Cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
/*
复用池的作用:
当UITableViewCell滑出屏幕时,不会被销毁,而是放到tableView的复用池中。
当新的Cell要出现在屏幕上时,先在复用池中查看有没有相同标识的Cell,如果有,就直接刷新数据使用。如果没有,就创建新的

假如一个界面上有多个UITableView,每个tableView有自己的复用池
*/
// 3.如果复用池中找不到Cell,就自己创建
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

static int a = 0;
NSLog(@"%d",a++);
}

// 给cell赋值
cell.textLabel.text = @"cell";

// 4.返回cell
return cell;
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
}
-(void)createUITableView{
// 初始化并设置样式和frame
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 设置背景色
_tableView.backgroundColor = [UIColor orangeColor];

// 设置代理
_tableView.dataSource = self;
_tableView.delegate = self;

[self.view addSubview:_tableView];

// 设置tableView的头视图
UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
headerImageView.image = [UIImage imageNamed:@"1.jpg"];
_tableView.tableHeaderView = headerImageView;
// 设置tableView的脚视图
UIImageView *footerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
footerImageView.image = [UIImage imageNamed:@"2.jpg"];
_tableView.tableFooterView = footerImageView;
}
#pragma mark -
#pragma mark dataSource
//返回多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
//返回每组有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 10;
}
return 15;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// 1.标识
static NSString *identifier = @"cellID";

// 2.从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果从复用池中取不到cell,就创建新的
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

cell.textLabel.text = [NSString stringWithFormat:@"第%ld组-第%ld行",indexPath.section,indexPath.row];

return cell;
}
#pragma mark -
#pragma mark delegate
////设置头标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组头标题";
}
return @"第二组头标题";
}
//设置头标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 默认22
return 40;
}
//设置分组脚标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"第一组脚标题";
}
return @"第二组脚标题";
}
//设置脚标题的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
//返回自定义分组的头标和脚标,会把前面设置的标题覆盖
//必须设置头标题或头标题高度,否则本方法不生效
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
[button setTitle:@"分组标题" forState:UIControlStateNormal];
button.backgroundColor = [UIColor purpleColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void)onClick{
NSLog(@"点点点点点");
}
//必须设置脚标题或脚标题高度,否则本方法不生效
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
imageView.image = [UIImage imageNamed:@"6.jpg"];
return imageView;
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
// 表格视图
UITableView *_tableView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
// [_tableView performSelector:@selector(reloadData) withObject:nil afterDelay:5];
}
//创建UITableView
-(void)createUITableView{

// 初始化UITableView
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// UITableViewStylePlain, 平铺样式
// UITableViewStyleGrouped 分组样式

// 背景色
_tableView.backgroundColor = [UIColor orangeColor];

// 设置数据源代理
_tableView.dataSource = self;
// 设置代理
_tableView.delegate = self;

[self.view addSubview:_tableView];
}
#pragma mark -
#pragma mark dataSource
//返回多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@"numberOfSectionsInTableView");
return 2;
}
//每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"numberOfRowsInSection");
if (section == 0) {
return 10;
}
return 20;

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"cellForRowAtIndexPath");
// indexPath
// 表示当cell在表格视图中的位置
// indexPath.section 代表的是cell所在的分组
// indexPath.row 代表的是cell所在分组的行的位置


// 1.表格的标识
static NSString *identifier = @"cellID";

// 2.按照标识从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果在复用池中找不以Cell,就创建新的Cell
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// UITableViewCellStyleDefault,
// UITableViewCellStyleValue1,
// UITableViewCellStyleValue2,
// UITableViewCellStyleSubtitle
}
// 设置展示标题
cell.textLabel.text = [NSString stringWithFormat:@"第%ld组",indexPath.section];
// 设置展示详细标题
cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
// 设置展示图片
cell.imageView.image = [UIImage imageNamed:@"1"];

// 附件按钮类型
cell.accessoryType = UITableViewCellAccessoryDetailButton;

// UITableViewCellAccessoryNone, 无样式
// UITableViewCellAccessoryDisclosureIndicator箭头
//UITableViewCellAccessoryDetailDisclosureButton 箭头+信息按钮
// UITableViewCellAccessoryCheckmark,对勾
// UITableViewCellAccessoryDetailButton 信息按钮

//=============================================
// UIView *accessView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
// accessView.backgroundColor = [UIColor blackColor];
//// 设置自定义的附件视图
// cell.accessoryView = accessView;
//=============================================


// 选中样式
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
// UITableViewCellSelectionStyleNone,
// UITableViewCellSelectionStyleBlue,
// UITableViewCellSelectionStyleGray,
// UITableViewCellSelectionStyleDefault

// 设置选中背景View
UIView *BackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
BackgroundView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = BackgroundView;

// UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
// imageView.image = [UIImage imageNamed:@"1"];
// cell.selectedBackgroundView = imageView;

return cell;
}

#pragma mark -
#pragma mark delegate
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击了附件按钮");
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 默认行高 44
return 80;
}
//设置分组的头标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组组标题";
}
return @"第二组组标题";
}
//设置脚标题
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"第一组脚标";
}
return @"第二组脚标";
}
//组头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 默认高度是22
return 40;
}
//组尾的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
// 默认高度是22
return 40;
}
@end
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
NSMutableArray *_dataArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createUITableView];
[self createDataArray];

self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//编辑按钮触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
NSLog(@"%d",editing);
// 重新调用父类的方法,处于Done字样时,editing=YES,处于Edit样式时,editing = NO.
[super setEditing:editing animated:YES];

// 设置tableView的可编辑性
[_tableView setEditing:editing animated:YES];
}
-(void)createUITableView{
// 初始化UITableView 设置样式和frame
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 设置背景色
_tableView.backgroundColor = [UIColor orangeColor];
// 设置代理
_tableView.dataSource = self;
_tableView.delegate = self;
// 添加到视图
[self.view addSubview:_tableView];
}
-(void)createDataArray{

_dataArray = [NSMutableArray array];

// 数据源中要添加两组数据:删除的、增加的
NSMutableArray *deleteArray = [NSMutableArray array];
for (int i = 0 ; i < 10; i ++) {
NSString *deleteStr = [NSString stringWithFormat:@"被删除的第%d行",i];
[deleteArray addObject:deleteStr];
}
NSMutableArray *insertArray = [NSMutableArray array];
for (int i = 0 ; i < 10; i ++) {
NSString *insertStr = [NSString stringWithFormat:@"增加的第%d行",i];
[insertArray addObject:insertStr];
}

[_dataArray addObject:deleteArray];
[_dataArray addObject:insertArray];
NSLog(@"%@",_dataArray);
}
#pragma mark -
#pragma mark dataSource
//返回有多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataArray.count;
}
//每组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSMutableArray *array = [_dataArray objectAtIndex:section];

return array.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.定义标识
static NSString *identifier = @"cellID";
// 2.从tableView复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 3.如果取不到cell,就创建新的cell
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}

// cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];

// 赋值
cell.textLabel.text = [[_dataArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];

return cell;
}
#pragma mark -
#pragma mark delegate
//选中了哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"选中了哪一行");
NSLog(@"第%ld组-第%ld行",indexPath.section,indexPath.row);
}
//反选
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"didDeselectRowAtIndexPath");
NSLog(@"----第%ld组-第%ld行",indexPath.section,indexPath.row);
}
//设置tableView 的编辑状态
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

// UITableViewCellEditingStyleNone,无效果
// UITableViewCellEditingStyleDelete,删除
// UITableViewCellEditingStyleInsert 增加

if (indexPath.section == 0) {
return UITableViewCellEditingStyleDelete;
}else{
return UITableViewCellEditingStyleInsert;
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {
// 删除当前对应行的数据源
[[_dataArray objectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row];

// [tableView reloadData];
// 刷新表
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}else{
NSString *insertStr = @"我是新来的";
// 数据源添加新数据
[[_dataArray objectAtIndex:indexPath.section] insertObject:insertStr atIndex:indexPath.row];
// 刷表
// [tableView reloadData];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
//是否允许某一行被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.row % 2 == 0) {
// return YES;
// }
// return NO;
return YES;
}
//移动要实现的方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
// sourceIndexPath 将要移动的cell的位置信息
// destinationIndexPath 目的位置信息

// 根据sourceIndexPath.section来获得第section组的数据
NSMutableArray *sourceArray = _dataArray[sourceIndexPath.section];
// 取出要移动cell所在行的数据
NSString *sourceStr = sourceArray[sourceIndexPath.row];
// 删除要移动cell所在行的数据
[sourceArray removeObjectAtIndex:sourceIndexPath.row];

// 根据destinationIndexPath.section来获取要插入的数组
NSMutableArray *destinationArray = _dataArray[destinationIndexPath.section];

[destinationArray insertObject:sourceStr atIndex:destinationIndexPath.row];
}
//能否移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return NO;
}
return YES;
}
@end

UIStepper

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
40
41
42
43

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 初始化并设置frame
UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
// 最小值
stepper.minimumValue = 0;

// 最大值
stepper.maximumValue = 10;
// 设置当前值
stepper.value = 4;
// 设置每次+2或-2,默认是1
stepper.stepValue = 2;

// 设置颜色
[stepper setTintColor:[UIColor orangeColor]];
// 添加事件
[stepper addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

// 处理事件是否一直回调,默认是YES
stepper.continuous = YES;

// 设置能否重复操作,默认是YES
stepper.autorepeat = YES;

// 设置是否能循环,默认是NO
stepper.wraps = YES;

[self.view addSubview:stepper];
}
-(void)valueChange:(UIStepper*)stepper{
NSLog(@"%lf",stepper.value);
}
@end

UISlider

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// slider高度是固定的
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];
slider.tag = 10;
// 最小值
slider.minimumValue = 0;
// 最大值
slider.maximumValue = 255;
// 设置当前值
slider.value = 200;

// 未滑动到的颜色
slider.maximumTrackTintColor = [UIColor greenColor];
// 已经滑动过的颜色
slider.minimumTrackTintColor = [UIColor orangeColor];
// 小球(滑块)颜色
slider.thumbTintColor = [UIColor blackColor];
// 设置图片
// 最左端图片
slider.minimumValueImage = [UIImage imageNamed:@"blackHeart"];
// 最右端图片
slider.maximumValueImage = [UIImage imageNamed:@"redHeart"];
// 设置滑块正常状态下的图片
[slider setThumbImage:[UIImage imageNamed:@"blackHeart"] forState:UIControlStateNormal];
// 设置滑块高亮状态下的图片
[slider setThumbImage:[UIImage imageNamed:@"redHeart"] forState:UIControlStateHighlighted];

[slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];

UISlider *greenSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 200, 200, 80)];
greenSlider.tag = 11;
greenSlider.minimumValue = 0;
greenSlider.maximumValue = 255;
[greenSlider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:greenSlider];

UISlider *blueSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 300, 200, 100)];
blueSlider.tag = 12;
blueSlider.minimumValue = 0;
blueSlider.maximumValue = 255;
[blueSlider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:blueSlider];
}
-(void)valueChange:(UISlider*)slider{
NSLog(@"---%lf",slider.value);
UISlider *redSlider = (UISlider *)[self.view viewWithTag:10];
UISlider *greenSlider = (UISlider *)[self.view viewWithTag:11];
UISlider *blueSlider = (UISlider *)[self.view viewWithTag:12];

[self.view setBackgroundColor:[UIColor colorWithRed:redSlider.value/255.0 green:greenSlider.value/255.0 blue:blueSlider.value/255.0 alpha:1.0]];


}

@end

UIsegementControl

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 初始化UISegmentedControl
UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
// 设置背景色
segmentControl.backgroundColor = [UIColor yellowColor];
// 添加第一段
[segmentControl insertSegmentWithTitle:@"第一段" atIndex:0 animated:YES];
// 添加第二段
[segmentControl insertSegmentWithTitle:@"第二段" atIndex:1 animated:YES];
// 添加第三段
// 设置文字和图片,谁在后面,显示谁
[segmentControl insertSegmentWithTitle:@"第三段" atIndex:2 animated:YES];

UIImage *image = [UIImage imageNamed:@"qq"];
// 显示原始图片
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// [segmentControl insertSegmentWithImage: image atIndex:2 animated:YES];
[segmentControl setImage:image forSegmentAtIndex:2];


// 选中颜色
segmentControl.tintColor = [UIColor redColor];
// 设置选中某一段
segmentControl.selectedSegmentIndex = 1;

// 获取某一段的title
NSString *title = [segmentControl titleForSegmentAtIndex:1];
NSLog(@"title = %@",title);
// 添加响应事件
[segmentControl addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:segmentControl];

}
-(void)valueChange:(UISegmentedControl*)seg{

NSInteger index = seg.selectedSegmentIndex;
switch (index) {
case 0:{
NSLog(@"0");

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"还钱" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"不还", nil];
[alert show];
}
break;
case 1:
NSLog(@"1");
break;
case 2:
NSLog(@"2");
break;

default:
break;
}
}

@end

Transform形变

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

// [self transform];
[self lastTransform];
}
#
-(void)lastTransform{
UIView *changeView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
changeView.tag = 20;
changeView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:changeView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 50, 50, 50);
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)onClick{

// 仿射变化
// 通过center bounds transform来控制要显示的内容
UIView *changeView = [self.view viewWithTag:20];
// 在上次形变的基础上,继续形变
changeView.transform = CGAffineTransformTranslate(changeView.transform, 10, 0);

// 旋转
CGFloat angle = [self getAgreeFromDegree:10];
changeView.transform = CGAffineTransformRotate(changeView.transform, angle);

// 缩放
// changeView.transform = CGAffineTransformScale(changeView.transform, 0.9, 1.0);
NSLog(@"%@",NSStringFromCGRect(changeView.frame));
NSLog(@"%f",changeView.bounds.size.width);
NSLog(@"%f",changeView.bounds.size.height);
NSLog(@"%f",changeView.center.x);
NSLog(@"%f",changeView.center.y);

}
#pragma mark -
#pragma mark 形变
//形变
-(void)transform{
UIView *changeView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
[view1 setBackgroundColor:[UIColor blueColor]];
[changeView addSubview:view1];

changeView.backgroundColor = [UIColor yellowColor];

// 平移
changeView.transform = CGAffineTransformMakeTranslation(0, 100);
// 缩放
// changeView.transform = CGAffineTransformMakeScale(0.5, 1.0);

// 旋转
// 传入的值为正值时,顺时针转动
CGFloat angle = [self getAgreeFromDegree:20];
changeView.transform = CGAffineTransformMakeRotation(angle);

[self.view addSubview:changeView];
}
-(CGFloat)getAgreeFromDegree:(CGFloat)degree{
CGFloat agree = degree * M_PI / 180;
return agree;
}
//创建一个UIView
-(void)createUIView{
// 初始化UIView
UIView *orangeView = [[UIView alloc]init];
// 设置背景色
orangeView.backgroundColor = [UIColor orangeColor];
// 设置frame
orangeView.frame = CGRectMake(0, 0, 100, 100);
// 设置center
orangeView.center = CGPointMake(0, 0);
orangeView.center = self.view.center;

// 添加到视图
[self.view addSubview:orangeView];
}

@end

UIActionSheet

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
40
41
42
43
44
#import "ViewController.h"

@interface ViewController ()<UIActionSheetDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 初始化
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@"微博分享",@"微信分享",@"QQ分享", nil];
// 显示在指定的view上
[sheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
// 在这里做事件处理
switch (buttonIndex) {
case 0:
NSLog(@"%ld",buttonIndex);
break;
case 1:
NSLog(@"%ld",buttonIndex);
break;
case 2:
NSLog(@"%ld",buttonIndex);
break;
case 3:
NSLog(@"%ld",buttonIndex);
break;
case 4:
NSLog(@"%ld",buttonIndex);
break;


default:
break;
}
}
@end