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
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值