1.为tableView上添加图片,实现图片的拉大的效果
self.image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"6.jpg"]];
self.image.frame=CGRectMake(0, 0, self.view.frame.size.width, 200);
//给tableView添加头视图
//宽是tableview的宽度
//self.tableView.tableHeaderView=self.image;
//[self.tableView addSubview:self.image];
[self.tableView addSubview:self.image];
self.tableView.contentInset=UIEdgeInsetsMake(200, 0, 0, 0);
}
#pragma mark tableview的delegate已经签订好scrollview的协议,只要设置好代理人,就乐意使用scrollview的协议方法
只要滑动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//获取偏移量
CGFloat y=scrollView.contentOffset.y;
NSLog(@"%g",y);
if(y<0){
self.image.frame=CGRectMake(0, y, self.view.frame.size.width, -y);}}
2.tableView的界面传值
在从前往后传值的时候,直接传值.从后面往前传值的时候要用协议传值
写协议名,设定属性,方法,设置代理属性.
@property(nonatomic assign)id<协议名>delegate;
引头文件,签订代理人,执行方法
3.tableView的重新加载
一般在更新tableView时候会用到[tableView reloadData];
4.tableView有两个协议方法其中有关于数据的数据源有两个函数是必须实现的.一个是返回cell里面的row的数量
另一个常见cell.cell自带3个属性,两个Label和一个imageView
5,tableView可以实现自定义cell.
所谓的自定义cell就是自己升值cell上要放些什么视图.
#pragma mark 重写cell的初始化方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
//完成对属性视图的创建,但是一般创建之后不给属性视图frame
[self createView];
}return self;}
#pragma mark 属性视图进行创建
-(void)createView{
//创建左imageView
self.leftImageView=[[UIImageView alloc] init];
self.leftImageView.backgroundColor=[UIColor greenColor];
//添加
//cell上又一个专门用来现实空间的视图,叫contentView,我们把视图就放到contentView上进行显示
[self.contentView addSubview:self.leftImageView];
[_leftImageView release];
//上lable
self.upLable=[[UILabel alloc] init];
self.upLable.backgroundColor=[UIColor whiteColor];
[self.contentView addSubview:self.upLable];
[_upLable release];
//下lable
self.downLable=[[UILabel alloc] init];
self.downLable.backgroundColor=[UIColor cyanColor];
[self.contentView addSubview:self.downLable];
[_downLable release];
//右rightimageVIew
self.rightImageView=[[UIImageView alloc] init];
self.rightImageView.backgroundColor=[UIColor greenColor];
[self.contentView addSubview:self.rightImageView];
[_rightImageView release];
}
#pragma mark 这个方法是cell显示之前走的最后一个方法,一般会在这个方法里设置所有的属性视图的大小尺寸,这个方法会用在图片文字的自适应的设置上.
- (void)layoutSubviews{
//重写了父类的layoutSubviews方法,如果想要这个方法发挥正常功能,别忘了[super layoutSubviews];
[super layoutSubviews];
//对所有属性视图的位置和大小的设置
self.leftImageView.frame=CGRectMake(0, 0, WIGTH/3,HEIGHT);
self.upLable.frame=CGRectMake(WIGTH/3, 0, WIGTH/3, HEIGHT/2);
self.downLable.frame=CGRectMake(WIGTH/3, HEIGHT/2, WIGTH/3, HEIGHT/2);
self.rightImageView.frame=CGRectMake(WIGTH/3*2, 0, WIGTH/3, HEIGHT);}
- (void)dealloc{
[_rightImageView release];
[_leftImageView release];
[_upLable release];
[_downLable release];
[super dealloc];}
6.在tableView中解析数据的时候会遇到字典与数组互相嵌套的使用.一般我们解析的时候会把字典封装在一个model里面来筛选自己需要的属性,所谓的model就是新建一个了类,把自己需要从字典里拿到的属性,写成类的写属性.利用
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{}方法来过筛选掉不需要的属性
7.cell的自适应高度
简单来说就是应为cell上加载图片是,图片大小不好控制,可以用图片比例的方式来让图片自己适应cell的高度
#pragma mark 这个方法是tableView的delegate所提到的协议方法,只要是用来设置每一行高度的
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//根据图片的尺寸,设置行高
UIImage *image=[UIImage imageNamed:self.picArr[indexPath.row]];
//通过CGSize找到image里面图片的尺寸
CGSize picsize=image.size;
//计算行高
CGFloat rowheight=picsize.height*self.view.frame.size.width/picsize.width;
//计算lable的高度
//根据对应的文字求出lable显示的高度
NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
//根据文字的大小,计算出文本的尺寸
//还需要指定一个尺寸(375,0);
//第三个参数:计算高度需要依据字体的哪一个特征来确定
CGRect rect=[self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
//最后把结果作为返回值返回
return rowheight+rect.size.height;
然后在自定义的cell中实现设定图片的frame的位置
-(void)creatData{
self.myImage=[[UIImageView alloc] init];
self.myImage.backgroundColor=[UIColor yellowColor];
[self.contentView addSubview:self.myImage];
[_myImage release];
self.myLable=[[UILabel alloc] init];
//指定字体是14号,默认是17号
self.myLable.font=[UIFont systemFontOfSize:14];
//0是最大行
self.myLable.numberOfLines=0;
[self.contentView addSubview:self.myLable];
[_myLable release];}
-(void)layoutSubviews{
[super layoutSubviews];
//让imgaeview的尺寸和image的图片大小相同;
//因为这个是最后一个被执行的,所以执行到这个方法的时候,已经对cell的各个属性进行完赋值操作,所以可以通过imageview.image找到图片的尺寸.
CGSize picsize=self.myImage.image.size;
CGFloat height=picsize.height*WIGTH/picsize.width;
self.myImage.frame=CGRectMake(0, 0, WIGTH,height);
NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
//根据文字的大小,计算出文本的尺寸
//还需要指定一个尺寸(375,0);
//第三个参数:计算高度需要依据字体的哪一个特征来确定
CGRect rect=[self.myLable.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
self.myLable.frame=CGRectMake(0, height, WIGTH, rect.size.height);}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *newView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)] autorelease];
newView.backgroundColor=[UIColor cyanColor];
UILabel *lable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
NSMutableDictionary *dic=self.proArr[section];
lable.text=dic[@"proName"];
[newView addSubview:lable];
[lable release];
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(325, 0, 50, 20);
[button setTitle:@"更多" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:button];
return newView;}
-(void)click:(UIButton *)button{}
在tableView的编辑中
//self.navigationItem.rightBarButtonItem=self.editButtonItem;可以控制在导航视图器中右面的一个编辑按钮,在点击的时候可以变成done按钮
//直接打开tableview的可编辑模式
[self.table setEditing:YES animated:YES];
一般不采用上面的方法.我们一般会重写setEditing方法来实现打开table的可编辑模式#pragma mark 重写系统的编辑按钮点击触发方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.table setEditing:editing animated:YES];}
#pragma mark 设置哪些行可以进行编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row%2==1){
return YES;}
return NO;}
#pragma mark 有俩种方式,一个是插入,一个是删除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleInsert;}
//#pragma mark 删除数据
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle==UITableViewCellEditingStyleDelete){
//先删除数据源///一种方式
[self.arr removeObjectAtIndex:indexPath.row];
[self.table reloadData];
/
//通过tableView来删除上面的cell
//第一个参数:指定删除哪一个分区的哪一行,把他作为一个元素放在数组中
//第二个参数:删除动画
第二种方式
[self.arr removeObjectAtIndex:indexPath.row];
[self.table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}}
//#pragma mark 修改删除按钮的标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"点我啊";}
#pragma mark 这个方法是iOS8.0之后出现的方法,可以再编辑状态的时候多个按钮
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteaction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//先删除数据源
[self.arr removeObjectAtIndex:indexPath.row];
//通过tableView来删除上面的cell
//第一个参数:指定删除哪一个分区的哪一行,把他作为一个元素放在数组中
// //第二个参数:删除动画
[self.table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//按钮的点击所要触发的事件,都是写在block中
NSLog(@"触发了删除按钮");}];
deleteaction.backgroundColor=[UIColor cyanColor];
UITableViewRowAction *deleteaction1=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//按钮的点击所要触发的事件,都是写在block中
NSLog(@"触发了按钮");}];
deleteaction1.backgroundColor=[UIColor cyanColor];
return @[deleteaction,deleteaction1];}
#pragma mark 移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//1,先获取到起始位置的数据
NSString *str=[self.arr[sourceIndexPath.row] retain];
//2,把起始位置的对象从数据源中移除
[self.arr removeObjectAtIndex:sourceIndexPath.row];
//3.把数据插入到数组到数组的目的位置上去
[self.arr insertObject:str atIndex:destinationIndexPath.row];
[str release];}