IOS 之TableView

.h

#import <UIKit/UIKit.h>

@interface YYWViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) NSArray *listData;

@end

.m

#import "YYWViewController.h"

@interface YYWViewController ()

@end

@implementation YYWViewController




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self addTableView];
    
}

//添加tableview
-(void) addTableView
{
    UITableView *tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
    [tableview setDelegate:self];
    [tableview setDataSource:self];
    [self.view addSubview: tableview];
    [tableview release];
    NSArray *array=[[NSArray alloc]initWithObjects:@"Apple",@"Orange",@"Banana",@"Pear",@"Mango",@"Grape",@"Strawberry",@"Watermelon",@"Cantaloupe", nil];
    _listData=array;
    
}

//很多关于tableview的方法全是托管,就跟.net中的很多事件一样
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //返回行数
    return [self.listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //cell是可以重复使用的,创建的key就是在创建cell的时候能够成为某种标识,这样程序就会自动去搜索带有这种标识的key,完成创建
    static NSString *SimpleTableIdentifier = @"SimlpeTableIdentifier";
    //寻找带有某种标识的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    //如果没有找到,就执行创建,并且新创建的cell也带有这种key
    if(cell == nil) {
        //默认的风格UITableViewCellStyleDefault
        //带有子标题的风格UITableViewCellStyleSubtitle
        //cell = [[UITableViewCell alloc]
              //  initWithStyle:UITableViewCellStyleDefault
               // reuseIdentifier:SimpleTableIdentifier];
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:SimpleTableIdentifier];
    }
    //获取行数
    NSUInteger row = [indexPath row];
    //设置cell的数据
    cell.textLabel.text = [_listData objectAtIndex:row];
    //设置字体大小
    cell.textLabel.font = [UIFont boldSystemFontOfSize:30];
    //cell.textLabel.font = [UIFont systemFontOfSize:20];
    UIImage *image=[UIImage imageNamed:@"star.png"];
    //正常情况下显示的图片
    cell.imageView.image=image;
    //选中之后显示的图片
    cell.imageView.highlightedImage=[UIImage imageNamed:@"star2.gif"];
    //显示的是二级标题
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@ is a good fruit,you can not miss it",[_listData objectAtIndex:row]];
    
    return cell;
}

//选中之前进行处理
- (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 *message = [[NSString alloc] initWithFormat:@"You selected %@", rowValue];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected!"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"Yes I Did"
                                          otherButtonTitles:nil];
    [alert show];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

 自定义cell样式,详情参见:http://www.cnblogs.com/minglz/archive/2013/01/12/2857720.html

posted @ 2013-04-23 16:48  Peter_youny  阅读(185)  评论(0编辑  收藏  举报