QQ风格的列表

头文件:

 

Ios代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface QQstyleViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {  
  4.       
  5.     UITableView *table;     
  6.     NSMutableArray *array;     
  7.     BOOL *flag;     
  8.       
  9. }     
  10.   
  11. @property (nonatomic, retain) UITableView *table;    
  12.   
  13. @end  

 

实现文件:

 

Ios代码   收藏代码
  1. #import "QQstyleViewController.h"  
  2.   
  3. @implementation QQstyleViewController  
  4.   
  5. @synthesize table;   
  6.   
  7. - (void)viewDidLoad {  
  8.     [super viewDidLoad];  
  9.     table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];     
  10.     table.delegate = self;     
  11.     table.dataSource = self;    
  12.     [self.view addSubview:table];   
  13.   NSArray *ary1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];  
  14.     NSArray *ary2 = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", nil];  
  15.     NSArray *ary3 = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", nil];  
  16.     array = [[NSMutableArray alloc] initWithObjects:ary1, ary2, ary3, nil];    
  17.     [ary1 release];  
  18.     [ary2 release];  
  19.     [ary3 release];  
  20.     flag = (BOOL *) malloc([array count] * sizeof(BOOL *));     
  21.     memset(flag, NO, sizeof(flag));  
  22. }  
  23.   
  24. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {     
  25.     return [array count];     
  26. }     
  27.   
  28. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     
  29.     if (flag[section]) {     
  30.         return [(NSArray *)[array objectAtIndex:section] count];     
  31.     }     
  32.     else {     
  33.         return 0;     
  34.     }     
  35. }     
  36.   
  37. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     
  38. {     
  39.     static NSString *CellIdentifier = @"CellIdentifier";     
  40.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     
  41.     if (cell == nil) {     
  42.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];     
  43.         cell.selectionStyle = UITableViewCellSelectionStyleNone;     
  44.     }     
  45.     NSString *str = [[array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];     
  46.     cell.textLabel.text = str;     
  47.     return cell;     
  48. }     
  49.   
  50. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section     
  51. {     
  52.     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];   
  53.     btn.tag = section;     
  54.     [btn addTarget:self action:@selector(headerClicked:) forControlEvents:UIControlEventTouchUpInside];     
  55.     return btn;     
  56. }     
  57.   
  58. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  
  59.     return 30;  
  60. }  
  61.   
  62. -(void)headerClicked:(id)sender     
  63. {     
  64.     int sectionIndex = ((UIButton *)sender).tag;     
  65.     flag[sectionIndex] = !flag[sectionIndex];     
  66.     [table reloadData];     
  67. }     
  68.   
  69. - (void)dealloc {  
  70.     free(flag);    
  71.     [array release];  
  72.     [table release];    
  73.     [super dealloc];  
  74. }  
  75.   
  76. @end  

 

示例图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值