ios開發之Swift - 點擊狀態列使tableView返回頂部(附:狀態列點擊事件響應)

來源:互聯網
上載者:User
1,當頁面上只有一個scrollView,點擊狀態列scrollView會自動滾動到頂部

 

比如頁面上只有一個表格(UITableView),當點擊頂部狀態條後,表格會像QQ、微信連絡人清單那樣回到最上面。

 

這個是iOS系統預設就有的。

 


2,當頁面上有多個scrollView,點擊狀態列時,視圖都不會滾動

這時我們需要把不需要滾動的 scrollView 的 scrollToTop 設為 false,只留下一個。
tableView?.scrollsToTop = false;

3,狀態列點擊事件響應
有時我們想在狀態列點擊的時候,除了讓視圖自動滾動外,還想執行一些其他動作。實現方式分為下面兩種情況:

(1)頁面上有scrollView時
如果頁面上有滾動視圖的話,直接在 scrollViewShouldScrollToTop() 事件響應中添加相關操作即可。
(註:如過不需要滾動視圖,方法內可以return false)

import UIKit
 
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
     
    var tableView:UITableView?
     
    override func loadView() {
        super.loadView()
    }
     
    override func viewDidLoad() {
        super.viewDidLoad()
                 
        //建立表視圖
        self.tableView = UITableView(frame: self.view.frame, style:UITableViewStyle.Plain)
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        //建立一個重用的儲存格
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview(self.tableView!)
    }
     
    func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool {
        print("狀態列點擊")
        //這裡添加需要執行的代碼邏輯....
         
        //不滾動表格視圖
        return false
    }
     
    //在本例中,只有一個分區
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }
     
    //返回表格行數(也就是返回控制項數)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }
     
    //建立各單元顯示內容(建立參數indexPath指定的單元)
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell
    {
        //為了提供表格顯示效能,已建立完成的單元需重複使用
        let identify:String = "SwiftCell"
        //同一形式的儲存格重複使用,在聲明時登入
        let cell = tableView.dequeueReusableCellWithIdentifier(identify,
            forIndexPath: indexPath) as UITableViewCell
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.textLabel?.text = "條目資料\(indexPath.row)"
        return cell
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

(2)頁面上沒有scrollView時
如果頁面上沒有滾動視圖,我們可以自己添加個隱藏的scrollView(高度為0),然後同樣在 scrollViewShouldScrollToTop() 中添加相應的操作。
(註:不要使用 hidden 或者 alpha=0 隱藏 scrollView,會無法調用 scrollViewShouldScrollToTop() 方法)

import UIKit
 
class ViewController: UIViewController, UIScrollViewDelegate{
     
    var tableView:UITableView?
     
    override func loadView() {
        super.loadView()
    }
     
    override func viewDidLoad() {
        super.viewDidLoad()
                 
        //建立一個隱藏的滾動視圖
        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("狀態列點擊")
        //這裡添加需要執行的代碼邏輯....
         
        //不滾動視圖
        return false
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.