Objective
In iOS, Uitableviewcontroller is not as convenient as Uiviewcontroller, and has encountered a need to add a hover button in TableView without sliding with the tableview. This requirement is well implemented in Uiviewcontroller, add a sub-view to Self.view, and then put the view to the top. But in the table view controller, it is difficult to do, because the controller does not exist as the parent view of TableView, and the button as a sub-view of the TableView appear, it will slide with the table sliding (⊙﹏⊙ b Khan, for a long time have been confused). But it finally took care of the problem. This article original, reproduced please indicate the source: http://blog.csdn.net/zhenggaoxing/article/details/44857765
Look at the effect first (put a cake first)
RealizeOur solution is to add the button as a sub-view of the TableView, and then dynamically change the height of the button as the table slides to achieve a "fixed" effect.
First, initialize the button, add the child view as TableView, and put it above TableView
Add hover button Flowbutton=[[uibutton alloc]initwithframe:cgrectmake ($, $, ())]; [Flowbutton setimage:[uiimage imagenamed:@ "Flowbutton"] forstate:uicontrolstatenormal]; [Self.tableview Addsubview:flowbutton]; [Self.tableview Bringsubviewtofront:flowbutton]; buttony= (int) flowbutton.frame.origin.y;
here said the following Bringsubviewtofront, is the handle view to move to the top of the meaning, corresponding to the bringsubviewtoback. Look at the official website description:
|
Moves the specified Subview so, it appears on top of its siblings. |
|
|
This method moves the specified view to the end of the array of thesubviews property. |
|
Parameters |
|
View |
The subview to move to the front. |
|
|
Availability |
IOS (2.0 and later) |
To move the specified child view to the top of its sibling view, this method moves the specified view to the end of the Child View property array.
here, in fact, the hover button has appeared, the next thing to consider how to achieve the fixed. First, take a look at the inheritance relationship of UITableView
TableView is inherited from the Uiscrollview,So here we are through <uiscrollviewdelegate> 's-(void) Scrollviewdidscroll: (Uiscrollview*) ScrollView method and UiscrollviewThe Contentoffset property dynamically adjusts the Y value of the button to achieve a visually "fixed". look at the code:
-(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);}
Full code:
commontableviewcontroller.m// IOS table1 type//// Created by h92 on 15/1/6.// Copyright (c) 2015 Li Fei. All rights reserved.
<span style= "FONT-SIZE:14PX;" > This article original, reprint please indicate source: </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];} /*--------------------------------------initialize---------------------------------------*/-(void) initview{[self setup ];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(void) setup{//add hover button 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; Get file path NSBundle *bundle=[nsbundle Mainbundle]; NSString *plist=[bundle pathforresource:@ "Team" oftype:@ "plist"]; Read file data (nsdictionary type) Datalist=[[nsmutablearray alloc] initwithcontentsoffile:plist]; Set title Self.title=commomtabletitle;} #pragma mark-datasource method/*-------------------------------------- Class name---------------------------------------*///group Several people-(Nsinteger) TableView: (UITableView *) TableView Numberofrowsinsection: (Nsinteger) section{return [dataList count];} What are they called-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{// Determine the cell identity static nsstring *[email protected] "cell"; multiplexed 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] "This is the subtitle"; Team name Cell.textlabel.text=[[datalist ObjectAtIndex:indexPath.row] objectforkey:@ "name"]; Generate string NSString *imagepath=[[[datalist objectatindex:indexpath.row]objectforkey:@ "image" based on plist file) stringbyappendingstring:@ ". png"]; Load a picture based on a string 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
Source code: Https://git.oschina.net/zhengaoxing/table1-typeThis article original, reproduced please indicate the source: http://blog.csdn.net/zhenggaoxing/article/details/44857765
iOS Learning-table view Add a hover window to Tableviewcontroller