我有一个friendslist视图控制器,它允许用户查看未决的朋友请求。首先使用isPending方法查看特定用户是否与当前用户有待处理的好友请求。
-(BOOL)isPending:(PFUser *)user
{
for (PFUser *pending in self.Pending){
if ([pending.objectId isEqualToString:user.objectId]){
return YES;
}
}
return NO;
}
在TableViewController的cellForRowAtIndexPath方法中调用此方法。我遇到的问题是,如果我在tableViewController中运行查询,则会正确显示待处理的用户。当我将查询移到单例数据源类时,我已经设置了一个委托方法,当从查询返回数据时将调用该方法。
-(void)pendingFriendsQuarryDidFinishWithData{
self.Pending = [NSMutableArray arrayWithArray:dataHolder.getPendingFriendsList];
[self.tableView reloadData];
}
调用reloadData不会导致复选标记出现在用户名旁边。
编辑:这是cellForRowAtIndexPath的代码。它不会随单例改变。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
cell.detailTextLabel.text = user.email;
cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
if([self isPending:user]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
真正改变的是如何获取数据。没有单例,这里是代码
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
/*
self.currentUser = [PFUser currentUser];
if (self.currentUser != nil){
PFQuery *userQuery = [PFUser query];
PFQuery *pendingUser = [PFUser query];
PFRelation *friendRelation = [self.currentUser relationForKey:@"friendRelation"];
PFQuery *existingFriends = [friendRelation query];
PFQuery *pendingFriends = [PFQuery queryWithClassName:@"FriendRequest"];
userQuery.cachePolicy = kPFCachePolicyCacheThenNetwork;
pendingFriends.cachePolicy = kPFCachePolicyCacheThenNetwork;
[pendingFriends whereKey:@"fromUser" equalTo:self.currentUser.objectId];
[pendingFriends whereKey:@"status" equalTo:@"Pending"];
[userQuery whereKey:@"objectId"doesNotMatchKey:@"toUser" inQuery:pendingFriends];
[userQuery whereKey:@"objectId" doesNotMatchKey:@"objectId" inQuery:existingFriends];
[userQuery orderByAscending:@"username"];
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@" Error %@ %@", error, [error userInfo] );
}else{
NSLog(@"All Users Array Starts Here: %@", objects);
self.allUsers = objects;
[self.tableView reloadData];
}
}];
[pendingUser whereKey:@"objectId" matchesKey:@"toUser" inQuery:pendingFriends];
[pendingUser orderByAscending:@"username"];
[pendingUser findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error){
NSLog(@"%@ %@", error, [error userInfo]);
}else{
[self.Pending addObjectsFromArray:objects];
NSLog(@"Pending Friends Array Stars here: %@", self.Pending);
}
}];
}
*/
}
与单身人士
- (void)viewDidLoad
{
dataHolder = [DataHolder sharedInstance];
dataHolder.delegate = self;
self.friends = [NSMutableArray new];
self.allUsers = [NSMutableArray new];
self.allUsers = [NSMutableArray arrayWithArray:dataHolder.getAllUsersWithQuarry];
self.Pending = [NSMutableArray new];
self.Pending = [NSMutableArray arrayWithArray:dataHolder.getPendingFriendsListWithQuarry];
[super viewDidLoad];
NSLog(@"Now in Connection Editor");
}
-(void)allUsersQuarryDidFinishWithData{
self.allUsers = [NSMutableArray arrayWithArray:dataHolder.getAllUsers];
[self.tableView reloadData];
}
-(void)pendingFriendsQuarryDidFinishWithData{
self.Pending = [NSMutableArray arrayWithArray:dataHolder.getPendingFriendsList];
[self.tableView reloadData];
}
最佳答案
愚蠢的问题,您是否将tableviewcontroller的数据源设置为指向单例数据源?
tableview.datasource = [your singleton class instance]
并确保您在调试器中进入它。
希望这可以帮助。
关于ios - UitableViewCell无法正确显示配件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24274759/