【问题标题】:Pulling to refresh Data拉动刷新数据
【发布时间】:2013-01-29 02:16:15
【问题描述】:

让我的应用程序使用 ODRefreshcontrol 进行刷新动作:https://github.com/Sephiroth87/ODRefreshControl 如何真正刷新我的表格视图?,这是 .m 文件中的代码:

#import "ThirdViewController.h"
#import "ODRefreshControl.h"

@interface ThirdViewController ()

@end




@implementation ThirdViewController

@synthesize tableView = _tableView;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

-(void)TableView:(UITableView *)TableView didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't    connect. Please check your internet Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{ 
[super viewDidLoad];
ODRefreshControl *refreshControl = [[ODRefreshControl alloc] initInScrollView:self.tableView];
[refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];

// Do any additional setup after loading the view.
[self fetchTweets];
self.tableView.dataSource = self;
self.tableView.delegate = self;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
[tempImageView setFrame:self.tableView.frame];

self.tableView.backgroundView = tempImageView;



}

- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
{
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [refreshControl endRefreshing];
});
}



- (void)fetchTweets
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData* data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString: @"http://search.twitter.com/search.json?q=from:user"]];

    NSError* error;

    tweets = [NSJSONSerialization JSONObjectWithData:data
                                             options:kNilOptions
                                               error:&error];

    NSLog(@"Tweets %@", tweets);

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
});
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}

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



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSArray *tweetsArray = [tweets valueForKey:@"results"];
NSDictionary *tweet = [tweetsArray objectAtIndex:indexPath.row];


NSString *text = [tweet objectForKey:@"text"];
//NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];

cell.textLabel.text = text;
//cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];

return cell;

}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"Row %d selected", indexPath.row);
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

我需要添加什么来刷新我的表格视图中的 twitter 提要?

【问题讨论】:

    标签: ios xcode json twitter pull-to-refresh


    【解决方案1】:

    刷新时需要调用fetchTweets方法

    - (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
    {
        double delayInSeconds = 2.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    
            //ADD
            [self fetchTweets];
    
            [refreshControl endRefreshing];
    });
    }
    

    【讨论】:

    • 非常感谢,但我在您回复前 2 分钟才设法弄清楚这一点。不过谢谢!!
    • 还有一件事我不知道如何将加载图标的颜色更改为白色,我有深色背景所以看不到?
    • 在 ODRefreshControl.m _activity = activity ? activity : [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 中将活动指示器样式更改为 UIActivityIndi​​catorViewStyleWhite
    • 不确定如何输入请查看:stackoverflow.com/questions/14857447/… 谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2020-12-02
    • 2020-07-07
    相关资源
    最近更新 更多