IOS學習之——表視圖 給tableViewController添加懸浮視窗

來源:互聯網
上載者:User

標籤:ios   uitableviewcontrolle   懸浮按鈕   

前言

在IOS中,UITableViewController不如UIViewController用的方便,遇到了一個需求:在TableView中添加一個懸浮按鈕,不隨TableView滑動而滑動。這個需求在UIViewController裡面很好實現,給self.view 添加子視圖,再把子視圖放到最上方即可。可是在表視圖控制器中,就很難辦,因為控制器中沒有作為tableView的父視圖的view存在,而把button作為tableView的子視圖出現呢,則會隨著table的滑動而滑動(⊙﹏⊙b汗,搞了好久都搞不定)。不過終於搞定了這個問題。本文原創,轉載請註明出處:http://blog.csdn.net/zhenggaoxing/article/details/44857765

先看看效果吧(先放個蛋糕)



實現我們的方案是把button添加為tableVIew的子視圖,然後隨著table的滑動,動態改變button的高度,實現效果上的“固定”。
首先,初始化button,添加為tableView的子視圖,並且放到tableView上方
//  添加懸浮按鈕    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;

這裡說以下 bringSubviewToFront,就是把子視圖移動到頂部的意思,相應的有bringSubviewToBack。看一下官網描述:

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 thesubviews property.

Parameters

view

The subview to move to the front.

Availability

iOS (2.0 and later)


翻譯一下:將指定的子視圖移動到它兄弟視圖的頂部,這個方法將指定的視圖移動到子視圖屬性數組的尾部。
到這裡,其實懸浮的button已經出現了,接下來就要考慮如何?固定了。首先,看一下UITableView的繼承關係
tableVIew是繼承自UIScrollView的,所以這裡我們通過<UIScrollViewDelegate> 的-(void)scrollViewDidScroll:(UIScrollView *)scrollView 方法和UIScrollView的contentOffset屬性來動態調整button的Y值,以實現視覺上的“固定”。看代碼:
-(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 *[email protected]"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];    }    [email protected]"這就是副標題";        // 球隊名稱    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

原始碼:https://git.oschina.net/zhengaoxing/table1-type本文原創,轉載請註明出處:http://blog.csdn.net/zhenggaoxing/article/details/44857765





IOS學習之——表視圖 給tableViewController添加懸浮視窗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.