拉动以刷新UITableView数据

时间:2014-06-18 00:22:35

标签: ios objective-c uitableview

我有一个刷新设置。它目前正在调用[self.tableView reloadData];但它没有从blogData方法重新加载我解析的Json数据。这是我失踪的东西吗?

我的控制器是这样的:

//
//  ARTableViewController.m
//  WorldCupLive
//
//  Created by Adam Rais on 14/06/2014.
//  Copyright (c) 2014 Adam Rais. All rights reserved.
//

#import "ARTableViewController.h"
#import "ARModal.h"

@interface ARTableViewController ()

@end

@implementation ARTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.blogPost = [[ARModal alloc] init];
    self.blogPost.jsonMutable = [NSMutableArray array];

    for (NSDictionary *post in self.blogPost.blogData) {
        ARModal *bp = [ARModal blogPostWithHome:[[post objectForKey:@"home"] objectForKey:@"text"]];
        bp.away = [[post objectForKey:@"away"] objectForKey:@"text"];
        bp.result = [[post objectForKey:@"result"] objectForKey:@"text"];
        bp.info = [post objectForKey:@"info"];
        bp.homeImage = [[post objectForKey:@"homeimage"] objectForKey:@"src"];
        [self.blogPost.jsonMutable addObject:bp];
    }

    [self randomBackgroundImage];

    self.tableView.contentInset = UIEdgeInsetsMake(0.0f, -10.0f, 0.0f, 0.0f);

    // Initialize Refresh Control
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];

    // Configure Refresh Control
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

    // Configure View Controller
    [self setRefreshControl:refreshControl];

}

-(void)randomBackgroundImage {
    UIImage *image = self.blogPost.imageUI;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    self.tableView.backgroundView = imageView;
    self.tableView.backgroundView.layer.zPosition -= 1;
}

- (void)refresh:(id)sender
{
    NSLog(@"Refreshing");
    [self.tableView reloadData];
    [self randomBackgroundImage];

    // End Refreshing
    [(UIRefreshControl *)sender endRefreshing];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.blogPost.jsonMutable count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    ARModal *post = [self.blogPost.jsonMutable objectAtIndex:indexPath.row];
    NSData *imageData = [NSData dataWithContentsOfURL:post.jsonURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.textLabel.text = [[[[[post.home stringByAppendingString:@" "]stringByAppendingString:@" "] stringByAppendingString:post.bst] stringByAppendingString:@" "] stringByAppendingString:post.away];
    cell.detailTextLabel.text = post.info;
    cell.imageView.image = image;
    cell.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.000];


    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

我的模态:

//
//  ARModal.m
//  WorldCupLive
//
//  Created by Adam Rais on 14/06/2014.
//  Copyright (c) 2014 Adam Rais. All rights reserved.
//

#import "ARModal.h"

@implementation ARModal

-(id)initWithHome:(NSString *)home {
    self = [super init];

    if (self) {
        _home = home;
        _away = nil;
        _result = nil;
        _info = nil;
        _homeImage = nil;
    }
    return self;
}

+(id)blogPostWithHome:(NSString *)home {
    return [[self alloc] initWithHome:home];
}

-(NSArray *)blogData {
    NSURL *jsonURL = [NSURL URLWithString:@"http://www.kimonolabs.com/api/2nfgfo2s?apikey=1a1f5f323969d5157af8a8be857026c2"];
    NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];

    NSError *jsonError = nil;
    NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];

    NSArray *jsonArray = [[jsonDictionary objectForKey:@"results"] objectForKey:@"collection1"];

    if (blogData == nil) {
        blogData = jsonArray;
    }
    return blogData;
}

-(NSURL *)jsonURL {
    return [NSURL URLWithString:self.homeImage];
}

-(NSString *)bst {

    NSString *sentence = self.result;
    NSString *word = @"-";
    NSString *wordTwo = @"00.00";
    NSString *wordThree = @"01.00";
    NSMutableArray *bstArray = [NSMutableArray array];

    if ([sentence rangeOfString:word].location != NSNotFound) {
        NSLog(@"Found the string");
        [bstArray addObject:sentence];
    } else if ([sentence rangeOfString:wordTwo].location != NSNotFound) {
        NSLog(@"time is 23:00");
        [bstArray addObject:@"23:00"];
    } else if ([sentence rangeOfString:wordThree].location != NSNotFound) {
        NSLog(@"time is 00:00");
        [bstArray addObject:@"00:00"];
    } else {
        float floatOne = [sentence floatValue];
        float floatFinal = floatOne - 1.000000;
        NSString *str = [NSString stringWithFormat:@"%f", floatFinal];
        NSString *bstFinal = [str substringToIndex:[str length] - 4];
        [bstArray addObject:bstFinal];
    }
    return [bstArray objectAtIndex:0];
}

