Muju Muju - 4 years ago 129
iOS Question

Why Image is not Shown in cellforRowAtIndexPath in objective C

I am new in iOS and I am facing problem regarding to show image in the Image View from Web Service.I am using code like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *STI=@"STI";
NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
if (cell == nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

NSString *strImgURLAsString = [NewsImageArray objectAtIndex:indexPath.row];
[strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imgURL = [NSURL URLWithString:strImgURLAsString];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
img = [[UIImage alloc] initWithData:data];
if (img==nil) {
// img=[UIImage imageNamed:@"userimage.png"];

// pass the img to your imageview

cell.Headlbl.text=[NSString stringWithFormat:@"%@",[headarray objectAtIndex:indexPath.row]];

NSString *aux = [shortnamearray objectAtIndex:indexPath.row];
NSString * htmlString = @"<html><body>";
NSString *htmlString2=@"</body></html>";
NSString * NewString=[NSString stringWithFormat:@"%@%@%@",htmlString,aux,htmlString2];

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NewString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

cell.bodylbl.textColor=[UIColor whiteColor];
[cell.bodylbl setFont:[UIFont fontWithName:@"Arial" size:16]];

cell.backgroundColor = cell.contentView.backgroundColor;

// cell.bodylbl.text=[NSString stringWithFormat:@"%@",[shortnamearray objectAtIndex:indexPath.row]];
// cell.randrid.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
return cell;


Image Path - Schaefer, of his role in the slaying, for which he is serving 18 year.png,%20of%20his%20role%20in%20the%20slaying,%20for%20which%20he%20is%20serving%2018%20year.png

enter image description here
When Image Does not contain any gap it shows image
But when the Image contain any gap it not show image.How to solve this problem.Thanks in Advance!

Answer Source

There is no need to use any other library etc. Your code is mistake on this line:

[strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Have a look at this what does it do? Nothing. You should reassign the variable strImgURLAsString to this then it should work:

 strImgURLAsString = [strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download