将JSON Url中的图像显示到tableview中

时间:2016-05-26 11:06:01

标签: ios objective-c uiimageview

我收到了这个

的3个错误
1) " [__NSCFString size]: unrecognized selector sent to instance "

2) " [NSNull length]: unrecognized selector sent to instance "

3) " [__NSCFString size]: unrecognized selector sent to instance "

下面是我的代码。你能不能帮我爱这个问题。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section {
    return matchesProfileArr.count;;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    displayCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
    forIndexPath:indexPath];

    if(!cell) {
        cell = [[displayCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:CellIdentifier];
    }

    NSMutableDictionary *dict= [matchesProfileArr objectAtIndex:indexPath.row];            
    NSMutableDictionary *dictdetails=[dict objectForKey:@"ABSAdvancedSearch"];

    cell.lblAge.text = [dictdetails objectForKey:@"age"];
    cell.lblLocation.text = [dictdetails objectForKey:@"CustomerCity"];
    cell.lblProfession.text = [dictdetails objectForKey:@"rb_profession_title"];
    cell.lblEducation.text = [dictdetails objectForKey:@"rb_education_title"];
    cell.lblFullName.text = [dictdetails objectForKey:@"Username"];
    cell.profileImage.image = [dictdetails objectForKey:@"profile_pic"];

    return cell;
}

3 个答案:

答案 0 :(得分:1)

您正在向图像发送字符串。您必须从网址加载图片,如下所示:

NSString *urlImage = [dictdetails objectForKey:@"profile_pic"];
cell.profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlImage]]];

编辑:这是同步图像加载。

我希望您建议您使用AFNetworking方法从网址加载图片:

[cell. profileImage setImageWithURL:[NSURL URLWithString:urlImage]placeholderImage:[UIImage imageNamed:@"someImage"]];

或者:

dispatch_async(dispatch_get_main_queue(), ^{

        cell.profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlImage]]];
    });

答案 1 :(得分:1)

dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[dictdetails objectForKey:@"profile_pic"]]];
    if ( data == nil )
        return;
    dispatch_async(dispatch_get_main_queue(), ^{

        cell.image = [UIImage imageWithData: data];
    });

});

使用此代码,如果您使用dispatch_async

,您的ui将不会卡住

答案 2 :(得分:0)

除了@Stefan和@ balkaranSingh的答案之外。我认为您的@"age"密钥持有NSNumber而不是NSString,因此将其转换为字符串。

cell.lblAge.text = [[dictdetails objectForKey:@"age"] stringValue];