我尝试使用UITableView
和一些JSON对象制作商品Feed,
但是当我尝试使用JSON数据填充自定义单元格的实例时,UILabel
不会更改其文本。
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
我做错了什么?
答案 0 :(得分:0)
您已在 FeedPosts 中声明了多个 UILabel 。
FeedPosts *posts = [FeedPosts InitPost];
NSLog(@"feed check %@" ,[bpdDictionary objectForKey:@"name"]);
posts.postTitle = [bpdDictionary objectForKey:@"name"];
在以下代码中,您将 NSString (文本)分配给标签对象:
posts.postTitle.text = [bpdDictionary objectForKey:@"name"];
相反,您应该为这些标签设置文字:
postMessage
同样适用于postDatum
和MultiValuedMap<String,String> params = uriInfo.getQueryParameters()
。