iphone开发之UITableView的使用

每天学习的东西,我希望以这样的方式来记录自己的成长,同时提供一个回顾的地方。同时提供初学者一个查询资料的来源,技术大牛们的赐教的机会。好了,我今天在学习如何使用UITableView,在IPhone开发中UITableView是一个很常用的对象,它提供了两种不同的样式UITableViewStyleDefault与UITableViewStyleGrouped。好了先贴一段代码,然后细细解析!

#import "SystemSettingViewController.h"


#import "addSensorViewController.h"
#import "addChannelViewController.h"


@implementation SystemSettingViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


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


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


#pragma mark - View lifecycle


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    bgView.backgroundColor = [UIColor greenColor];
    
    NSArray *array1 = [[NSArray alloc] initWithObjects:NSLocalizedString(@"添加回路执行器", nil), NSLocalizedString(@"添加开关或红外", nil), NSLocalizedString(@"添加窗帘", nil), nil];
    
    NSArray *array2 = [[NSArray alloc] initWithObjects:NSLocalizedString(@"更改回路执行器的名称", nil), NSLocalizedString(@"更改开关或红外的名称", nil), nil];
    
    NSArray *array3 = [[NSArray alloc] initWithObjects:NSLocalizedString(@"删除回路执行器", nil), NSLocalizedString(@"删除开关或红外", nil), nil];
    
    addOperate = [[NSArray alloc] initWithArray:array1];
    editOperate = [[NSArray alloc] initWithArray:array2];
    deleteOperate = [[NSArray alloc] initWithArray:array3];
    [array1 release];
    [array2 release];
    [array3 release];
    
    //UITableViewStyleDefault
    //UITableViewStyleGrouped
    
    contentTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStyleGrouped];
    contentTable.delegate = self;
    contentTable.dataSource = self;
    contentTable.backgroundColor = [UIColor clearColor];
    
    [bgView addSubview:contentTable];
    
    [self.view addSubview:bgView];
    [bgView release];
}




- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma UITableView


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell) {
        cell = nil;
    }
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = [addOperate objectAtIndex:indexPath.row];
            break;
            
        case 1:
            cell.textLabel.text = [editOperate objectAtIndex:indexPath.row];
            break;
            
        case 2:
            cell.textLabel.text = [deleteOperate objectAtIndex:indexPath.row];
            break;
            
        default:
            break;
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                {
                    addChannelViewController *addChannelController = [[addChannelViewController alloc] init];
                    [self.navigationController pushViewController:addChannelController animated:YES];
                }                 
                    break;
                    
                case 1:
                {
                    addSensorViewController *addSensorController = [[addSensorViewController alloc] init];
                    [self.navigationController pushViewController:addSensorController animated:YES];
                }
                    break;
                    
                default:
                    break;
            }
            break;
            
        case 1:
            
            break;
            
        case 2:
            
            break;
            
        default:
            break;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rows = 0;
    switch (section) {
        case 0:
            rows = [addOperate count];
            break;
            
        case 1:
            rows = [editOperate count];
            break;
            
        case 2:
            rows = [deleteOperate count];
            break;
            
        default:
            rows = 0;
            break;
    }
    return rows;
}


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




@end


1、不知道大家注意到没有,在ViewDidLoad函数中有一个contentTable,再说在该文件的头文件中定义的一个UITableView变量。创建UITableView的代码如下:

contentTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStyleGrouped];
     contentTable.delegate = self;
     contentTable.dataSource = self;
     contentTable.backgroundColor = [UIColor clearColor];

delegate属性UITableView的重要属性之一,如果要使用UITableView的方法操作表数据的话,必须在头文件中引用<UITableViewDelegate>。

dataSource属性同样比较重要,用于指定UITableView的数据源,需要在头文件中引用<UITableViewDataSource>


2、-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath该函数用于绘制表的单元格,其中最重要的是UITableViewCell对象,它包括了一下几个属性:

textLabel 对象,cell.textLabel调用。其中的text用于指定单元格的文本,同时还包括font用于指定单元格的字体;textAlignment用于指定对齐方式;textColor用于指定单元格文本颜色

imageView对象,用于为单元格指定图像

accessoryType对象,用于指定单元格的样式,其中包括UITableViewCellAccessoryTNone(默认样式)、UITableViewCellAccessoryCheckmark(校验表样式)、UITableViewCellAccessoryDisclosureIndicator(扩展表样式)


3、-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 该函数是在选择表的行是触发

4、-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 返回当前表的分组数

5、-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 返回指定分组的行数


UITableView还有很多东西可以使用,我的这篇文章给初学者起到一个入门的作用,同时方便自己以后查阅。请各位大牛们多多指教!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值