【问题标题】:Incorrect view of PFUser username in UILabelUILabel 中 PFUser 用户名的错误视图
【发布时间】:2014-09-10 16:05:56
【问题描述】:

{用户名 = ; , 电子邮件 = ;

我正在开发一个一对多的关系博客应用程序。我已经编写了与我的自定义 TableViewCell 一起使用的所有内容,包括 UILabel 的 nameLabel(显示博客文章的作者)、timeLabel(显示 createdAt 日期/字符串的查询结果)和 textContent(显示也是来自 Parse Class 对象的查询)。一切正常...除了 nameLabel 返回

显然,此返回值大于自定义单元格中显示的 UILabel。请帮我弄清楚这一点,因为我想要做的就是查询我的“作者”对象的值,它是指向我的 _User 类的指针,PFUser 数据存储在 Parse 上。另外,如果我需要更多解释,请询问,我会尽力而为,因为我是一个新开发人员。谢谢!

哇...如果我有 10 个声望,我可以发布快照,这样每个人都会知道如何回答我。

![ALPostsQueryTableViewController.m file:][2]


#import "ALPostsQueryTableViewController.h"
#import "ALPostsTableViewCell.h"

@interface ALPostsQueryTableViewController ()

@end

@implementation ALPostsQueryTableViewController

- (id)initWithCoder:(NSCoder *)aCoder {
    self = [super initWithCoder:aCoder];
    if (self) {
        // Customize the table

        // The className to query on
        self.parseClassName = @"Post";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = YES;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = YES;

        // The number of objects to show per page
        self.objectsPerPage = 25;
    }
    return self;
}

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    // If no objects are loaded in memory, we look to the cache
    // first to fill the table and then subsequently do a query
    // against the network.
    if ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByDescending:@"createdAt"];
    [query includeKey:@"author"];

    return query;
}

#pragma mark - Table view data source

- (ALPostsTableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object {
    static NSString *CellIdentifier = @"Cell";

    ALPostsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ALPostsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }

    cell.textView.text = [object objectForKey:@"textContent"];

    cell.nameLabel.text = [NSString stringWithFormat:@"%@",
                                 [object objectForKey:@"author"]];
    NSLog(@"Username is %@", [object objectForKey:@"author"]);

    NSDate *created = [object createdAt];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EEE, MMM d, h:mm a"];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@", [dateFormat stringFromDate:created]];

    return cell;
}

@end

【问题讨论】:

  • 好吧,我在这里有一个截图......它也回来了
  • 这与我在这里为不同用户回答的代码几乎相同:stackoverflow.com/questions/24826773/…您是共享代码还是 SO 用户?
  • @danh 所以我正在寻找 findObjectsInBackground 方法?我知道我需要找到用户名对象,但是如何将其缩小到“用户”解析类的“用户名”对象。它返回给出(Null)响应的用户名和电子邮件字段。我看过 PFSubclassing,但对于 iOS 开发世界来说是新手,有点不知所措。我是否应该尝试将“用户”类子类化为更窄的“用户名”类并调用该特定对象。如果是这样,当我还从“Post”类中​​调用对象时,如何将其实现到 tableViewCell 中。对不起……
  • 我还认为它可能会将所有字符串值从指针“author”获取到我的“User”类,但它也会包含 password = “example”,因为这是一个字符串值,但是它没有...
  • 您还控制“Mike J Aspinall”用户帐户吗?或者这是某个地方的一些示例代码,你和那个用户都在学习它?

标签: ios objective-c uilabel parse-platform


【解决方案1】:

听起来正在发生的事情是,返回的对象具有使用 includeKey 急切获取的关系。

获取它们的属性的方法就是你如何做,但你需要更进一步,询问相关“作者”对象的内部结构(恰好是PFUser)

// replace your code that sets the nameLabel text with this code...
PFUser *author = [object objectForKey:@"author"];
NSString *authorsUsername = author.username;
cell.nameLabel.text = authorsUsername;

【讨论】:

  • 非常感谢!!!它工作得很好。如果我有 15 个代表,我会投票一百万次。哈现在我不必把我的电脑扔到任何东西上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
相关资源
最近更新 更多