标签:ios uitableviewcontrolle 悬浮按钮
在IOS中,UITableViewController不如UIViewController用的方便,遇到了一个需求:在TableView中添加一个悬浮按钮,不随TableView滑动而滑动。这个需求在UIViewController里面很好实现,给self.view 添加子视图,再把子视图放到最上方即可。可是在表视图控制器中,就很难办,因为控制器中没有作为tableView的父视图的view存在,而把button作为tableView的子视图出现呢,则会随着table的滑动而滑动(⊙﹏⊙b汗,搞了好久都搞不定)。不过终于搞定了这个问题。本文原创,转载请注明出处:http://blog.csdn.net/zhenggaoxing/article/details/44857765
先看看效果吧(先放个蛋糕)
// 添加悬浮按钮 flowButton=[[UIButton alloc]initWithFrame:CGRectMake(200, 200, 30, 30) ]; [flowButton setImage:[UIImage imageNamed:@"flowButton"] forState:UIControlStateNormal]; [self.tableView addSubview:flowButton]; [self.tableView bringSubviewToFront:flowButton]; buttonY=(int)flowButton.frame.origin.y;
Moves the specified subview so that it appears on top of its siblings. |
|||
|
This method moves the specified view to the end of the array of views in the subviews property. |
||
Parameters |
|
||
Availability |
iOS (2.0 and later) |
-(void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"%d",(int)flowButton.frame.origin.y); flowButton.frame = CGRectMake(flowButton.frame.origin.x, buttonY+self.tableView.contentOffset.y , flowButton.frame.size.width, flowButton.frame.size.height); }
// // CommonTableViewController.m // IOS table1 type // // Created by h92 on 15/1/6. // Copyright (c) 2015年 李腾飞. All rights reserved.
// <span style="font-size:14px;">本文原创,转载请注明出处:</span><span style="font-size:14px;">http://blog.csdn.net/zhenggaoxing/article/details/44857765</span> // #import "CommonTableViewController.h" #import "DefineA.h" @interface CommonTableViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate> @end @implementation CommonTableViewController @synthesize dataList; @synthesize table; @synthesize flowButton; - (void)viewDidLoad { [super viewDidLoad]; [self initView]; } /*--------------------------------------初始化---------------------------------------*/ -(void)initView { [self setup]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)setup { // 添加悬浮按钮 flowButton=[[UIButton alloc]initWithFrame:CGRectMake(200, 200, 30, 30) ]; [flowButton setImage:[UIImage imageNamed:@"flowButton"] forState:UIControlStateNormal]; [self.tableView addSubview:flowButton]; [self.tableView bringSubviewToFront:flowButton]; buttonY=(int)flowButton.frame.origin.y; // 获取文件路径 NSBundle *bundle=[NSBundle mainBundle]; NSString *plist=[bundle pathForResource:@"team" ofType:@"plist"]; // 读取文件数据(nsdictionary 类型) dataList=[[NSMutableArray alloc] initWithContentsOfFile:plist]; // 设置title self.title=COMMOMTABLETITLE; } #pragma mark-dataSource method /*--------------------------------------班级点名---------------------------------------*/ // 第几组有几个人 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataList count]; } // 他们都叫什么 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 确定Cell标识 static NSString *cellIdentifier=@"Cell"; // 复用 Cell UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(cell==nil){ // UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier]; // UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; } cell.detailTextLabel.text=@"这就是副标题"; // 球队名称 cell.textLabel.text=[[dataList objectAtIndex:indexPath.row] objectForKey:@"name"]; // 根据plist文件生成 字符串 NSString *imagePath=[[[dataList objectAtIndex:indexPath.row]objectForKey:@"image"] stringByAppendingString:@".png"]; // 根据字符串加载图片 cell.imageView.image=[UIImage imageNamed:imagePath]; // cell.accessoryType=UITableViewCellAccessoryNone; cell.accessoryType=UITableViewCellAccessoryCheckmark; // cell.accessoryType=UITableViewCellAccessoryDetailButton; // cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton; // cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"%d",(int)flowButton.frame.origin.y); flowButton.frame = CGRectMake(flowButton.frame.origin.x, buttonY+self.tableView.contentOffset.y , flowButton.frame.size.width, flowButton.frame.size.height); } @end
IOS学习之——表视图 给tableViewController添加悬浮窗口
标签:ios uitableviewcontrolle 悬浮按钮
原文地址:http://blog.csdn.net/zhenggaoxing/article/details/44857765