iOS 列表三级展开

效果图如下:

             

1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 
5 @property (strong, nonatomic) UIWindow *window;
6 
7 
8 @end
 1 #import "AppDelegate.h"
 2 #import "RootViewController.h"
 3 @interface AppDelegate ()
 4 
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
12     // Override point for customization after application launch.
13     self.window.backgroundColor = [UIColor whiteColor];
14     
15     RootViewController *root = [[RootViewController alloc] init];
16     UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:root];
17     self.window.rootViewController = navi;
18     
19     [self.window makeKeyAndVisible];
20     return YES;
21 }
22 
23 
24 @end
1 #import <UIKit/UIKit.h>
2 
3 @interface RootViewController : UIViewController
4 
5 @end
  1 #import "RootViewController.h"
  2 #import "MainCell.h"
  3 #import "DetialTableViewCell.h"
  4 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
  5 {
  6     UITableView *_tableView;
  7     NSMutableDictionary *dic;//存对应的数据
  8     NSMutableArray *selectedArr;// 二级列表是否展开状态
  9     NSMutableArray *titleDataArray;
 10     NSArray *dataArray;//数据源,显示每个cell的数据
 11     NSMutableDictionary *stateDic;//三级列表是否展开状态
 12     NSMutableArray *groupArr0;
 13     NSMutableArray *groupArr1;
 14     NSMutableArray *groupArr2;
 15     NSMutableArray *groupArr3;
 16     NSMutableArray *groupArr4;
 17     NSMutableArray *groupArr5;
 18     
 19 }
 20 @end
 21 
 22 @implementation RootViewController
 23 
 24 - (void)viewDidLoad {
 25     [super viewDidLoad];
 26     self.title = @"列表";
 27     dic = [[NSMutableDictionary alloc] init];
 28     selectedArr = [[NSMutableArray alloc] init];
 29     dataArray = [[NSMutableArray alloc] init];
 30     
 31     // 初始化tableView
 32     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320*scaleX, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
 33     _tableView.delegate = self;
 34     _tableView.dataSource = self;
 35     // 隐藏滚动条
 36     _tableView.showsVerticalScrollIndicator = NO;
 37     _tableView.showsHorizontalScrollIndicator = NO;
 38     _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 39     [self.view addSubview:_tableView];
 40     
 41     // 加载数据
 42     [self loadData];
 43 }
 44 /**
 45  *  加载数据
 46  */
 47 - (void)loadData
 48 {
 49     titleDataArray = [[NSMutableArray alloc] initWithObjects:@"亲朋",@"家人",@"好友", @"蓝颜",@"同学",@"老乡",nil];
 50     
 51     NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"小莉",@"name",@"NO",@"state", nil];
 52     NSMutableDictionary *nameAndStateDic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"婷婷",@"name",@"NO",@"state", nil];
 53     NSMutableDictionary *nameAndStateDic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"小庄",@"name",@"NO",@"state", nil];
 54     
 55     groupArr0 = [[NSMutableArray alloc] initWithObjects:nameAndStateDic1,nameAndStateDic2,nameAndStateDic3, nil];
 56     
 57     NSMutableDictionary *nameAndStateDic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"晨晨姐",@"name",@"NO",@"state",nil];
 58     NSMutableDictionary *nameAndStateDic5 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"李涛",@"name",@"NO",@"state",nil];
 59     NSMutableDictionary *nameAndStateDic6 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"海波",@"name",@"NO",@"state",nil];
 60     NSMutableDictionary *nameAndStateDic7 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"张敏",@"name",@"NO",@"state",nil];
 61     
 62     groupArr1 = [[NSMutableArray alloc]initWithObjects:nameAndStateDic4,nameAndStateDic5,nameAndStateDic6,nameAndStateDic7, nil];
 63     
 64     NSMutableDictionary *nameAndStateDic8 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"杨浩",@"name",@"NO",@"state",nil];
 65     NSMutableDictionary *nameAndStateDic9 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"小明",@"name",@"NO",@"state",nil];
 66     NSMutableDictionary *nameAndStateDic10 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"洋洋",@"name",@"NO",@"state",nil];
 67     NSMutableDictionary *nameAndStateDic11 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"赵蒙",@"name",@"NO",@"state",nil];
 68     NSMutableDictionary *nameAndStateDic12 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"小催",@"name",@"NO",@"state",nil];
 69     NSMutableDictionary *nameAndStateDic13 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"和平",@"name",@"NO",@"state",nil];
 70     
 71     groupArr2 = [[NSMutableArray alloc]initWithObjects:nameAndStateDic8,nameAndStateDic9,nameAndStateDic10,nameAndStateDic11,nameAndStateDic12,nameAndStateDic13,nil];
 72     
 73     NSMutableDictionary *nameAndStateDic14 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"超人",@"name",@"NO",@"state",nil];
 74     NSMutableDictionary *nameAndStateDic15 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"怪兽",@"name",@"NO",@"state",nil];
 75     
 76     groupArr3 = [[NSMutableArray alloc] initWithObjects:nameAndStateDic14,nameAndStateDic15, nil];
 77     
 78     NSMutableDictionary *nameAndStateDic16 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"大包",@"name",@"NO",@"state",nil];
 79     NSMutableDictionary *nameAndStateDic17 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"小林子",@"name",@"NO",@"state",nil];
 80     NSMutableDictionary *nameAndStateDic18 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"石头",@"name",@"NO",@"state",nil];
 81     NSMutableDictionary *nameAndStateDic19 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"小轩轩",@"name",@"NO",@"state",nil];
 82     NSMutableDictionary *nameAndStateDic20 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"阿东",@"name",@"NO",@"state",nil];
 83     
 84     groupArr4 = [[NSMutableArray alloc]initWithObjects:nameAndStateDic16,nameAndStateDic17,nameAndStateDic18,nameAndStateDic19,nameAndStateDic20, nil];
 85     
 86     NSMutableDictionary *nameAndStateDic21 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"郑平",@"name",@"NO",@"state",nil];
 87     NSMutableDictionary *nameAndStateDic22 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"刘凡",@"name",@"NO",@"state",nil];
 88     NSMutableDictionary *nameAndStateDic23 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"韩琴",@"name",@"NO",@"state",nil];
 89     NSMutableDictionary *nameAndStateDic24 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"刘华健",@"name",@"NO",@"state",nil];
 90     NSMutableDictionary *nameAndStateDic25 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"彭晓明",@"name",@"NO",@"state",nil];
 91     NSMutableDictionary *nameAndStateDic26 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"张欢",@"name",@"NO",@"state",nil];
 92     NSMutableDictionary *nameAndStateDic27 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"刘来楠",@"name",@"NO",@"state",nil];
 93     NSMutableDictionary *nameAndStateDic28 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",@"任强",@"name",@"NO",@"state",nil];
 94     
 95     groupArr5 = [[NSMutableArray alloc]initWithObjects:nameAndStateDic21,nameAndStateDic22,nameAndStateDic23,nameAndStateDic24,nameAndStateDic25,nameAndStateDic26,nameAndStateDic27,nameAndStateDic28, nil];
 96 
 97     [dic setValue:groupArr0 forKey:@"0"];
 98     [dic setValue:groupArr1 forKey:@"1"];
 99     [dic setValue:groupArr2 forKey:@"2"];
