iOS Development Swift-click on the status bar to bring TableView back to the top (with: Status bar Click event Response)

Source: Internet
Author: User
Tags one table uikit
1, when only one scrollview on the page, click the status bar ScrollView will automatically scroll to the top

For example, there is only one table on the page (UITableView), when clicked on the top state bar, the table will be like QQ, micro-letter contact list that back to the top.

This is the default for the iOS system.


2, when the page has multiple ScrollView, click the status bar, the view will not scroll

Then we need to set the scrolltotop of the scrollview that does not need to scroll to false, leaving only one.
TableView? Scrollstotop = false;

3, the status bar click event Response
Sometimes we want to do some other things when we click on the status bar, in addition to having the view automatically scroll. The implementation is divided into the following two scenarios:

(1) When there is scrollview on the page
If there is a scrolling view on the page, add the relevant action directly in the Scrollviewshouldscrolltotop () event response.
(Note: If you do not need to scroll the view, you can return False in the method)

Import Uikit

Class Viewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {

var tableview:uitableview?

Override Func Loadview () {
Super.loadview ()
}

Override Func Viewdidload () {
Super.viewdidload ()

Create a table view
Self.tableview = UITableView (Frame:self.view.frame, Style:UITableViewStyle.Plain)
self.tableview!. delegate = Self
self.tableview!. DataSource = Self
To create a reused cell
self.tableview!. RegisterClass (uitableviewcell.self, Forcellreuseidentifier: "Swiftcell")
Self.view.addSubview (self.tableview!)
}

Func scrollviewshouldscrolltotop (Scrollview:uiscrollview)-> Bool {
Print ("status bar clicks")
Here to add code logic to execute ....

Do not scroll table view
return False
}

In this case, there is only one partition
Func Numberofsectionsintableview (Tableview:uitableview)-> Int {
return 1;
}

Returns the number of table rows (that is, returns the number of controls)
Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {
Return 100
}

Create each cell display (create a parameter indexpath a specified cell)
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)
-> UITableViewCell
{
In order to provide tabular display performance, the completed cells need to be reused
Let identify:string = "Swiftcell"
Cells in the same form are reused and registered at the time of declaration
Let cell = Tableview.dequeuereusablecellwithidentifier (Identify,
Forindexpath:indexpath) as UITableViewCell
Cell.accessorytype = Uitableviewcellaccessorytype.disclosureindicator
Cell.textlabel? Text = "entry data \ (Indexpath.row)"
return cell
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

(2) When there is no scrollview on the page
If there is no scrolling view on the page, we can add a hidden scrollview (height of 0) and then add the corresponding action in Scrollviewshouldscrolltotop ().
(Note: Do not use hidden or alpha=0 to hide scrollview, you cannot invoke the Scrollviewshouldscrolltotop () method)

Import Uikit

Class Viewcontroller:uiviewcontroller, uiscrollviewdelegate{

var tableview:uitableview?

Override Func Loadview () {
Super.loadview ()
}

Override Func Viewdidload () {
Super.viewdidload ()

Create a hidden scrolling view
Let ScrollView = Uiscrollview ()
Scrollview.frame = CGRect (x:0, y:0, Width:self.view.frame.width, height:0)
Scrollview.contentsize = Cgsize (width:self.view.frame.width,height:10)
Scrollview.contentoffset = Cgpoint (x:0,y:10)
Scrollview.delegate = Self
Self.view.addSubview (ScrollView)
}

Func scrollviewshouldscrolltotop (Scrollview:uiscrollview)-> Bool {
Print ("status bar clicks")
Here to add code logic to execute ....

Do not scroll view
return False
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.