【问题标题】:pull to refresh not working with small amount of cells拉动刷新不适用于少量单元格
【发布时间】:2015-02-23 19:29:56
【问题描述】:

我有一个提要视图控制器(使用 UICollectionViewController 实现) 当我没有足够的单元格来覆盖大厅屏幕高度时,刷新的拉动不起作用。 我该如何解决这个问题?

代码:

var refreshControl: UIRefreshControl!

    override func viewDidLoad() {
    super.viewDidLoad()


    self.refreshControl = UIRefreshControl()
    self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
    self.postCollection.addSubview(refreshControl)
}

    func refresh(sender:AnyObject)
{
    getPost()
}

编辑: 不工作我的意思是 uicollectionview 不能被拉下并且动画没有开始。

【问题讨论】:

  • 你解决了吗?
  • @ReneDohan,没有找到解决方案,st.derrick 回答也许会对你有所帮助

标签: ios swift uicollectionview


【解决方案1】:

斯威夫特 4

以下将强制集合视图始终反弹,在这种情况下垂直反弹,即使项目没有填满整个集合视图高度。

self.collectionView.alwaysBounceVertical = true

【讨论】:

  • 请添加注释说明此代码如何解决问题
  • 为我工作..@Terru_theTerror 下面是描述:如果此属性设置为true并且bounces为true,则即使内容小于滚动视图的边界,也允许垂直拖动。默认值为 false。
【解决方案2】:

这意味着您很可能不会在 getPost() 方法中刷新 tableview。更新数据后,您需要致电self.postCollection.reloadData()

你能发布 cellForRowAtIndexPath 的代码吗?您也可能没有正确考虑重复使用的单元格。

编辑:我相信我已经解决了

我看到了与您类似的行为,它与自动布局和大小类有关

对我来说,刷新控件显示在右侧,而不是居中:

模拟器:

bad-sim http://derrrick.com/stackoverflow/refreshcontrol/bad-sim.png

对你来说,它可能会在屏幕外显示。

注意在查看情节提要时这是如何显示的(“使用自动布局”和“使用大小类”都被选中)。

Xcode:

bad-xcode http://derrrick.com/stackoverflow/refreshcontrol/bad-xcode.png

一个解决方案是取消选中“使用自动布局”。然后你可能需要调整你的 tableview 的大小(我不得不删除它并重新添加它)。

那么它应该会正确显示。

模拟器:

good-sim http://derrrick.com/stackoverflow/refreshcontrol/good-sim.png

Xcode:

good-xcode http://derrrick.com/stackoverflow/refreshcontrol/good-xcode.png

最后,这是我的代码(虽然它缺少刷新方法:)

类 ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var postCollection: UITableView!
        var myCollection = ["hi", "there", "bob"]
        var refreshControl: UIRefreshControl!

        override func viewDidLoad() {
            super.viewDidLoad()

            if let myTableView = self.postCollection {
                self.postCollection!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
                self.postCollection!.delegate = self
                self.postCollection!.dataSource = self

                self.refreshControl = UIRefreshControl(frame: CGRectMake(0, 0, 40, 40))
                self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
                self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
                self.postCollection.insertSubview(self.refreshControl, atIndex: 0)
            }
        }

         func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
            cell.textLabel?.text = self.myCollection[indexPath.row]
            return cell
        }

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.myCollection.count
        }
    }

【讨论】:

  • 无法看到您的 PNG(网站显示未经授权)。
【解决方案3】:
    override func viewDidLoad() {
    super.viewDidLoad()

    refreshControl = UIRefreshControl()
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    self.refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
    listview.addSubview(refreshControl)

}
func refresh(sender:AnyObject) {
    // Your code here
    refreshControl.endRefreshing()
}

工作正常

【讨论】:

    【解决方案4】:

    以编程方式: self.collectionView.alwaysBounceVertical = true

    或从情节提要: 您应该启用这两个选项

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 2018-02-07
      • 2018-10-27
      相关资源
      最近更新 更多