iOS7 Creating Custom TableView Cell Accessories

####iOS7 Programming Cookbook 第四章 Creating Custom Table View Cell Accessories

#####ViewController.m

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

static NSString *MyCellIdentifier = @"SimpleCell";

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation ViewController

//iOS7电池状态栏 样式
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}

-(void)viewWillLayoutSubviews{
//iOS7,UIScreen下降20像素,显示黑色电池栏
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.view.clipsToBounds = YES;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
self.view.frame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
}

//__unused
- (UIView *) superviewOfType:(Class)paramSuperviewClass
forView:(UIView *)paramView{
if (paramView.superview != nil){
if ([paramView.superview isKindOfClass:paramSuperviewClass]){
return paramView.superview;
} else {
return [self superviewOfType:paramSuperviewClass
forView:paramView.superview];
}
}
return nil;
}

//处理按钮点击事件
- (void) performExpand:(UIButton *)paramSender{
//__unused 未使用
__unused UITableViewCell *parentCell = (UITableViewCell *)[self superviewOfType:[UITableViewCell class] forView:paramSender];

}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell* cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier
forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld",
(long)indexPath.section,
(long)indexPath.row];
//初始化button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//设置坐标
button.frame = CGRectMake(0.0f, 0.0f, 150.0f, 25.0f);
//设置title,规则
[button setTitle:@"Expand"
forState:UIControlStateNormal];

//添加点击事件
[button addTarget:self
action:@selector(performExpand:)
forControlEvents:UIControlEventTouchUpInside];

cell.accessoryView = button;

return cell;

}

//3 section
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}

//3rows
- (NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return 3;
}

- (void)viewDidLoad{
[super viewDidLoad];
//使用布局,显示扩展边缘
// self.edgesForExtendedLayout = UIRectEdgeNone;

//初始化tableview,style为plain类型
self.myTableView = [[UITableView alloc]
initWithFrame:self.view.bounds
style:UITableViewStylePlain];

//注册cell类
[self.myTableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:MyCellIdentifier];

//datasource,delegate指向ViewController
self.myTableView.dataSource = self;
self.myTableView.delegate = self;

//自动调整布局
self.myTableView.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;

//添加到view
[self.view addSubview:self.myTableView];

}

@end

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%