UITableView的用法

UITableView这个控件可以说是最常用的控件了,由于刚开始学习IOS找了好多资料,才搞明白他的基本用法:

UITableView本身要比UIButton 、UILabel等控件要稍微复杂一点

在这里是拖的控件,也就是UITableView控件。

1.如果是拖得控件,那么就要把dataSource和delegate 通过File's owner 使他们之间能够进行想联系,也就是要她们通过拖拽与File's owner进行连接.

2.要定义自己的类:@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (strong,nonatomic)NSArray *list;

@end

3.其他的实现方式都是在.m文件中实现的:

(1).例如:在-(void)ViewDidLoad{

}函数里实现添加数据.

(2).在-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   return  [self.list count];

}想这个函数,是用来返回你添加的数据的个数,也就是对象的个数。

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

{

在里面你可以实现层的复用等例如:

 static NSString *str=@"Sameide";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];//在这里就是实现复用方法.

   if(cell==nil)

   {

       cell=[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:str];

   }

    NSUInteger row=[indexPath row];

    cell.textLabel.text=[self.list objectAtIndex:row];

    UIImage *image=[UIImage imageNamed:@"123.jpg"];

    cell.imageView.image=image;

    UIImage *hig=[UIImage imageNamed:@"youdao"];

    cell.imageView.highlightedImage=hig;

     cell.detailTextLabel.text=@"hello";

    return cell;

}

这个函数的作用就是用来返回每个cell的数据,要注意index的用法以及他包含的内容.


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

{

NSString *str=[self.list objectAtIndex:[indexPath row]];

    UIAlertView *alter=[[UIAlertView alloc] initWithTitle:@"选中行信息" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

       [alter show];


}在这个函数里实现的是cell上的点击事件。当你点击cell的时候会显示一些相关的信息.

当然这里也可以用代码实现,今天就先到这里了,希望对你们有帮助.



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值