ios7 using different types of accessories in a tableviewcell

####iOS7 Programming Cookbook 第四章 Using Different Types of Accessories in a Table View Cell

#####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

static NSString *MyCellIdentifier = @"SimpleCell";

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

@implementation

- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

/* Do something when the accessory button is tapped */
NSLog(@"Accessory button is tapped for cell at index path = %@", indexPath);

UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];

NSLog(@"Cell Title = %@", ownerCell.textLabel.text);

}

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

UITableViewCell* result = nil;

if ([tableView isEqual:self.myTableView]){
result = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier
forIndexPath:indexPath];
result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld",(long)indexPath.section,(long)indexPath.row];
//标准附件类型 添加i按钮
result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}

return result;

}

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

- (void)viewDidLoad{
[super viewDidLoad];

//初始化tableview,style为group类型
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//注册cell类
[self.myTableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:MyCellIdentifier];
//datasource指向ViewController
self.myTableView.dataSource = self;
//自动调整布局
self.myTableView.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
//添加到view
[self.view addSubview:self.myTableView];

}


//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);
}
}
@end

Reference