iPhone开发之UITableView简单应用(5)

本文指导您如何使用Objective-C语言构建一个简单的应用程序,该应用包含一个表格视图,用于展示预设的数据集,并实现数据选择的弹出提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1.单击菜单File->New->Proget.....新建一个项目:

   

   2.选择Single View Application模板,单击Next:

   

   3.将项目命名为Simple Table,单击Next->Create,新建项目:

   

   4.单击ViewController.xib文件,在View视图上添加控件Table View:

  

  5.为控件Table View添加数据、方法委托:

  

 

  6.单击ViewController.h头文件,为ViewController添加数据成员:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
   NSArray *list;
}
@property(nonatomic,retain)NSArray *list;

@end


  7.单击ViewController.m文件,为ViewController添加数据和协议方法:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize list;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    NSArray *array = [[NSArray alloc]initWithObjects:@"广东",@"湖南",@"北京",@"上海",@"香港",@"澳门", nil];
    self.list = array;
    [array release];
}

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

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

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

#pragma mark -
#pragma mark Table Data Source Methods

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identfier=@"placetable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfier];
    }
    NSInteger row = [indexPath row];
    cell.textLabel.text = [list objectAtIndex:row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSString *message = [list objectAtIndex:row];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"你选择:" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

@end

  8.现在可以单击运行按钮,测试程序,单击其中的选项“广东”,效果如下:

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值