UITableView的简单实现

    //设置主页面

    self.flowColorTableViewController = [[[FlowColorTableViewController alloc] initWithNibName:@"FlowColorTableViewController" bundle:nil] autorelease];

    self.window.rootViewController = self.flowColorTableViewController;

 

//---常量的定义---

//分区数
#define sectionCount 2
//系统分区0
#define sysSetSection 0
//个人分区1
#define personSection 1



#import "FlowColorTableViewController.h"

@interface FlowColorTableViewController ()

@end

@implementation FlowColorTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
	[sysSetArray release];
	[personSetArray release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化数组
    sysSetArray = [[NSMutableArray alloc] 
				  initWithObjects:@"IP 设置",@"主页样式",@"软件初始化",nil];
	personSetArray = [[NSMutableArray alloc] 
				   initWithObjects:@"自动登陆",@"记住密码",@"主题设置",nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
   
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return sectionCount;
}

//返回给定分区显示的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
		case sysSetSection:
			return [sysSetArray count];
		case personSection:
			return [personSetArray count];
		default:
			return 0;
	}
}

//返回给定分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case sysSetSection:
            return @"系统设置";
        case personSection:
            return @"个人设置";
        default:
            return @"Unknown";
    }
}

//返回指定分区和行对应的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
	//寻找已分配且可用的单元格
	UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	//如何为空,就分配并初始化一个单元格
    if(cell == nil) 
	{
		cell = [[[UITableViewCell alloc] 
				 initWithFrame:CGRectZero 
				 reuseIdentifier:CellIdentifier] autorelease];
	}
	//根据indexPath提供的信息,设置单元格的格式
	switch (indexPath.section) {//根据分区进行设置
		case sysSetSection://系统设置分区
            //单元格对象 setText 值是来自于数组
			[[cell textLabel] setText:[sysSetArray objectAtIndex:indexPath.row]];
			break;
		case personSection://个人设置分区
			[[cell textLabel] setText:[personSetArray objectAtIndex:indexPath.row]];
			break;
		default:
			[[cell textLabel] 
			 setText:@"Unknown"];
	}
	return cell;

}


//表行点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *showMsgAlert;
	NSString	*showMsg;
	switch (indexPath.section) {
		case sysSetSection:
			showMsg=[[NSString alloc] 
						   initWithFormat:
						   @"你点击了,系统设置【 %@ 】项",
						   [sysSetArray objectAtIndex: indexPath.row]];
			break;
		case personSection:
			showMsg=[[NSString alloc] 
						   initWithFormat:
						   @"你点击了,个人设置【 %@ 】项",
						   [personSetArray objectAtIndex: indexPath.row]];
			break;
		default:
			showMsg=[[NSString alloc] 
						   initWithFormat:
						   @"unknown"];
			break;
	}
	
	showMsgAlert = [[UIAlertView alloc] 
					 initWithTitle: @"Flower Selected" 
					 message:showMsg 
					 delegate: nil 
					 cancelButtonTitle: @"Ok" 
					 otherButtonTitles: nil];
	[showMsgAlert show];
	[showMsgAlert release];
	[showMsg release];
}

@end
 

你可能感兴趣的:(UITableView)