gpt4 book ai didi

ios - 拉动刷新 UITableView 数据

转载 作者:行者123 更新时间:2023-11-29 02:52:55 25 4
gpt4 key购买 nike

我有刷新设置的请求。它当前正在调用 [self.tableView reloadData];但它不会从 blogData 方法重新加载我解析的 Json 数据。我缺少什么吗?

我的 Controller 是这样的:

//
// 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

最佳答案

当您调用 -[UITableView reloadData] 方法时,您是在告诉 tableview 根据您已经提供给他的源刷新 ints 内容,但他不会更改源。在某些情况下,您刷新数据源并且需要刷新 UITableView,因此您调用 -[UITableView reloadData]。您需要做的是先刷新数据,然后刷新 View ,以便 View 使用您的新数据。

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

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],但现在他将使用您从模态中获得的刷新数据进行刷新。

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

关于ios - 拉动刷新 UITableView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24275256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com