【发布时间】: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