0

Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"ConvoreCell";

ConvoreCell * cell = (ConvoreCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ConvoreCell" owner:nil options:nil];
    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell = (ConvoreCell *) currentObject;
            break;
        }
    }
}

NSMutableDictionary *cellValue = [results objectAtIndex:indexPath.row];

NSString *picURL = [[cellValue objectForKey:@"creator"] objectForKey:@"img"];
if ([picURL isEqualToString:@"https://secure.gravatar.com/avatar/1f043010eb1652b3fab3678167dc0487/?default=https%3A%2F%2Fconvore.com%2Fmedia%2Fimages%2Feric.png&s=80"])
    picURL = @"https://convore.com/media/images/eric.png";

if ((picURL != (NSString *) [NSNull null]) && (picURL.length !=0)) {
    NSLog(@"%@", picURL);
    NSData *imgData = [[[NSData dataWithContentsOfURL:
                         [NSURL URLWithString:
                          picURL]] autorelease] retain];
    [cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];

} else {
    cell.pic = nil;
}


//NSLog(@"Name is : %@", [cellValue objectForKey:@"name"]);
cell.title.text = [cellValue objectForKey:@"name"];
cell.info.text = (@"Created by: %@", [[cellValue objectForKey:@"creator"] objectForKey:@"name"]);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;



// Configure the cell...

return cell;

}

For some reason the image is not showing up. What am I doing wrong here? The URL of the image is valid, I checked.

Also I don't want to have the accessory view on the TableViewCell, I did specify that as no in IB, but it still shows up... i set the accessory attribute to none already

aherlambang
  • 14,290
  • 50
  • 150
  • 253

4 Answers4

1

you have some issues in your code.

NSData *imgData = [[[NSData dataWithContentsOfURL:
                     [NSURL URLWithString:
                      picURL]] autorelease] retain];

the former is not a real issue (maybe by coincidence?!), but it's ugly and confusing. get rid of the autorelease and the retain. The object you get from dataWithContentsOfURL: is already autoreleased.


[cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];

And your real issue might be this. From what I know you should call init exactly one time; after you've allocated an object.

So if cell.pic is an UIImageView I would try to change the code to something like this:

cell.pic.image = [[[UIImage alloc] initWithData:imgData] autorelease];

Once you see your images you should change your code so that it saves the images in the data you use to populate the cell. You are downloading the image every time a cell appears. That's something you shouldn't do. But this is beyond this question.

EDIT:

Also I don't want to have the accessory view on the TableViewCell, I did specify that as no in IB, but it still shows up... i set the accessory attribute to none already

You do? But I can see this line of code:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0

You should be able to get an UIImage by just

[cell.pic initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSUrl NSURL picURL]]]];
João Moreno
  • 691
  • 8
  • 17
0

You're not showing your cellForRowAtIndexPath method, which is the key to analyzing what's going on. The typical paradigm is that UITableViewCell objects get reused repeatedly by the UITableView. You should be setting the UIImage into the cell when the UITableView calls cellForRowAtIndexPath.

Bogatyr
  • 19,255
  • 7
  • 59
  • 72
0

The problem causing your immediate crash is that the nib for ConvoreCell is trying to set an IBOutlet you have declared but finding no way to actually set it. I would check that you have synthesized your pic property. There are other problems here. The line

    [cell.pic initWithImage:[[UIImage alloc] initWithData:imgData]];

Is wrong; you do not call init on an object that already exists, but only when you are allocating an object. Do something like

cell.pic = [[[WhateverClassPicIS alloc] initWithImage:[[UIImage alloc] initWithData:imgData]];

Also, you are using NSData dataWithContentsOfURL on the main thread. This will lock up your user interface for an indeterminate time as you connect to the network. Use asynchronous calls instead.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81