IOS5基础之十-----表视图

一,一个简单的表视图

新建一个项目使用Single View Application模板并且命名为Simple Table。

找到xib文件,将库中的Table View 拖入到xib中,并且将连接检查器中的datasource和Delegate 关联到File‘s Owner上。保存这样页面的基本完成。

在头文件中添加委托和资源。

#import<UIKit/UIKit.h>


@interface BIDViewController :UIViewController

<UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic)NSArray *listData;


@end


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

   NSArray *array =[[NSArrayalloc]initWithObjects:@"sleepy",@"Sneezy",@"Bashful",@"Happy",@"Doc",

   @"Grumpy",@"Dopey",@"Thorin",@"Dorin",@"Nori",@"Ori",@"Balin",@"Dwalin",@"Fili",@"Kili",@"Oin",@"Gloin",@"Bifur",@"Bofur",@"Bomfur",nil];

    self.listData=array;

}


- (void)viewDidUnload

{

    [superviewDidUnload];

   // Release any retained subviews of the main view.

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

    self.listData =nil;

}


#pragma mark

#pragma mark Table View Data Source Methods

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

{

    //该方法来查看指定分区中的行数。

   return [self.listDatacount];

}


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

{

    static NSString *SimpleTableIdentifier=@"SimapleTableIdentifier";//此字符串充当表示某种表单元的键。

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell==nil)

    {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:SimpleTableIdentifier];

    }

    NSUInteger row =[indexPath row];

    cell.textLabel.text=[listDataobjectAtIndex:row];

     return cell;

}

简单一个表视图就完成了。

玩了两天,今天开始学习,努力中!

现在为每个单元格添加图像。

也就是增加了一句话。

 UIImage *image=[UIImageimageNamed:@"star2.png"];

 cell.imageView.image=image;

每个单元格都有imageView和highlightedImage属性。

如何设置单元格的详细文本标签。修改为:

cell=[[UITableViewCellallocinitWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:SimpleTableIdentifier];


cell=[[UITableViewCellallocinitWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SimpleTableIdentifier];

cell=[[UITableViewCellallocinitWithStyle:UITableViewCellStyleValue2reuseIdentifier:SimpleTableIdentifier];



设置缩进级别

#pragma mark

#pragma mark Table Delegate Methods

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSInteger row =[indexPath row];

    return row;

}


处理行的选择

选中第一行不做任何操作

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

{

    NSInteger row =[indexPath row];

    NSString *rowValue=[listData objectAtIndex:row];

    

    NSString *message= [[NSString allocinitWithFormat:@"You Selected %@",rowValue];

    UIAlertView *alert= [[UIAlertView allocinitWithTitle:@"Row Selected" message:message delegate:nil cancelButtonTitle:@"Yes I did"otherButtonTitles:nil];

    [alert show];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


更改字体大小和行高

cell.textLabel.font=[UIFont boldSystemFontOfSize:50];

根据委托来修改行高

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 70;

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值