gpt4 book ai didi

ios - 如何让我的 Facebook GraphAPI 数据在 UITableView 中不重复?

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

我知道对于任何精通 Objective-C 的人来说,这可能是一个简单的答案,但我是使用 Objective-C 的新手,过去几天我一直被这个问题困扰。我已经阅读了很多关于重复单元格的 stackoverflow 线程,并尝试复制和粘贴了很多答案,但似乎没有一个对我有用。

我正在尝试将从适用于 iOS 的 Facebook SDK 获取的数据放入 UITableView。我已经设法从 Facebook GraphAPI 获取数据并进入 UITableView,但我无法解决的问题是应用程序从 Facebook 获取的最后一条记录中的数据在所有单元格中不断重复。除此之外,当我向上或向下滚动时,填充单元格需要几秒钟,即使它已经知道数据。

我已经尝试了很多方法来让它工作,但解决这个问题超出了技能水平。在这一点上,我将尽一切努力让它正常工作并显示我从 Facebook 获得的结果而不会导致当前问题,因此非常感谢任何帮助。

我的 NSLogs 显示两条记录,但我的 UITableView 只会显示一条。这是我的日志示例。

The follow data is from the UITableViewCell method in FBLeaderBoardTableViewController.m
Full Name: John Smith
High Score: 5666
Facebook ID: 654321
Image: <UIImage: 0xde12345>
IndexPath Row: 1

*************************************

2013-12-26 11:51:48.380 [7820:70b] CREATING NEW CELL
2013-12-26 11:51:48.380 [7820:70b] indexPath.row: 1
2013-12-26 11:51:48.468 [7820:70b]

The follow data is from the UITableViewCell method in FBLeaderBoardTableViewController.m
Full Name: Figurative Art
High Score: 2762
Facebook ID: 123456
Image: <UIImage: 0x1101234>
IndexPath Row: 1

*************************************

2013-12-26 11:51:48.468 [7820:70b] CREATING NEW CELL
2013-12-26 11:51:48.469 [7820:70b] indexPath.row: 1
2013-12-26 11:51:48.553 [7820:70b]

The follow data is from the UITableViewCell method in FBLeaderBoardTableViewController.m
Full Name: John Smith
High Score: 5666
Facebook ID: 654321
Image: <UIImage: 0xde12345>
IndexPath Row: 0

*************************************

2013-12-26 11:51:48.553 [7820:70b] CREATING NEW CELL
2013-12-26 11:51:48.553 [7820:70b] indexPath.row: 0
2013-12-26 11:51:48.637 [7820:70b]

The follow data is from the UITableViewCell method in FBLeaderBoardTableViewController.m
Full Name: Figurative Art
High Score: 2762
Facebook ID: 123456
Image: <UIImage: 0x1101234>
IndexPath Row: 0

*************************************

2013-12-26 11:51:48.637 [7820:70b] CREATING NEW CELL
2013-12-26 11:51:48.637 [7820:70b] indexPath.row: 0
2013-12-26 11:51:48.720 [7820:70b]

The follow data is from the UITableViewCell method in FBLeaderBoardTableViewController.m
Full Name: John Smith
High Score: 5666
Facebook ID: 654321
Image: <UIImage: 0xde12345>
IndexPath Row: 2

*************************************

2013-12-26 11:51:48.720 [7820:70b] CREATING NEW CELL
2013-12-26 11:51:48.720 [7820:70b] indexPath.row: 2
2013-12-26 11:51:49.290 [7820:70b]

The follow data is from the UITableViewCell method in FBLeaderBoardTableViewController.m
Full Name: Figurative Art
High Score: 2762
Facebook ID: 123456
Image: <UIImage: 0x1101234>
IndexPath Row: 2

*************************************

这是我的 LeadeboardViewController.m 中的代码

#import "FBLeaderBoardTableViewController.h"
#import "FBLeaderBoardCell.h"
#import "ViewController.h"


@interface FBLeaderBoardTableViewController ()

@end

@implementation FBLeaderBoardTableViewController

@synthesize facebookID, facebookFullName, facebookFirstName, facebookHighScore, facebookProfilePicture, tableView, closeFacebookLeaderboard;
@synthesize FBArray;
@synthesize FBMutableArray;



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

- (void)viewDidLoad
{
[super viewDidLoad];

[self.tableView registerNib:[UINib nibWithNibName:@"FBLeaderBoardCell" bundle:nil ] forCellReuseIdentifier:@"FBLeaderBoardCellID"];

}


- (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];


}

- (IBAction)closeFacebookLeaderboard:(id)sender {

[self dismissViewControllerAnimated:TRUE completion:nil];

}

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

// [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 10;

//RESULTS THAT DIDN'T WORK
//return [displayData count];
//return [self.data count];
//return [FBLeaderBoardData count];
//return [facebookHighScore count];
//return [data count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"fbcell";
FBLeaderBoardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{

cell = [[[FBLeaderBoardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FBLeaderBoardCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[FBLeaderBoardCell class]]){
cell = (FBLeaderBoardCell *)currentObject;

}
}

} [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"xxxx/scores?fields=score,user"] parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

