如果它缓慢滚动,如何更新tableView?

时间:2016-09-19 08:26:43

标签: ios swift uitableview

我想只调用tableView.reloadData(),如果tableView缓慢滚动以避免闪烁。 为实现此目的,我在scrollViewDidScroll委托函数中监控滚动速度。

func scrollViewDidScroll(scrollView: UIScrollView) {
    let scrollSpeed:CGFloat = scrollView.contentOffset.y - previousScrollViewYOffset
    previousScrollViewYOffset = scrollView.contentOffset.y
    if scrollSpeed < 1.0
    { 
        tViewMoves = false
        // tableView.reloadData()    
    }
    else
    {
        tViewMoves = true   
    }
}

问题是Bool tViewMoves经常被访问和设置,但是不会将其值从true更改为false,反之亦然。然而,每次访问Bool时都会调用tableView.reloadData(),并在滚动tableView时导致一些闪烁。我试图通过向tViewMoves添加一个带.New选项的观察者来克服这个问题。但是每次设置tViewMoves时仍会调用tableView.reloadData()方法,即使它没有更改其值。

self.addObserver(self, forKeyPath: "tViewMoves", options: .New, context: &observerContext)

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &observerContext {
     if let newValue = change?[NSKeyValueChangeNewKey] {
           print("changed: \(newValue)") 
           tableView.reloadData()   
        }
    } 
else {
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)

    }
}

deinit {
    self.removeObserver(self, forKeyPath: "tViewMoves", context: &observerContext)
}

我有什么建议可以解决这个问题吗?

更新编辑:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:CalendarCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CalendarCell
    cell.backgroundColor = UIColor.clearColor()

    let buttonView = self.buttonViewArray[indexPath.row]
    cell.aBackgroundView.addSubview(buttonView)

    if tViewMoves == false
    {

        let buttonStarView = self.buttonStarViewArray[indexPath.row]
        buttonStarView.myDelegate = cell
        buttonStarView.kalenderVCDelegate = self
        cell.aBackgroundView.addSubview(buttonStarView)


        //add tapRecognizer
        let tap = SubclassedTapRec(target: self, action: #selector(KalenderVC.tapButtonView(_:)))
        tap.myTag = buttonStarView.tag
        buttonStarView.addGestureRecognizer(tap)


    }
    return cell
}

更新编辑2:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    let myCell = cell as! CalendarCell

    dispatch_async(dispatch_get_global_queue(priority, 0)) {
        let buttonStarView = self.buttonStarViewArray[indexPath.row]
        buttonStarView.myDelegate = myCell
        buttonStarView.kalenderVCDelegate = self
        myCell.aBackgroundView.addSubview(buttonStarView)

        dispatch_async(dispatch_get_main_queue()) {

            tableView.reloadData()
        }
    }
}

2 个答案:

答案 0 :(得分:2)

我会推荐这样的东西:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {

        let testView = UIView(frame: CGRectMake(0,0,100,100))
        testView.frame = CGRectMake(0, 0, 100, 10)
        testView.backgroundColor = UIColor.greenColor()

        dispatch_async(dispatch_get_main_queue()) {
            cell.addSubview(testView)
            //You should not reload the complete table view from inside the draw methods because it will cause a infinite loop
            //tableView.reloadData()
        }
    }



    return cell
}

修改 看看你的代码,我会改变它。

$("#hide").click(function(){
        $("p").hide();
    });
    $("#show").click(function(){
        $("p").show();
    });

答案 1 :(得分:0)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {

        let testView = UIView(frame: CGRectMake(0,0,100,100))
        testView.frame = CGRectMake(0, 0, 100, 10)
        testView.backgroundColor = UIColor.greenColor()
        cell.addSubview(testView)

        dispatch_async(dispatch_get_main_queue()) {

            tableView.reloadData()
        }
    }



    return cell
}