Iphone表实例应用

来源:互联网 发布:恒大海上威尼斯 知乎 编辑:程序博客网 时间:2024/05/04 11:08

//

//  Simple_TableViewController.m

//  Simple Table

//

//  Created by sinnMac05 on 3/30/10.

//  Copyright sinn 2010. All rights reserved.

//

 

#import "Simple_TableViewController.h"

 

@implementation Simple_TableViewController

@synthesize listData;

@synthesize images;

 

 

/*

// The designated initializer. Override to perform setup that is required before the view is loaded.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        // Custom initialization

    }

    return self;

}

*/

 

/*

// 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];

}

*/

 

 

/*

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

*/

 

- (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.

}

 

- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

 

 

- (void)dealloc {

    [super dealloc];

}

 

-(void)viewDidLoad{

 

NSArray *imageNames=[[NSArray alloc] initWithObjects:@"u=1055417007,3623440744&fm=0&gp=0.jpg",

@"u=1023570728,1484262426&fm=0&gp=0.jpg",

@"u=1477798512,22194711&fm=0&gp=0.jpg",

@"u=3788263137,3792757984&fm=0&gp=0.jpg",nil];

NSArray *array=[[NSArray alloc] initWithObjects:@"王秋磊  13012345678",@"赵齐奎  13111111111",@"倪雨成  15888888888",@"王建刚  13855555555",nil];

//向表视图传递一个数组

self.listData=array;

self.images=imageNames;

//self.images=newImages;

     [imageNames release];

[array release];

[super viewDidLoad];

}

 

/*

 tableview方法的作用是在当前视图中显示多少行

tableView 表视图,section表示第几个分区

 */

 

 

 

-(NSInteger)tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger)section{

 

//分区中的行数是数组中数组项的数目。

return [self.listData count];

}

 

 

 

/*

 下面的方法是获得一行,

 tableView 是对所请求的表的引用,

 NSIndexPath类可以将分区和行绑定到一个对象中,如果xiang

 */

 

 

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath

{

//充当表示某种表单元的键,在此表中只创建了一个单元,因此只定义一种标识符。

static NSString *simpleTableIdentifier=@"SimpleTable";

//根据simpleTableIdentifier标识符初始化,UITableViewCelldequeueReusableWithIdentifier返回一个

//可重复使用的表视图单元,改单元格位于标识赋simpleTableIdentifier

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:

  simpleTableIdentifier];

//判断可重复使用的表单元是否为空,

if(cell==nil){

cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:

  simpleTableIdentifier]autorelease];

}

//通过NSIndexPath知道表视图需要显示第几行

NSInteger row=[indexPath row];

//获得所显示行的数据,而后将数据给cell.textLable

cell.textLabel.text=[listData objectAtIndex:row];

/*

得到图片并显示在当前行

*/

UIImage *image=[UIImage imageNamed:[images objectAtIndex:row]];

 

cell.imageView.image=image;

[image release];

return cell;

}

 

/*

 获得当前所选择的行,如果第一行被选中,其索引始终为零,那么他将返回nil,表示实际上没有行被选中。

 */

 

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSUInteger row=[indexPath row];

if(row==0)

return nil;

return indexPath;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//得到当前所选择的行

NSUInteger row=[indexPath row];

//通过行,得到数据

NSString *rowValue=[listData objectAtIndex:row];

NSString *messages=[[NSString alloc] initWithFormat:@"用户: %@/n",rowValue];

//设置警告对话框

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:messages message:@"Row selected" delegate:nil cancelButtonTitle:@"Yes I Did" otherButtonTitles:nil];

 

[alert show];

[messages release];

[alert release];

}

 

 

@end

原创粉丝点击