gpt4 book ai didi

ios - UISegmentedControl filter Table View data JSON 错误

转载 作者:行者123 更新时间:2023-11-29 03:34:30 26 4
gpt4 key购买 nike

我正在尝试过滤 TableView 的数据,该 View 正在调用 JSON 文件并将数据解析到 TableView 。我收到一些奇怪的错误。这是我的代码:

#import "FacebookViewController.h"
#import "RNBlurModalView.h"
#import "FacebookPost.h"
#import "TwitterPost.h"

#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
#define FONT_SIZE 14.0f

@interface FacebookViewController ()
{
NSInteger refreshIndex;
NSArray *fbPost;
NSArray *pic;
}

@end

@implementation FacebookViewController
@synthesize tweets;

- (void)refreshChannels:(id)sender {

if (tweets.count == 0) return;

// disable UI
self.title = @"Updating...";
self.navigationController.view.userInteractionEnabled = YES;
refreshIndex = 0;
}

- (void) reloadFB {

}
- (void)viewDidLoad
{
[super viewDidLoad];


UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refreshChannels:)];
self.navigationItem.rightBarButtonItem = button;

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStyleBordered target:self action:@selector(showMenu)];

UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
[self.view addGestureRecognizer:gestureRecognizer];

self.myTableView.separatorColor = [UIColor clearColor];

[self issueLoadRequest];
}

- (void)swipeHandler:(UIPanGestureRecognizer *)sender
{
[[self sideMenu] showFromPanGesture:sender];
}

#pragma mark -
#pragma mark Button actions

- (void)showMenu
{
[[self sideMenu] show];
}

#pragma mark - Table view data source

- (void)issueLoadRequest
{
if (changeData.selectedSegmentIndex == 1) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://my-site-facebookparse.php?person=Person"]];
[self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
});
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://my-site-twitterparse.php?person=Person"]];
[self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
});
}
}

- (void)receiveData:(NSData *)data {
if (changeData.selectedSegmentIndex == 1) {
self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.myTableView reloadData];
} else {
self.tweets1 = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.myTableView reloadData];
}
}

- (void)receiveTwitter:(NSData *)data {
// When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
// going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
self.tweets1 = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.myTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (changeData.selectedSegmentIndex == 1) {
return self.tweets.count;
} else {
return self.tweets1.count;
}
}

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

// The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text".
NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row];
NSDictionary *tweet1 = [self.tweets1 objectAtIndex:indexPath.row];

FacebookPost *cell = (FacebookPost *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FacebookPost" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

if (changeData.selectedSegmentIndex == 1) {
cell.fbPost.text = [tweet objectForKey:@"message"];
} else {
cell.fbPost.text = [tweet1 objectForKey:@"tweet"];
}
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (changeData.selectedSegmentIndex == 1) {
//Open the link
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * storyLink = [[tweets objectAtIndex: storyIndex] objectForKey:@"message"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];

RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:@"Message" message:storyLink];
[modal show];

NSString *formattedJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[self.tweets objectAtIndex:indexPath.row] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
NSLog(@"tweet:\n%@", formattedJSON);
} else {
//Öppna länken eller liknande
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString * storyLink = [[tweets objectAtIndex: storyIndex] objectForKey:@"tweet"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];

RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:@"Message" message:storyLink];
[modal show];

// Spit out some pretty JSON for the tweet that was tapped. Neato.
NSString *formattedJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[self.tweets objectAtIndex:indexPath.row] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
NSLog(@"tweet:\n%@", formattedJSON);
}
}



@end

TableView 数据正在下载发布时的 Twitter 帖子,即使我已将其设置为下载 Facebook 帖子。这很奇怪...请帮我解决这个问题!

最佳答案

您需要做几件事。首先,您需要设置选定的段索引。在 viewDidLoad 中调用 [self issueLoadRequest] 之前,您应该像这样设置选定的索引:

changeData.selectedSegmentIndex = 0; 

这会将第一个片段设置为所选片段。此外,您还需要确保在更改所选分割时加载了正确的数据。为此,您应该将以下内容添加到 viewDidLoad:

[changeData addTarget:self action:@selector(segmentedControlSelectedIndexChanged:) forControlEvents:UIControlEventValueChanged];

以及关联方法,segmentedControlSelectedIndexChanged:

- (void)segmentedControlSelectedIndexChanged:(id)sender 
{
[self issueLoadRequest];
}

现在,无论何时在 Facebook 段和 Twitter 段之间切换,它都会调用相应的 API、下载数据并更新 TableView 。根据您的连接速度,在选择段和更新表格 View 之间可能会有一小段但明显的延迟。

关于ios - UISegmentedControl filter Table View data JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19368455/

26 4 0