可伸缩的表视图

头文件:

 

#import <UIKit/UIKit.h>

@interface TableMenuViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
	NSMutableArray *itemArray;
	NSMutableArray *openItemArray;
	UITableView *menuTable;
}

@property (nonatomic, retain) NSMutableArray *itemArray;
@property (nonatomic, retain) NSMutableArray *openItemArray;
@property (nonatomic, retain) IBOutlet UITableView *menuTable;

- (void)readPlistToArray;

@end

 

实现文件:

 

#import "TableMenuViewController.h"

@implementation TableMenuViewController

@synthesize itemArray, openItemArray, menuTable;

- (void)viewDidLoad {
	[super viewDidLoad];
	self.itemArray = [NSMutableArray arrayWithCapacity:0];
	self.openItemArray = [NSMutableArray arrayWithCapacity:0];
	[self readPlistToArray];
}

- (void)readPlistToArray {
	if ([self.itemArray count]) {
		[self.itemArray removeAllObjects];
	}
	
	NSString *path = [[NSBundle mainBundle] pathForResource:@"MenuOrder" ofType:@"plist"];
	NSArray *array = [NSArray arrayWithContentsOfFile:path];
	
	for (int i = 0; i < [array count]; i++) {
		NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
		[dic setObject:@"0" forKey:@"level"];
		[dic setObject:[[array objectAtIndex:i] objectAtIndex:0] forKey:@"name"];
		[self.itemArray addObject:dic];
		
		for (int j = 0; j < [self.openItemArray count]; j++) {
			if ([[[self.openItemArray objectAtIndex:j] objectForKey:@"name"] 
					 isEqualToString:[[array objectAtIndex:i] objectAtIndex:0]]) {
				NSArray *ary = [array objectAtIndex:i];
				for (int k = 1; k < [ary count]; k++) {
					NSMutableDictionary *menuDic = [NSMutableDictionary dictionaryWithCapacity:0];
					[menuDic setObject:@"2" forKey:@"level"];
					[menuDic setObject:[ary objectAtIndex:k] forKey:@"name"];					
					[self.itemArray addObject:menuDic];	
				}
			}
		}
	}
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.itemArray count];
}

- (NSInteger)tableView:(UITableView *)tableView 
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return [[[self.itemArray objectAtIndex:[indexPath row]] objectForKey:@"level"] intValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {	
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}
	
	NSDictionary *dic = [self.itemArray objectAtIndex:[indexPath row]];
	
	if(![[dic objectForKey:@"level"] intValue])
	{
		cell.selectionStyle = UITableViewCellSelectionStyleNone;		
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menubackgroud.png"]];
		[cell setBackgroundView:imageView];		
	}
	else {
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"itembackgroud.png"]];
		[cell setBackgroundView:imageView];		
	}
	cell.textLabel.text = [dic objectForKey:@"name"];
	
	return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSDictionary *dic = [self.itemArray objectAtIndex:[indexPath row]];
	NSString *name = [dic objectForKey:@"name"];
	
	if(![[dic objectForKey:@"level"] intValue])
	{
		for (int i = 0; i < [self.openItemArray count]; i++) {
			if ([[[self.openItemArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:name]) {
				[self.openItemArray removeObjectAtIndex:i];
				[self readPlistToArray];
				[self.menuTable reloadData];
				
				return;
			}
		}
		[self.openItemArray addObject:dic];
		[self readPlistToArray];
		[self.menuTable reloadData];
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:(@"%@", name) delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 44;
}

- (void)dealloc {
	[itemArray release];
	[openItemArray release];
	[menuTable release];
	
	[super dealloc];
}

@end

 

示例图:


可伸缩的表视图

你可能感兴趣的:(ios,iPhone,表视图,可伸缩)