• iOS UITableView制作类似QQ好友列表视图


                     

    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     UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
    16     self.window.rootViewController = navi;
    17     
    18     [self.window makeKeyAndVisible];
    19     return YES;
    20 }
    21 
    22 @end
    1 #import <UIKit/UIKit.h>
    2 
    3 @interface RootViewController : UIViewController
    4 
    5 @end
      1 #import "RootViewController.h"
      2 
      3 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
      4 {
      5     UITableView *_tableView;
      6     NSMutableDictionary *dataDic;
      7 }
      8 @end
      9 
     10 @implementation RootViewController
     11 
     12 - (void)viewDidLoad {
     13     [super viewDidLoad];
     14     // 初始化_tableView
     15     [self initializeTableView];
     16     // 加载数据
     17     [self loadData];
     18 }
     19 /**
     20  *  初始化_tableView
     21  */
     22 - (void)initializeTableView
     23 {
     24     _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
     25     _tableView.dataSource = self;
     26     _tableView.delegate = self;
     27     [self.view addSubview:_tableView];
     28 }
     29 /**
     30  *  加载数据
     31  */
     32 - (void)loadData
     33 {
     34     // 数组的第一个对象为标志位(是否展开)
     35     NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"0",@"apple",@"alex",@"alert",@"awake", nil];
     36     NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"0",@"blance",@"bank",@"baby",@"bet",@"balance", nil];
     37     NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"0",@"cake",@"cat",@"caught",@"cell",@"clabe", @"cry",@"cave",nil];
     38     NSMutableArray *arr4 = [NSMutableArray arrayWithObjects:@"0",@"dog",@"data",@"date",@"drive",@"down", @"deliver",@"dire",@"drawn",@"dety",@"depature",@"dom",nil];
     39     NSMutableArray *arr5 = [NSMutableArray arrayWithObjects:@"0",@"elphance",@"eleven",@"every",nil];
     40     // 把对应的数据存入字典
     41     dataDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:arr1,@"A",arr2,@"B",arr3,@"C",arr4,@"D",arr5,@"E", nil] ;
     42 }
     43 
     44 #pragma mark - UITableViewDataSource And UITableViewDelegate - 
     45 // 返回的表头个数
     46 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     47 {
     48     return [[dataDic allKeys] count];
     49 }
     50 // 每个表头返回对应的行数
     51 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     52 {
     53     // 因为字典是无序的,通过比较来进行排序
     54     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
     55     NSString *key = keys[section];
     56     NSMutableArray *array = dataDic[key];
     57     NSString *IsExpand = array[0];
     58     // 如果为“1”则返回相应的行数,否则返回0
     59     if ([IsExpand isEqualToString:@"1"]) {
     60         // 因为数组的第一位为标志位所以减1
     61         return ([array count] - 1);
     62     }
     63     return 0;
     64 }
     65 
     66 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
     67 {
     68     return 45;
     69 }
     70 
     71 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
     72 {
     73     return 1;
     74 }
     75 
     76 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     77 {
     78     return 60;
     79 }
     80 
     81 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
     82 {
     83     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
     84     view.backgroundColor = [UIColor colorWithRed:206 green:206 blue:206 alpha:1];
     85     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width - 20, 45)];
     86     label.font = [UIFont systemFontOfSize:30];
     87     label.backgroundColor = [UIColor clearColor];
     88     label.textColor = [UIColor blackColor];
     89     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
     90     label.text = keys[section];
     91     [view addSubview:label];
     92     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     93     button.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45);
     94     [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
     95     button.tag = 1000 + section;
     96     [view addSubview:button];
     97     return view;
     98 }
     99 
    100 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    101 {
    102     static NSString *identifier = @"cell";
    103     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    104     if (cell == nil) {
    105         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    106     }
    107     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    108     NSString *key = keys[indexPath.section];
    109     NSMutableArray *array = dataDic[key];
    110     // 由于数组的第一位是标志位,且不用显示,所以加1
    111     NSString *textStr = array[indexPath.row + 1];
    112     cell.textLabel.text = textStr;
    113     return cell;
    114 }
    115 
    116 #pragma mark -Target Action-
    117 /**
    118  *  点击表头,如果没有展开,则展开;如果已经展开,则关闭
    119  */
    120 - (void)buttonAction:(UIButton *)sender
    121 {
    122     long section = sender.tag - 1000;
    123     NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    124     NSString *key = keys[section];
    125     NSMutableArray *array = dataDic[key];
    126     NSString *IsExpand = array[0];
    127     // 如果IsExpand等于“0”(关闭状态),则展开,且设置其值为“1”;相反,如果IsExpand等于“1”(展开状态),则关闭,且设置其值为“0”
    128     if ([IsExpand isEqualToString:@"0"]) {
    129         array[0] = @"1";
    130     }else{
    131         array[0] = @"0";
    132     }
    133     [_tableView reloadData];
    134 }
    135 @end
  • 相关阅读:
    onkeypress事件.onkeydown事件.onkeyup事件
    汉诺塔递归算法拙见
    《编写可读代码的艺术》读后总结
    select下拉菜单反显不可改动,且submit能够提交数据
    Freemarker list 的简单使用
    Freemarker导出带格式的word的使用
    Freemarker导出word的简单使用
    Freemarker取list集合中数据(将模板填充数据后写到客户端HTML)
    struts2在配置文件与JSP中用OGNL获取Action属性
    Web下文件上传下载的路径问题
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4758774.html
Copyright © 2020-2023  润新知