-(UIImage *)imageUI {

    NSArray *imageArray = @[[UIImage imageNamed:@"Algeria"],[UIImage imageNamed:@"Argentina"],[UIImage imageNamed:@"Australia"],[UIImage imageNamed:@"Belgium"],[UIImage imageNamed:@"Bosnia-Herzegovina"],[UIImage imageNamed:@"Switzerland"],[UIImage imageNamed:@"Uruguay"],[UIImage imageNamed:@"USA"],[UIImage imageNamed:@"Brazil"],[UIImage imageNamed:@"Cameroon"],[UIImage imageNamed:@"Chile"],[UIImage imageNamed:@"Colombia"],[UIImage imageNamed:@"Costa Rica"],[UIImage imageNamed:@"Côte d'Ivoire"],[UIImage imageNamed:@"Croatia"],[UIImage imageNamed:@"Ecuador"],[UIImage imageNamed:@"England"],[UIImage imageNamed:@"France"],[UIImage imageNamed:@"Germany"],[UIImage imageNamed:@"Ghana"],[UIImage imageNamed:@"Greece"],[UIImage imageNamed:@"Honduras"],[UIImage imageNamed:@"Iran"],[UIImage imageNamed:@"Italy"],[UIImage imageNamed:@"Japan"],[UIImage imageNamed:@"Mexico"],[UIImage imageNamed:@"Netherlands"],[UIImage imageNamed:@"Nigeria"],[UIImage imageNamed:@"Portugal"],[UIImage imageNamed:@"Russia"],[UIImage imageNamed:@"South Korea"],[UIImage imageNamed:@"Spain"]];

    int random = arc4random_uniform(imageArray.count);
    return [imageArray objectAtIndex:random];

}

@end

2 个答案:

答案 0 :(得分:1)

当您调用 - [UITableView reloadData]方法时,您告诉tableview根据您已经给他的来源刷新内容,但他没有更改源。在某些情况下,您刷新数据源,您需要刷新UITableView,以便调用 - [UITableView reloadData]。您需要做的是首先刷新数据并刷新视图,以便视图使用您的新数据。

使用与模态数据相同的方式刷新它。

self.blogPost.jsonMutable = [NSMutableArray array];

for (NSDictionary *post in self.blogPost.blogData) {
    ARModal *bp = [ARModal blogPostWithHome:[[post objectForKey:@"home"] objectForKey:@"text"]];
    bp.away = [[post objectForKey:@"away"] objectForKey:@"text"];
    bp.result = [[post objectForKey:@"result"] objectForKey:@"text"];
    bp.info = [post objectForKey:@"info"];
    bp.homeImage = [[post objectForKey:@"homeimage"] objectForKey:@"src"];
    [self.blogPost.jsonMutable addObject:bp];
}

然后执行 - [UITableView reloadData],但现在他将使用从模态中获得的刷新数据进行刷新。

如果您还需要其他内容,请发表评论。

答案 1 :(得分:0)

-[UITableView reloadData]实际上并未重新加载您的数据。它告诉表您已重新加载数据,现在需要重新加载表视图。

因此,在致电reloadData之前,您应该这样做:

    self.blogPost.jsonMutable = [NSMutableArray array];

    for (NSDictionary *post in self.blogPost.blogData) {
        ARModal *bp = [ARModal blogPostWithHome:[[post objectForKey:@"home"] objectForKey:@"text"]];
        bp.away = [[post objectForKey:@"away"] objectForKey:@"text"];
        bp.result = [[post objectForKey:@"result"] objectForKey:@"text"];
        bp.info = [post objectForKey:@"info"];
        bp.homeImage = [[post objectForKey:@"homeimage"] objectForKey:@"src"];
        [self.blogPost.jsonMutable addObject:bp];
    }

此外,由于此代码块仅使用ARModal提供的信息,您应该将其放在ARModal的方法中