QQ风格的列表

头文件:

 

#import <UIKit/UIKit.h>

@interface QQstyleViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
	
	UITableView *table;   
	NSMutableArray *array;   
	BOOL *flag;   
	
}   

@property (nonatomic, retain) UITableView *table;  

@end

 

实现文件:

 

#import "QQstyleViewController.h"

@implementation QQstyleViewController

@synthesize table; 

- (void)viewDidLoad {
	[super viewDidLoad];
	table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];   
	table.delegate = self;   
	table.dataSource = self;  
	[self.view addSubview:table]; 
  NSArray *ary1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
	NSArray *ary2 = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
	NSArray *ary3 = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", nil];
	array = [[NSMutableArray alloc] initWithObjects:ary1, ary2, ary3, nil];  
	[ary1 release];
	[ary2 release];
	[ary3 release];
	flag = (BOOL *) malloc([array count] * sizeof(BOOL *));   
	memset(flag, NO, sizeof(flag));
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {   
	return [array count];   
}   

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   
	if (flag[section]) {   
		return [(NSArray *)[array objectAtIndex:section] count];   
	}   
	else {   
		return 0;   
	}   
}   

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{   
	static NSString *CellIdentifier = @"CellIdentifier";   
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   
	if (cell == nil) {   
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   
		cell.selectionStyle = UITableViewCellSelectionStyleNone;   
	}   
	NSString *str = [[array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];   
	cell.textLabel.text = str;   
	return cell;   
}   

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   
{   
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
	btn.tag = section;   
	[btn addTarget:self action:@selector(headerClicked:) forControlEvents:UIControlEventTouchUpInside];   
	return btn;   
}   

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	return 30;
}

-(void)headerClicked:(id)sender   
{   
	int sectionIndex = ((UIButton *)sender).tag;   
	flag[sectionIndex] = !flag[sectionIndex];   
	[table reloadData];   
}   

- (void)dealloc {
	free(flag);  
	[array release];
	[table release];  
	[super dealloc];
}

@end

 

示例图:


QQ风格的列表

你可能感兴趣的:(ios,qq,iPhone)