0
我试图用UITableView
和一些JSON对象 进行项目提要,但是当我尝试用JSON数据填充我的自定义单元格的实例时,UILabel
s不会改变他们的文字。UITableViewCells不采取自定义值
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:'方法中。 –
您需要在加载数据 – Paulw11
后在表上调用'reloadData',现在没有任何权利。因为即时通讯试图在'viewdidload'方法内的for循环中执行此操作@RoboticCat –