【问题标题】:UITableViewCells don't take custom valueUITableViewCells 不采用自定义值
【发布时间】:2015-05-03 23:57:13
【问题描述】:

我正在尝试使用 UITableView 和一些 JSON 对象制作项目提要, 但是当我尝试使用 JSON 数据填充自定义单元格的实例时,UILabels 不会更改其文本。

JSON 已经过测试并且可以工作。它通过循环并创建适量的行。但文本不会更改为 JSON 文件中的文本。

这是我的代码:

feed.m

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *FeedURL = [NSURL URLWithString:@"http://www.personeelsapp.jordivanderhek.com/company/bijcasper/nieuws.json"];
    NSData *jsonData = [NSData dataWithContentsOfURL:FeedURL];
    NSError *error = nil;
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    NSLog(@"%@", dataDictionary);

    self.posts = [NSMutableArray array];
    PostsArray = [dataDictionary objectForKey:@"feed"];

    for (NSDictionary *bpdDictionary in PostsArray) {
        // make new post object
        FeedPosts *posts = [FeedPosts InitPost];
        NSLog(@"feed check %@" ,[bpdDictionary objectForKey:@"name"]);
        posts.postTitle = [bpdDictionary objectForKey:@"name"];
        posts.postProfilepic = [bpdDictionary objectForKey:@"profilePic"];
        posts.postDatum = [bpdDictionary objectForKey:@"timeStamp"];
        posts.postMessage = [bpdDictionary objectForKey:@"status"];
        posts.postImage = [bpdDictionary objectForKey:@"image"];
        [self.posts addObject:posts];
    }
}

 […]

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section {
    return [self.posts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Cellindentifier = @"PostCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellindentifier forIndexPath:indexPath];

    // Configure the cell...
    FeedPosts *posts = [self.posts objectAtIndex:indexPath.row];

   cell.postTitle.text = @"test title";
   cell.postDatum.text = posts.postDatum.text;
   cell.postMessage.text = posts.postMessage.text;
return cell;
}
}

FeedPosts.h

@property (strong, nonatomic) IBOutlet UILabel *postTitle;
@property (strong, nonatomic) IBOutlet UILabel *postMessage;
@property (strong, nonatomic) IBOutlet UIImageView *postImage;
@property (strong, nonatomic) IBOutlet UIImageView *postProfilepic;
@property (strong, nonatomic) IBOutlet UILabel *postDatum;

// designated init
+ (id) InitPost;

FeedPosts.m

+ (id) InitPost {
    // init new feed item
    return [[self alloc]init];
}

遇到以下错误:

-[__NSCFString text]: unrecognized selector sent to instance

我做错了什么?

【问题讨论】:

  • 您的代码在哪里显示单元格 UILabels 的设置以及新文本?请将缺少的代码添加到您的 tableView:cellForRowAtIndexPath: 方法中。
  • 加载数据后需要在你的表上调用reloadData
  • 目前没有。因为我试图在 viewdidload 方法 @RoboticCat 的 for 循环中执行此操作
  • @JordivanderHek 嗯?您需要在 cellForRowAtIndexPath 方法中创建、设置和返回一个单元格。显示该代码。
  • @rmaddy 我无法重新定义 cellForRowAtIndexPath 中的任何内容。 'cell' 没有任何与我的标签关联的属性可供使用

标签: ios objective-c json uitableview


【解决方案1】:

您在 FeedPosts 中声明了多个 UILabel

@property (strong, nonatomic) IBOutlet UILabel *postTitle;

在以下代码中,您将 NSString(文本)分配给标签对象:

FeedPosts *posts = [FeedPosts InitPost];
NSLog(@"feed check %@" ,[bpdDictionary objectForKey:@"name"]);
posts.postTitle = [bpdDictionary objectForKey:@"name"];

相反,您应该为这些标签设置文本:

posts.postTitle.text = [bpdDictionary objectForKey:@"name"];

postMessagepostDatum 也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2021-03-02
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    相关资源
    最近更新 更多