表视图UITableView
使用沙盒获取图片和plist文件的路径,调用类方法:(NSString *)[[NSBundle mainbundle] pathForResource:(NSString *)ofType:];
接下来就是表视图UITableView;
手写的时候,需要先实例化一个tableView,(UITableView *)[[UITableView alloc] initWithFrame:(CGRect) style:(EnUITableViewStyle)];
UITableViewSytlePlain(简约风格)和UITableViewStyleGrouped(分组风格);
加载到父视图[self.view addSubview:(UITableView *)];
需要遵守两个协议:UITableViewDataResource和UITableViewDelegate
实现UITableViewDataSource下面两个必须实现的方法:
//设置行数
-(NSInteger)tableView:(UITableview *)tableView numberOfRowsInSection:(NSInteger)section;
//绘制单元格(cell)
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//单元格的复用,避免重复创建对象
static NSString *reuse=@“cell”;
//从队列里面实例化一个UITableViewCell的对象
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
if(cell==nil)
{
//实例化的时候,单元格的显示风格有四种:1.UITableViewCellStyleDefault 默认风格,仅显示一行文字
2.UITableViewCellStyleValue1 存在imageView,textLabel,detailTextLabel,细节描述在最右边
3.UITableViewValue2 不存在imageView
4.UITableViewSubtitle 存在imageView,textLabel,detailTextLabel,细节描述位于textLabel 的下方
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
}
cell.textLabel.text //设置单元格显示的内容
cell.imageView.image //设置单元格里面显示的图片内容
cell.detailTextlabel.text //设置细节描述
return cell;
}
需要设置代理和数据源【delegate,datasource】的对象,才能触发协议里面的方法;
还可以设置分组的组数:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
设置行高:
-(CGFloat )tableView :( UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 行高;
}
处理cell里面自带imageView的大小的方法:
cell.imageView.image = [UIImage imageNamed:@"光影.jpg"];
CGSize itemSize = CGSizeMake(80, 80);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
后面学习的UITableViewController,需要直接添加一个UITableViewController,然后新建一个UITableView的类,在新建的类的.m文件里面设置行数,设置组,绘制单元格,需要更改Controller的初始状态。