if (result && !error)
{
// int index = 0;
for (NSDictionary *dict in [result objectForKey:@"data"])
{
NSString *name = [[dict objectForKey:@"user"] objectForKey:@"name"];
NSString *highScore = [dict objectForKey:@"score"];
NSString *FBID = [[dict objectForKey:@"user"] objectForKey:@"id"];
NSString *imageURL = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture",FBID];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
[imageURL release];
self.facebookFullName = name;
self.facebookHighScore = highScore;
self.facebookProfilePicture = image;

NSLog(@"\n\nThe follow data is from the UITableViewCell method in FBLeaderBoardTableViewController.m\nFull Name: %@ \nHigh Score: %@ \nFacebook ID: %@ \nImage: %@\nIndexPath Row: %ld\n\n*************************************\n\n", facebookFullName, facebookHighScore, FBID, facebookProfilePicture, (long)indexPath.row);
// NSLog(@"FBGraph Index Row: %d", indexPath.row);
NSLog(@"CREATING NEW CELL");
NSLog(@"indexPath.row: %d", indexPath.row);

cell.FBPlayerPhoto.image = facebookProfilePicture;
cell.FBPlayerName.text = facebookFullName;
cell.FBPlayerScore.text = [NSString stringWithFormat:@"$%@ Gold Coins",facebookHighScore];

}
}

}];

// [tableView reloadData];


}

return cell;
}



// Removes iOS 7 Staus bar from this view.
- (BOOL)prefersStatusBarHidden{
return YES;
}

@end

这是我的 LeadeboardViewController.h 中的代码

#import <UIKit/UIKit.h>
#import "FBLeaderBoardCell.h"
#import <FacebookSDK/FacebookSDK.h>

@interface FBLeaderBoardTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *FBLeaderBoardData;
IBOutlet UILabel *showScore;
__block NSMutableArray* _itemsNamesFriend;
__block NSArray* data;
__block NSMutableArray* displayData;
// IBOutlet FBProfilePictureView *facebookProfilePicture;
IBOutlet UITableView *tableView;
IBOutlet UIButton *closeFacebookLeaderboard;

}


@property (strong, nonatomic) NSArray *data;
@property (strong) NSString *facebookID;
@property (strong) NSString *facebookFullName;
@property (strong) NSString *facebookFirstName;
@property (strong) NSString *facebookHighScore;
@property (strong) UIImage *facebookProfilePicture;
//@property (strong) IBOutlet FBProfilePictureView *facebookProfilePicture;
@property (strong) IBOutlet UITableView *tableView;
@property (strong) IBOutlet UIButton *closeFacebookLeaderboard;
@property (strong, nonatomic) NSArray *FBArray;
@property (strong, nonatomic) NSMutableArray *FBMutableArray;

@end

我知道我目前在我的 - (UITableViewCell *) 中使用 FBRequest 的方式可能不是调用数据的最佳方式,但是当我尝试将其放入 viewDidLoad 方法时,我在 - (UITableViewCell *) 中得到 NULL *).弄清楚如何将它作为 NSArray 或 NSMutableArray 一直给我带来问题,所以我只是将所有内容放入 - (UITableViewCell *)。

最佳答案

您的代码如此重复的原因是因为您的 cellForRowAtIndexPath 方法中有这个 for 循环:

for (NSDictionary *dict in [result objectForKey:@"data"]) {

NSString *name = [[dict objectForKey:@"user"] objectForKey:@"name"];
NSString *highScore = [dict objectForKey:@"score"];
NSString *FBID = [[dict objectForKey:@"user"] objectForKey:@"id"];
NSString *imageURL = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture",FBID];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
[imageURL release];
self.facebookFullName = name;
self.facebookHighScore = highScore;
self.facebookProfilePicture = image;
cell.FBPlayerPhoto.image = facebookProfilePicture;
cell.FBPlayerName.text = facebookFullName;
cell.FBPlayerScore.text = [NSString stringWithFormat:@"$%@ Gold Coins",facebookHighScore];

}

您在加载每一行的同时浏览字典,因此您最终要使用最终迭代期间生成的结果来设置您的单元格。

所以用与当前特定行相关的单个字典值替换循环。例如,您可以设置 dict = [[result objectForKey:@"data"] objectAtIndex:indexPath.row] 并删除 for 循环,如下所示:

NSDictionary *dict = [[result objectForKey:@"data"] objectAtIndex:indexPath.row];

NSString *name = [[dict objectForKey:@"user"] objectForKey:@"name"];
NSString *highScore = [dict objectForKey:@"score"];
NSString *FBID = [[dict objectForKey:@"user"] objectForKey:@"id"];
NSString *imageURL = [[NSString alloc] initWithFormat:@"https://graph.facebook.com/%@/picture",FBID];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
[imageURL release];
self.facebookFullName = name;
self.facebookHighScore = highScore;
self.facebookProfilePicture = image;
cell.FBPlayerPhoto.image = facebookProfilePicture;
cell.FBPlayerName.text = facebookFullName;
cell.FBPlayerScore.text = [NSString stringWithFormat:@"$%@ Gold Coins",facebookHighScore];

编辑:还有一些其他的事情,我们正在做……

(1) 你在这里犯了类似的错误:

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"FBLeaderBoardCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[FBLeaderBoardCell class]]){
cell = (FBLeaderBoardCell *)currentObject;

}
}

通过在循环的每次迭代期间重新填充单元格对象。那是不必要的。按照此处提到的方法使用 xib 正确加载自定义单元格:https://stackoverflow.com/a/1939305/2274694

(2) 您实际上可能想在代码中使用不同的方法获取图形路径结果,原因有两个:#1 让您的应用程序在加载每一行时调用 Facebook 图形路径没有意义。更重要的是,在您的情况下,#2 为了在 numberOfRowsInSection: 期间动态找出行数,您无论如何都需要预取结果。

所以我建议在每次加载表之前获取图形路径结果,在 block 中插入一个[tableView reloadData]语句,将结果保存为类变量,并设置行数结果。计数。

关于ios - 如何让我的 Facebook GraphAPI 数据在 UITableView 中不重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20790989/

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