QQ - iPhone 风格的好友列表实现

自定义tableview每一个section的headView。然后接受触控事件刷新数据并显示

QQ - iPhone 风格的好友列表实现

    博客分类: 
  • Iphone
QQ
C代码   收藏代码
  1. //  
  2. //  QQstyleTableViewViewController.h  
  3. //  QQstyleTableView  
  4. //  
  5. //  Created by xhan on 9/22/09.  
  6. //  Copyright In-Blue 2009. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface QQstyleTableViewViewController : UIViewController < UITableViewDelegate , UITableViewDataSource , UIScrollViewDelegate > {  
  12.     UITableView* _tableView;  
  13.     NSMutableArray* _array;  
  14.     BOOL *flag;  
  15. }  
  16.   
  17. @property (nonatomic, retain) UITableView *tableView;  
  18.   
  19. - (int)numberOfRowsInSection:(NSInteger)section;  
  20.   
  21. @end  
 
C代码   收藏代码
  1. //  
  2. //  QQstyleTableViewViewController.m  
  3. //  QQstyleTableView  
  4. //  
  5. //  Created by xhan on 9/22/09.  
  6. //  Copyright In-Blue 2009. All rights reserved.  
  7. //  
  8.   
  9. #import "QQstyleTableViewViewController.h"  
  10.   
  11. @implementation QQstyleTableViewViewController  
  12.   
  13. @synthesize tableView = _tableView;  
  14.   
  15. ////////////////////////////////////////////////////////////////////////////////////////  
  16. // NSObject   
  17. - (void)dealloc {  
  18.     free(flag);  
  19.     [_tableView release], _tableView = nil;  
  20.     [super dealloc];  
  21. }  
  22.   
  23. - (void)viewDidLoad {  
  24.     [super viewDidLoad];  
  25.     _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds  style:UITableViewStylePlain];  
  26.     _tableView.delegate = self;  
  27.     _tableView.dataSource = self;  
  28.   
  29.     [self.view addSubview:_tableView];  
  30.     _array = [[NSMutableArray alloc] initWithObjects:[[NSArray alloc] initWithObjects:@"AA",@"BB",@"CC",@"DD",nil],  
  31.                                                      [[NSArray alloc] initWithObjects:@"EE",@"FF",@"GG",@"XX",@"ZZ",nil],     
  32.                                                      [[NSArray alloc] initWithObjects:@"JJ",@"VV",@"EE",@"NN",nil],  
  33.                                                      nil];  
  34.     flag = (BOOL*)malloc([_array count]*sizeof(BOOL*));  
  35.     memset(flag, NO, sizeof(flag));  
  36.   
  37. }  
  38.   
  39. ////////////////////////////////////////////////////////////////////////////////////////  
  40. //   
  41. #pragma mark Table view  delegate methods  
  42.   
  43. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  44.     return [_array count];  
  45. }  
  46.   
  47.   
  48.   
  49. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  50.     return [self numberOfRowsInSection:section];  
  51. }  
  52.   
  53. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  54. {  
  55.     static NSString *CellIdentifier = @"CellIdentifier";  
  56.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  57.     if (cell == nil) {  
  58.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  59.         cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  60.     }  
  61.     NSString* str = [[_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];  
  62.     cell.textLabel.text = str;  
  63.     return cell;  
  64. }  
  65.   
  66.   
  67.   
  68. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  69. {  
  70.     UIButton *abtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  71. //  abtn.frame = CGRectMake(0, 0, 200, 48);  
  72. //  abtn.titleLabel.text = @"HEADER";  
  73.     abtn.tag = section;  
  74.     [abtn addTarget:self action:@selector(headerClicked:) forControlEvents:UIControlEventTouchUpInside];  
  75.     return abtn;  
  76. }  
  77.   
  78. ////////////////////////////////////////////////////////////////////////////////////////  
  79. //   
  80. -(void)headerClicked:(id)sender  
  81. {  
  82.     int sectionIndex = ((UIButton*)sender).tag;  
  83.     flag[sectionIndex] = !flag[sectionIndex];  
  84.     [_tableView reloadData];  
  85. }  
  86.   
  87. - (int)numberOfRowsInSection:(NSInteger)section  
  88. {  
  89.     if (flag[section]) {  
  90.         return [(NSArray*)[_array objectAtIndex:section] count];  
  91.     }  
  92.     else {  
  93.         return 0;  
  94.     }  
  95. }  
  96.   
  97. @end 

你可能感兴趣的:(职场,iPhone,风格,休闲)