我遇到了尝试从JSON加载缩略图到UITableView单元格的问题。单元格的UI是一个xib文件。我无法解析JSON的缩略图部分并将其显示在表格视图中。自从我做到这一点以来(我倾向于在比典型的iOS应用程序更多的游戏上工作),而最后一次我做的是稍微不同的实现。从JSON中将缩略图图像加载到UITableView中
JSON:
{
"data" :
[
{
"user_id" : "3",
"username" : "Alex Perez",
"avatar_url" : "http://mywebsite.com/images/alex_avatar.png",
"message" : "Hello there?"
},
{
"user_id" : "4",
"username" : "Jon Doe",
"avatar_url" : "http://mywebsite.com/images/john_avatar.png",
"message" : "I'm here now"
},
{
"user_id" : "2",
"username" : "Sam Givens",
"avatar_url" : "http://mywebsite.com/images/sam_avatar.png",
"message" : "Can we have a meeting around 2?"
},
....
而且在Data.h数据:
#import <Foundation/Foundation.h>
@interface Data : NSObject
@property (nonatomic, readwrite) int user_id;
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *avatar_url;
@property (nonatomic, strong) NSString *message;
- (void)loadWithDictionary:(NSDictionary *)dict;
@end
Data.m:
#import "Data.h"
@implementation Data
- (void)loadWithDictionary:(NSDictionary *)dict {
self.user_id = [[dict objectForKey:@"user_id"] intValue];
self.username = [dict objectForKey:@"username"];
self.avatar_url = [dict objectForKey:@"avatar_url"];
self.message = [dict objectForKey:@"message"];
}
@end
这里宣布时,我遇到的问题该属性并将属性分配给数据,然后可以将其传递给要显示它的View Controller。我得到的编译器警告不兼容的指针类型分配给来自NSString *的UIImage *。我不明白为什么Xcode不会抱怨UILabel和UITextView等其他属性。
Cell.m
#import "Cell.h"
@interface Cell()
@property (nonatomic) int userID;
// UIImageView property created from Interface Builder
@property (strong, nonatomic) IBOutlet UIImageView *avatarImage;
@property (nonatomic, strong) IBOutlet UILabel *usernameLabel;
@property (nonatomic, strong) IBOutlet UITextView *messageTextView;
@end
@implementation Cell
- (void)loadWithData:(Data *)data {
self.userID = data.user_id;
// Here I get "Incompatible pointer types assigning to UIImage * from NSString*
self.avatarImage.image = data.avatar_url;
self.usernameLabel.text = data.username;
self.messageTextView.text = data.message;
}
@end
最后它显示MessagesViewController:
@interface MessagesViewController()
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *loadedChatData;
@end
@implementation MessagesViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.loadedChatData = [[NSMutableArray alloc] init];
[self loadJSONData];
}
- (void)loadJSONData {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSError *error = nil;
NSData *rawData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
id JSONData = [NSJSONSerialization JSONObjectWithData:rawData options:NSJSONReadingAllowFragments error:&error];
[self.loadedChatData removeAllObjects];
if ([JSONData isKindOfClass:[NSDictionary class]]) {
NSDictionary *jsonDict = (NSDictionary *)JSONData;
NSArray *loadedArray = [jsonDict objectForKey:@"data"];
if ([loadedArray isKindOfClass:[NSArray class]])
{
for (NSDictionary *chatDict in loadedArray)
{
Data *chatData = [[Data alloc] init];
[chatData loadWithDictionary:chatDict];
[self.loadedChatData addObject:chatData];
}
}
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
Cell *cell = nil;
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
cell = (Cell *)[nib objectAtIndex:0];
}
Data *data = [self.loadedChatData objectAtIndex:[indexPath row]];
[cell loadWithData:data];
return cell;
}
这就是它!我之前发现了类似的解决方案,但无法正常工作。 – Paul 2015-03-19 05:26:12
由于可能会有很多头像,因此一次可能需要一段时间才能加载所有图像。您可以使用[AsyncImageView](https://github.com/nicklockwood/AsyncImageView/tree/master/Examples)进行图片加载。而且更简单[SDWebImage](https://github.com/rs/SDWebImage)。 :) – 2015-03-19 05:36:26