Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshcontrol jitters when pulled down and held

I have created a UIRefreshcontrol in my tableviewcontroller as follows in the viewdidload method :

    refresh = [UIRefreshControl.alloc init];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    [refresh addTarget:self action:@selector(refreshChatsTableView) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refresh;

the problem is that when I pull it down a little long , the table jitters up giving a pretty unpleasant UI experience. Can anyone please help ? This behavior is experienced only in the Landscape mode.

Here is my code :

-UIRefreshControl  *refresh;

    -(void)viewDidLoad{
        [super viewDidLoad];
         arr= [[NSArray    alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"A",@"B",@"C",@"D",@"E", nil];

        //refresh control
        refresh = [UIRefreshControl.alloc init];
        refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to   Refresh"];
        [refresh addTarget:self action:@selector(refreshChatsTableView)  forControlEvents:UIControlEventValueChanged];
        self.refreshControl = refresh;
      }


    -(void)refreshChatsTableView{
        [refresh endRefreshing];
    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       return arr.count;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell =[[UITableViewCell alloc]init];
        cell.textLabel.text = [arr objectAtIndex:indexPath.row ];

        return cell;
    }
like image 335
Anum Malik Avatar asked Mar 06 '14 12:03

Anum Malik


2 Answers

try changing

self.refreshControl = refresh;

for

[self.tableView addSubview:refresh];
[self.tableView sendSubviewToBack:refresh];

+++++++++ if u have problem with attributedString layout with first refreshControl triggered - add this after adding refreshControl as subview

dispatch_async(dispatch_get_main_queue(), ^{
    [refreshControl beginRefreshing];
    [refreshControl endRefreshing];
});
like image 181
tholub Avatar answered Nov 16 '22 08:11

tholub


So I just came across this same problem, and I came up with a solution. Basically, if you pull your tableView down far enough and hold it, the ValueChanged control event will fire before the user releases the touch. If you refresh your data and reload the tableView at this point, the table will jump up.

To combat this, I ended up refreshing the table independently of the ValueChanged event. Instead, I used that event, along with the scrollView delegate methods, to only refresh the table after the user had let go.

So first, set up your refreshControl in viewDidLoad.

override func viewDidLoad()
{
    super.viewDidLoad()

    refreshControl = UIRefreshControl()
    refreshControl?.addTarget(self, action: "refreshControlChanged", forControlEvents: UIControlEvents.ValueChanged)
}

The ValueChanged method is simple. If we've already stopped dragging, which happens after a quick swipe down, just refresh the table using your own custom method.

internal func refreshControlChanged()
{
    if !tableView.dragging
    {
        refresh()
    }
}

Then, you need to implement didEndDragging. If you've pulled down far enough, the refreshControl will be refreshing, so call refresh. Otherwise, you either haven't pulled down far enough, you were dragging on a different part of the table, or you dragged so quickly that the refreshControl ValueChanged event has yet to fire, and it will refresh the table there.

override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
    if refreshControl?.refreshing == true
    {
        refresh()
    }
}

And that's it. The table won't get reloaded until after you've stopped dragging it, which avoids the jittery jump that you noticed. I know this is a year old, but I hope this helps anyone else coming across this on Google.

like image 28
Devin Avatar answered Nov 16 '22 10:11

Devin