100     [dic setValue:groupArr3 forKey:@"3"];
101     [dic setValue:groupArr4 forKey:@"4"];
102     [dic setValue:groupArr5 forKey:@"5"];
103     
104 }
105 
106 #pragma mark - tableViewDelegate - 
107 // 返回几个表头
108 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
109 {
110     return titleDataArray.count;
111 }
112 
113 // 每个表头返回几行
114 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
115 {
116     NSString *string = [NSString stringWithFormat:@"%d",(int)section];
117     // 如果表头返回值不为零,则修改图片(改成箭头向下的图片)
118     if ([selectedArr containsObject:string]) {
119         UIImageView *imageView = (UIImageView *)[_tableView viewWithTag:20000 + section];
120         imageView.image = [UIImage imageNamed:@"buddy_header_arrow_down"];
121         
122         NSArray *array1 = dic[string];
123         return array1.count;
124     }
125     return 0;
126 }
127 
128 // 设置表头的高度
129 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
130 {
131     return 40*scaleX;
132 }
133 // Section Footer的高度
134 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
135 {
136     return 0.2;
137 }
138 // 设置view,将替代titleForHeaderInsection方法
139 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
140 {
141     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320*scaleX, 30*scaleY)];
142     view.backgroundColor = [UIColor whiteColor];
143     
144     UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*scaleX, 5*scaleY, tableView.frame.size.width - 20*scaleX, 30*scaleY)];
145     titleLabel.text = [titleDataArray objectAtIndex:section];
146     [view addSubview:titleLabel];
147     
148     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5*scaleX, 12*scaleY, 15*scaleX, 15*scaleY)];
149     imageView.tag = 20000+section;
150 
151     // 判断是不是选中状态
152     NSString *string = [NSString stringWithFormat:@"%d",(int)section];
153     if ([selectedArr containsObject:string]) {
154         imageView.image = [UIImage imageNamed:@"buddy_header_arrow_down"];
155     }else{
156         imageView.image = [UIImage imageNamed:@"buddy_header_arrow_right"];
157     }
158     [view addSubview:imageView];
159     
160     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
161     button.frame = CGRectMake(0, 0, 320*scaleX, 40*scaleY);
162     button.tag = 100 + section;
163     [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
164     [view addSubview:button];
165     
166     UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40*scaleY - 1, 320*scaleX, 1)];
167     lineImage.image = [UIImage imageNamed:@"line"];
168     [view addSubview:lineImage];
169     
170     return view;
171 }
172 
173 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
174 {
175     NSString *indexStr = [NSString stringWithFormat:@"%d",(int)indexPath.section];
176     if ([dic[indexStr][indexPath.row][@"cell"] isEqualToString:@"MainCell"]) {
177         return 60*scaleY;
178     }else{
179         return 40*scaleY;
180     }
181 }
182 
183 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
184 {
185     // 当前是第几个表头
186     NSString *indexStr = [NSString stringWithFormat:@"%d",(int)indexPath.section];
187     if ([dic[indexStr][indexPath.row][@"cell"] isEqualToString:@"MainCell"]) {
188         static NSString *identifier = @"MainCell";
189         MainCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
190         
191         if (cell == nil) {
192             cell = [[MainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
193             cell.selectionStyle = UITableViewCellSelectionStyleGray;
194         }
195         
196         if ([selectedArr containsObject:indexStr]) {
197             cell.Headerphoto.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random()%11]];
198             cell.nameLabel.text = dic[indexStr][indexPath.row][@"name"];
199             cell.IntroductionLabel.text = @"滴滴雨点仿佛似流泪,滴在我冰冷的身躯";
200             cell.networkLabel.text = @"2G";
201         }
202         // ???
203         if (indexPath.row == dataArray.count - 1) {
204             cell.imageLine.image = nil;
205         }else{
206             cell.imageLine.image = [UIImage imageNamed:@"line"];
207         }
208         return cell;
209     }else if ([dic[indexStr][indexPath.row][@"cell"] isEqualToString:@"AttachedCell"]){
210         static NSString *detialIdentifier = @"AttachedCell";
211         DetialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detialIdentifier];
212         
213         if (cell == nil) {
214             cell = [[DetialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:detialIdentifier];
215             cell.selectionStyle = UITableViewCellSeparatorStyleNone;
216             cell.imageLine.image = [UIImage imageNamed:@"line"];
217         }
218         return cell;
219     }
220     return nil;
221 }
222 
223 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
224 {
225     // TODO:search
226     [_tableView deselectRowAtIndexPath:indexPath animated:YES];
227     NSString *indexStr = [NSString stringWithFormat:@"%d",(int)indexPath.section];
228     NSIndexPath *path = nil;
229     
230     if ([dic[indexStr][indexPath.row][@"cell"] isEqualToString:@"MainCell"]) {
231         // TODO:search
232         path = [NSIndexPath indexPathForItem:(indexPath.row + 1) inSection:indexPath.section];
233     }else{
234         path = indexPath;
235     }    
236     if ([dic[indexStr][indexPath.row][@"state"] boolValue]) {
237         // 关闭附加cell
238         NSMutableDictionary *dd = dic[indexStr][indexPath.row];
239         NSString *name = dd[@"name"];
240         
241         NSMutableDictionary *nameAndStateDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",name,@"name",@"NO",@"state", nil];
242         
243         switch (indexPath.section) {
244             case 0:
245             {
246                 groupArr0[(path.row-1)] = nameAndStateDic;
247                 [groupArr0 removeObjectAtIndex:path.row];
248             }
249                 break;
250             case 1:
251             {
252                 groupArr1[(path.row-1)] = nameAndStateDic;
253                 [groupArr1 removeObjectAtIndex:path.row];
254             }
255                 break;
256             case 2:
257             {
258                 groupArr2[(path.row-1)] = nameAndStateDic;
259                 [groupArr2 removeObjectAtIndex:path.row];
260             }
261                 break;
262             case 3:
263             {
264                 groupArr3[(path.row-1)] = nameAndStateDic;
265                 [groupArr3 removeObjectAtIndex:path.row];
266             }
267                 break;
268             case 4:
269             {
270                 groupArr4[(path.row-1)] = nameAndStateDic;
271                 [groupArr4 removeObjectAtIndex:path.row];
272             }
273                 break;
274             case 5:
275             {
276                 groupArr5[(path.row-1)] = nameAndStateDic;
277                 [groupArr5 removeObjectAtIndex:path.row];
278             }
279                 break;
280             default:
281                 break;
282         }
283         
284         [_tableView beginUpdates];
285         [_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
286         [_tableView endUpdates];
287     }else{
288         // 打开附加cell
289         NSMutableDictionary *dd = dic[indexStr][indexPath.row];
290         NSString *name = dd[@"name"];
291         
292         NSMutableDictionary *nameAndStateDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",name,@"name",@"YES",@"state", nil];
293         switch (indexPath.section) {
294             case 0:
295             {
296                 groupArr0[(path.row-1)] = nameAndStateDic;
297                 NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state", nil];
298                 [groupArr0 insertObject:nameAndStateDic1 atIndex:path.row];
299             }
300                 break;
301             case 1:
302             {
303                 groupArr1[(path.row-1)] = nameAndStateDic;
304                 NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state", nil];
305                 [groupArr1 insertObject:nameAndStateDic1 atIndex:path.row];
306             }
307                 break;
308             case 2:
309             {
310                 groupArr2[(path.row-1)] = nameAndStateDic;
311                 NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state", nil];
312                 [groupArr2 insertObject:nameAndStateDic1 atIndex:path.row];
313             }
314                 break;
315             case 3:
316             {
317                 groupArr3[(path.row-1)] = nameAndStateDic;
318                 NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state", nil];
319                 [groupArr3 insertObject:nameAndStateDic1 atIndex:path.row];
320             }
321                 break;
322             case 4:
323             {
324                 groupArr4[(path.row-1)] = nameAndStateDic;
325                 NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state", nil];
326                 [groupArr4 insertObject:nameAndStateDic1 atIndex:path.row];
327             }
328                 break;
329             case 5:
330             {
331                 groupArr5[(path.row-1)] = nameAndStateDic;
332                 NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state", nil];
333                 [groupArr5 insertObject:nameAndStateDic1 atIndex:path.row];
334             }
335                 break;
336             default:
337                 break;
338         }
339         [_tableView beginUpdates];
340         [_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
341         [_tableView endUpdates];
342     }
343 }
344 
345 -(void)buttonAction:(UIButton *)sender
346 {
347     NSString *string = [NSString stringWithFormat:@"%d",(int)(sender.tag-100)];
348     
349     //数组selectedArr里面存的数据和表头想对应,方便以后做比较
350     if ([selectedArr containsObject:string])
351     {
352         [selectedArr removeObject:string];
353     }
354     else
355     {
356         [selectedArr addObject:string];
357     }
358     
359     [_tableView reloadData];
360 }
361 
362 - (void)didReceiveMemoryWarning {
363     [super didReceiveMemoryWarning];
364     // Dispose of any resources that can be recreated.
365 }
366 
367 
368 
369 @end
 1 #import <UIKit/UIKit.h>
 2 
 3 @interface MainCell : UITableViewCell
 4 
 5 @property (strong, nonatomic) UILabel *nameLabel;
 6 
 7 @property (strong, nonatomic) UILabel *IntroductionLabel;
 8 
 9 @property (strong, nonatomic) UILabel *networkLabel;
10 
11 @property (strong, nonatomic) UIImageView *Headerphoto;
12 
13 @property (strong, nonatomic) UIImageView *imageLine;
14 
15 @end
 1 #import "MainCell.h"
 2 
 3 @implementation MainCell
 4 
 5 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 6 {
 7     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 8     if (self) {
 9         // 头像
10         _Headerphoto = [[UIImageView alloc] initWithFrame:CGRectMake(6*scaleX, 5*scaleY, 50*scaleX, 50*scaleX)];
11         [self.contentView addSubview:_Headerphoto];
12         // 名字
13         _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60*scaleX, 5*scaleY, 200*scaleX, 25*scaleX)];
14         _nameLabel.font = [UIFont systemFontOfSize:15];
15         [self.contentView addSubview:_nameLabel];
16         // 简介
17         _IntroductionLabel = [[UILabel alloc] initWithFrame:CGRectMake(60*scaleX, 28*scaleY, 240*scaleX, 25*scaleX)];
18         _IntroductionLabel.backgroundColor = [UIColor clearColor];
19         _IntroductionLabel.textColor = [UIColor lightGrayColor];
20         _IntroductionLabel.font = [UIFont systemFontOfSize:13];
21         [self.contentView addSubview:_IntroductionLabel];
22         
23         // 网络
24         _networkLabel = [[UILabel alloc] initWithFrame:CGRectMake(290*scaleX, 5*scaleY, 50*scaleX, 25*scaleY)];
25         _networkLabel.backgroundColor = [UIColor clearColor];
26         _networkLabel.textColor = [UIColor lightGrayColor];
27         _networkLabel.font = [UIFont systemFontOfSize:13];
28         [self.contentView addSubview:_networkLabel];
29         
30         // 分割线
31         _imageLine = [[UIImageView alloc] initWithFrame:CGRectMake(60*scaleX, 60*scaleY - 1, (320 - 60)*scaleX, 1)];
32         [self.contentView addSubview:_imageLine];
33     }
34     return self;
35 }
36 
37 @end
1 #import <UIKit/UIKit.h>
2 
3 @interface DetialTableViewCell : UITableViewCell
4 
5 @property (strong, nonatomic) UIImageView *imageLine;
6 
7 @end
 1 #import "DetialTableViewCell.h"
 2 #import "UIButton+initializer.h"
 3 @implementation DetialTableViewCell
 4 
 5 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 6 {
 7     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 8     if (self) {
 9         // 分割线
10         _imageLine = [[UIImageView alloc] initWithFrame:CGRectMake(60*scaleX, 40*scaleY - 1, (320 - 60)*scaleY, 1)];
11         [self.contentView addSubview:_imageLine];
12         
13         UIButton *btn1 = [UIButton initButtonWithFrame:CGRectMake(70*scaleX, 9*scaleY, 50*scaleX, 20*scaleY) Title:@"语音" Target:self Selector:@selector(btnAction:)];
14         btn1.tag = 100;
15         
16         UIButton *btn2 = [UIButton initButtonWithFrame:CGRectMake(130*scaleX, 9*scaleY, 50*scaleX, 20*scaleY) Title:@"视频" Target:self Selector:@selector(btnAction:)];
17         btn2.tag = 200;
18         
19         UIButton *btn3 = [UIButton initButtonWithFrame:CGRectMake(190*scaleX, 9*scaleY, 50*scaleX, 20*scaleY) Title:@"图片" Target:self Selector:@selector(btnAction:)];
20         btn3.tag = 300;
21         
22         UIButton *btn4 = [UIButton initButtonWithFrame:CGRectMake(250*scaleX, 9*scaleY, 50*scaleX, 20*scaleY) Title:@"表情" Target:self Selector:@selector(btnAction:)];
23         btn4.tag = 400;
24         
25         [self.contentView addSubview:btn1];
26         [self.contentView addSubview:btn2];
27         [self.contentView addSubview:btn3];
28         [self.contentView addSubview:btn4];
29     }
30     return self;
31 }
32 
33 - (void)btnAction:(UIButton *)sender
34 {
35     switch (sender.tag) {
36         case 100:
37         {
38             NSLog(@"~~~~语音~~~~");
39         }
40             break;
41         case 200:
42         {
43             NSLog(@"~~~~视频~~~~");
44         }
45             break;
46         case 300:
47         {
48             NSLog(@"~~~~图片~~~~");
49         }
50             break;
51         case 400:
52         {
53             NSLog(@"~~~~表情~~~~");
54         }
55             break;
56         default:
57             break;
58     }
59 }
60 
61 - (void)awakeFromNib {
62     // Initialization code
63 }
64 
65 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
66     [super setSelected:selected animated:animated];
67 
68     // Configure the view for the selected state
69 }
70 
71 @end
 1 #import <UIKit/UIKit.h>
 2 
 3 @interface UIButton (initializer)
 4 /**
 5  *  初始化button
 6  *
 7  *  @param frame        button的尺寸
 8  *  @param target       button的目标对象
 9  *  @param selector     点击button所触发的方法
10  *  @param image        button的背景图片
11  *  @param imagePressed button高亮时的图片
12  *
13  *  @return button
14  */
15 + (UIButton *)initButtonWithFrame:(CGRect)frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed;
16 /**
17  *  初始化button
18  *
19  *  @param frame    button的尺寸
20  *  @param title    button的标题
21  *  @param target   button的目标对象
22  *  @param selector 点击button所触发的方法
23  *
24  *  @return button
25  */
26 + (UIButton *)initButtonWithFrame:(CGRect)frame Title:(NSString *)title Target:(id)target Selector:(SEL)selector;
27 
28 @end
 1 #import "UIButton+initializer.h"
 2 
 3 @implementation UIButton (initializer)
 4 
 5 + (UIButton *)initButtonWithFrame:(CGRect)frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
 6 {
 7     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 8     [button setFrame:frame];
 9     UIImage *newImage = [UIImage imageNamed:image];
10     [button setBackgroundImage:newImage forState:0];
11     UIImage *newPressedImage = [UIImage imageNamed:imagePressed];
12     [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
13     [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
14     return button;
15 }
16 
17 + (UIButton *)initButtonWithFrame:(CGRect)frame Title:(NSString *)title Target:(id)target Selector:(SEL)selector
18 {
19     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
20     [button setFrame:frame];
21     [button setTitle:title forState:0];
22     [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
23     return button;
24 }
25 
26 @end

pch文件的代码:

1 #ifndef QQList_PrefixHeader_pch
2 #define QQList_PrefixHeader_pch
3 
4 #define scaleX [UIScreen mainScreen].bounds.size.width/320.0
5 #define scaleY [UIScreen mainScreen].bounds.size.height/568.0
6 
7 #endif
原文地址:https://www.cnblogs.com/lantu1989/p/4755822.html