Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

uitableviewcell dequeue

stuffradiostuffradio Posts: 125Registered Users
I know there are a lot of threads about this, but I still don't understand the concept.

I have one array, it contains all the thumbs. I have a custom cell class with a custom cell with 4 uiimageviews. So, every row, I want to pull as many images into each row as I can.

If the element exists, the uiimageview gets that image. The problem is, when I scroll, it screws everything up, and it crashes when I click on the image because of that.

You can see all my attempts here in those code. This is my cellforrowatindexpath method


MainTableCells *cell = [theTable dequeueReusableCellWithIdentifier:@\"customCell\"];

if (cell == nil)
{
cell = [[[MainTableCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@\"customCell\"] autorelease];
tablerow++;
NSLog(@\"alloc tablecell\");
}

if (indexPath.row == 0)
{
// [[cell dynamicText] setTag:1];
[[cell firstImage] setTag:1];
[[cell secondImage] setTag:2];
[[cell thirdImage] setTag:3];
[[cell fourthImage] setTag:4];
} else if (indexPath.row == 1)
{
[[cell firstImage] setTag:5];
[[cell secondImage] setTag:6];
[[cell thirdImage] setTag:7];
[[cell fourthImage] setTag:8];
} else if (indexPath.row == 2)
{
[[cell firstImage] setTag:9];
[[cell secondImage] setTag:10];
[[cell thirdImage] setTag:11];
[[cell fourthImage] setTag:12];
}else if (indexPath.row == 3)
{
[[cell firstImage] setTag:13];
[[cell secondImage] setTag:14];
[[cell thirdImage] setTag:15];
[[cell fourthImage] setTag:16];
} else if (indexPath.row == 4)
{ [[cell firstImage] setTag:17];
[[cell secondImage] setTag:18];
[[cell thirdImage] setTag:19];
[[cell fourthImage] setTag:20];
} else if (indexPath.row == 5)
{
[[cell firstImage] setTag:21];
[[cell secondImage] setTag:22];
[[cell thirdImage] setTag:23];
[[cell fourthImage] setTag:24];
} else if (indexPath.row == 6)
{
[[cell firstImage] setTag:25];
[[cell secondImage] setTag:26];
[[cell thirdImage] setTag:27];
[[cell fourthImage] setTag:28];
} else if (indexPath.row == 7)
{
[[cell firstImage] setTag:29];
[[cell secondImage] setTag:30];
[[cell thirdImage] setTag:31];
[[cell fourthImage] setTag:32];
}


//NSLog(@\"table count %d\",indexPath.row);
// if ([didFill isEqualToString:@\"no\"])
//{
//for (NSString *key in thumblist)
//{

//if (tablerow > [thumblist count] || tablerow == [thumblist count]) { break;}
// if (tablerow == 0 || tablerow == 4 || tablerow == 8 || tablerow == 12 || tablerow == 16 || tablerow == 19 || tablerow == 23 || tablerow == 26 || tablerow == 29){
if ([thumblist objectAtIndex:tablerow] != nil)
{
NSURL *firstImageURL = [NSURL URLWithString:[thumblist objectAtIndex:tablerow]];
NSData *firstImageData = [[NSData alloc] initWithContentsOfURL:firstImageURL];
UIImage *firstImage = [[UIImage alloc] initWithData:firstImageData];
[[cell firstImage] setBackgroundImage:firstImage forState:UIControlStateNormal];
[firstImage release];
[firstImageData release];
}
//}
if (tablerow < ([thumblist count]-1))
{
tablerow++;
}
NSLog(@\"table image count %d\", tablerow);

// if (tablerow == 1 || tablerow == 5 || tablerow == 9 || tablerow == 13 || tablerow == 17 || tablerow == 20 || tablerow == 24 || tablerow == 27 || tablerow == 30){
if ([thumblist objectAtIndex:tablerow] != nil)
{
NSURL *secondImageURL = [NSURL URLWithString:[thumblist objectAtIndex:tablerow]];
NSData *secondImageData = [[NSData alloc] initWithContentsOfURL:secondImageURL];
UIImage *secondImage = [[UIImage alloc] initWithData:secondImageData];
[[cell secondImage] setBackgroundImage:secondImage forState:UIControlStateNormal];
[secondImage release];
[secondImageData release];
}
//}
if (tablerow < ([thumblist count]-1))
{
tablerow++;
}
//if (tablerow == 2 || tablerow == 6 || tablerow == 10 || tablerow == 14 || tablerow == 18 || tablerow == 21 || tablerow == 25 || tablerow == 28 || tablerow == 31){
if ([thumblist objectAtIndex:tablerow] != nil)
{
NSURL *thirdImageURL = [NSURL URLWithString:[thumblist objectAtIndex:tablerow]];
NSData *thirdImageData = [[NSData alloc] initWithContentsOfURL:thirdImageURL];
UIImage *thirdImage = [[UIImage alloc] initWithData:thirdImageData];
[[cell thirdImage] setBackgroundImage:thirdImage forState:UIControlStateNormal];
[thirdImage release];
[thirdImageData release];
}
// }
if (tablerow < ([thumblist count]-1))
{
tablerow++;
}
// if (tablerow == 3 || tablerow == 7 || tablerow == 11 || tablerow == 15 || tablerow == 19 || tablerow == 22 || tablerow == 26 || tablerow == 29 || tablerow == 32){
if ([thumblist objectAtIndex:tablerow] != nil)
{
NSURL *fourthImageURL = [NSURL URLWithString:[thumblist objectAtIndex:tablerow]];
NSData *fourthImageData = [[NSData alloc] initWithContentsOfURL:fourthImageURL];
UIImage *fourthImage = [[UIImage alloc] initWithData:fourthImageData];
[[cell fourthImage] setBackgroundImage:fourthImage forState:UIControlStateNormal];
[fourthImage release];
[fourthImageData release];
}
// }
if (tablerow < ([thumblist count]-1))
{
tablerow++;
}


NSLog(@\"nums %d\", tablerow);
//NSLog(@\"Did update %@\", didUpdate);

//NSLog(@\"image num %d:\", tablerow);
/* if (tablerow < ([thumblist count]-1)) {
tablerow++;
} else if (tablerow == ([thumblist count]-1) || tablerow >= ([thumblist count]-1)) {didFill=@\"yes\"; NSLog (@\"break...\"); break;}
if (negate == 1) { negate = 4; break; } else { negate--; }*/
// NSLog(@\"Negation: %d\",negate);
//}
//}
if ([didUpdate isEqualToString:@\"NO\"])
{
//[self populateTable];
//NSLog(@\"Description %@\", [thumblist description]);
didUpdate =@\"YES\";
}

return cell;


Edit: I am updating to say I added a try catch statement around everything before return cell so it doesn't crash the app. I still need to know about not having it re-order the pictures every time I scroll though.
Post edited by stuffradio on

Replies

  • BrianSlickBrianSlick Posts: 9,302Tutorial Authors, Registered Users
        if (indexPath.row == 0)
    {
    // [[cell dynamicText] setTag:1];
    [[cell firstImage] setTag:1];
    [[cell secondImage] setTag:2];
    [[cell thirdImage] setTag:3];
    [[cell fourthImage] setTag:4];
    } else if (indexPath.row == 1)
    {
    [[cell firstImage] setTag:5];
    [[cell secondImage] setTag:6];
    [[cell thirdImage] setTag:7];
    [[cell fourthImage] setTag:8];
    } else if (indexPath.row == 2)
    {
    [[cell firstImage] setTag:9];
    [[cell secondImage] setTag:10];
    [[cell thirdImage] setTag:11];
    [[cell fourthImage] setTag:12];
    }else if (indexPath.row == 3)
    {
    [[cell firstImage] setTag:13];
    [[cell secondImage] setTag:14];
    [[cell thirdImage] setTag:15];
    [[cell fourthImage] setTag:16];
    } else if (indexPath.row == 4)
    { [[cell firstImage] setTag:17];
    [[cell secondImage] setTag:18];
    [[cell thirdImage] setTag:19];
    [[cell fourthImage] setTag:20];
    } else if (indexPath.row == 5)
    {
    [[cell firstImage] setTag:21];
    [[cell secondImage] setTag:22];
    [[cell thirdImage] setTag:23];
    [[cell fourthImage] setTag:24];
    } else if (indexPath.row == 6)
    {
    [[cell firstImage] setTag:25];
    [[cell secondImage] setTag:26];
    [[cell thirdImage] setTag:27];
    [[cell fourthImage] setTag:28];
    } else if (indexPath.row == 7)
    {
    [[cell firstImage] setTag:29];
    [[cell secondImage] setTag:30];
    [[cell thirdImage] setTag:31];
    [[cell fourthImage] setTag:32];
    }


    Absolutely, 100%, the wrong approach. Never do this again.

        //NSLog(@\"table count %d\",indexPath.row);
    // if ([didFill isEqualToString:@\"no\"])
    //{
    //for (NSString *key in thumblist)
    //{

    //if (tablerow > [thumblist count] || tablerow == [thumblist count]) { break;}
    // if (tablerow == 0 || tablerow == 4 || tablerow == 8 || tablerow == 12 || tablerow == 16 || tablerow == 19 || tablerow == 23 || tablerow == 26 || tablerow == 29){


    Ah yes, nothing makes a forum post easy to read like including tons of commented out code. Auto-color coding is a feature of Xcode. I'm not looking at Xcode right now, am I?

    if ([thumblist objectAtIndex:tablerow] != nil)


    You can't have nil in an array, so this is pointless.

    As for the rest... dear lord you seem to like making things hard on yourself.

    You want to do the work in preparing your data array. You want to remove the work from your table delegate methods. There are several approaches. One would be to make an array of arrays of image names. Ex:

    mainArray
    imageNamesArrayForRow1
    imageName1
    imageName2
    imageName3
    imageName4
    imageNamesArrayForRow2
    imageName1
    imageName2
    imageName3
    imageName4
    ...


    So here in cellForRow when you are ready to grab images, you'd approach it like so:

    NSArray *imageNames = [[self mainArray] objectAtIndex:row];
    NSString *imageName1 = [imageNames objectAtIndex:0];
    NSString *imageName2 = [imageNames objectAtIndex:1];
    NSString *imageName3 = [imageNames objectAtIndex:2];
    NSString *imageName4 = [imageNames objectAtIndex:3];


    Much easier. However, there will be a problem if the total number of images is not a multiple of 4. You'll need to do a lot of range checking on the imageNames array before trying to grab any image. An alternative that might be a bit safer would be to use dictionaries instead:

    mainArray
    dictionaryForRow1
    < image names >
    dictionaryForRow2
    < image names >
    ...


    Same idea to extract:

    NSDictionary *imageNames = [[self mainArray] objectAtIndex:row];
    NSString *imageName1 = [imageNames objectForKey:@\"Image1\"];
    NSString *imageName2 = [imageNames objectForKey:@\"Image2\"];
    NSString *imageName3 = [imageNames objectForKey:@\"Image3\"];
    NSString *imageName4 = [imageNames objectForKey:@\"Image4\"];


    If the name isn't there, you'll get back nil, so you won't create an image, so you'll have a built-in bonus of automatically clearing out unused image views.
  • stuffradiostuffradio Posts: 125Registered Users
    Thank you. I have refactored my code. Not sure if this is the most efficient way yet, but it isn't switching rows all the time. Now the problem is that it is a bit slow when scrolling up and down.

    Here is how I setup the array

    for (NSString *key in thumblist)
    {

    if (thearray == 0)
    {
    [row0 addObject:key];
    } else if (thearray == 1)
    {
    [row1 addObject:key];
    } else if (thearray == 2)
    {
    [row2 addObject:key];
    } else if (thearray == 3)
    {
    [row3 addObject:key];
    } else if (thearray == 4)
    {
    [row4 addObject:key];
    } else if (thearray == 5)
    {
    [row5 addObject:key];
    } else if (thearray == 6)
    {
    [row6 addObject:key];
    } else if (thearray == 7)
    {
    [row7 addObject:key];
    }

    if (decre == 1)
    {
    decre=4;
    thearray++;
    } else {
    decre--;
    }
    }


    Here is cellforrowatindex

    MainTableCells *cell = [theTable dequeueReusableCellWithIdentifier:@\"customCell\"];
    @try {
    //static NSString *CellIdentifier = @\"customCell\";
    //NSString *rowContent = [[self signupList] objectAtIndex:[indexPath row]];
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil)
    {
    cell = [[[MainTableCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@\"customCell\"] autorelease];
    NSLog(@\"alloc tablecell\");
    }

    if (indexPath.row == 0)
    {
    NSURL *firstImageURL = [NSURL URLWithString:[row0 objectAtIndex:0]];
    NSData *firstImageData = [[NSData alloc] initWithContentsOfURL:firstImageURL];
    UIImage *firstImage = [[UIImage alloc] initWithData:firstImageData];
    [[cell firstImage] setBackgroundImage:firstImage forState:UIControlStateNormal];
    [firstImage release];
    [firstImageData release];

    NSURL *secondImageURL = [NSURL URLWithString:[row0 objectAtIndex:1]];
    NSData *secondImageData = [[NSData alloc] initWithContentsOfURL:secondImageURL];
    UIImage *secondImage = [[UIImage alloc] initWithData:secondImageData];
    [[cell secondImage] setBackgroundImage:secondImage forState:UIControlStateNormal];
    [secondImage release];
    [secondImageData release];

    NSURL *thirdImageURL = [NSURL URLWithString:[row0 objectAtIndex:2]];
    NSData *thirdImageData = [[NSData alloc] initWithContentsOfURL:thirdImageURL];
    UIImage *thirdImage = [[UIImage alloc] initWithData:thirdImageData];
    [[cell thirdImage] setBackgroundImage:thirdImage forState:UIControlStateNormal];
    [thirdImage release];
    [thirdImageData release];

    NSURL *fourthImageURL = [NSURL URLWithString:[row0 objectAtIndex:3]];
    NSData *fourthImageData = [[NSData alloc] initWithContentsOfURL:fourthImageURL];
    UIImage *fourthImage = [[UIImage alloc] initWithData:fourthImageData];
    [[cell fourthImage] setBackgroundImage:fourthImage forState:UIControlStateNormal];
    [fourthImage release];
    [fourthImageData release];

    } else if (indexPath.row == 1)
    {
    NSURL *firstImageURL = [NSURL URLWithString:[row1 objectAtIndex:0]];
    NSData *firstImageData = [[NSData alloc] initWithContentsOfURL:firstImageURL];
    UIImage *firstImage = [[UIImage alloc] initWithData:firstImageData];
    [[cell firstImage] setBackgroundImage:firstImage forState:UIControlStateNormal];
    [firstImage release];
    [firstImageData release];

    NSURL *secondImageURL = [NSURL URLWithString:[row1 objectAtIndex:1]];
    NSData *secondImageData = [[NSData alloc] initWithContentsOfURL:secondImageURL];
    UIImage *secondImage = [[UIImage alloc] initWithData:secondImageData];
    [[cell secondImage] setBackgroundImage:secondImage forState:UIControlStateNormal];
    [secondImage release];
    [secondImageData release];

    NSURL *thirdImageURL = [NSURL URLWithString:[row1 objectAtIndex:2]];
    NSData *thirdImageData = [[NSData alloc] initWithContentsOfURL:thirdImageURL];
    UIImage *thirdImage = [[UIImage alloc] initWithData:thirdImageData];
    [[cell thirdImage] setBackgroundImage:thirdImage forState:UIControlStateNormal];
    [thirdImage release];
    [thirdImageData release];

    NSURL *fourthImageURL = [NSURL URLWithString:[row1 objectAtIndex:3]];
    NSData *fourthImageData = [[NSData alloc] initWithContentsOfURL:fourthImageURL];
    UIImage *fourthImage = [[UIImage alloc] initWithData:fourthImageData];
    [[cell fourthImage] setBackgroundImage:fourthImage forState:UIControlStateNormal];
    [fourthImage release];
    [fourthImageData release];
    } else if (indexPath.row == 2)
    {
    NSURL *firstImageURL = [NSURL URLWithString:[row2 objectAtIndex:0]];
    NSData *firstImageData = [[NSData alloc] initWithContentsOfURL:firstImageURL];
    UIImage *firstImage = [[UIImage alloc] initWithData:firstImageData];
    [[cell firstImage] setBackgroundImage:firstImage forState:UIControlStateNormal];
    [firstImage release];
    [firstImageData release];

    NSURL *secondImageURL = [NSURL URLWithString:[row2 objectAtIndex:1]];
    NSData *secondImageData = [[NSData alloc] initWithContentsOfURL:secondImageURL];
    UIImage *secondImage = [[UIImage alloc] initWithData:secondImageData];
    [[cell secondImage] setBackgroundImage:secondImage forState:UIControlStateNormal];
    [secondImage release];
    [secondImageData release];

    NSURL *thirdImageURL = [NSURL URLWithString:[row2 objectAtIndex:2]];
    NSData *thirdImageData = [[NSData alloc] initWithContentsOfURL:thirdImageURL];
    UIImage *thirdImage = [[UIImage alloc] initWithData:thirdImageData];
    [[cell thirdImage] setBackgroundImage:thirdImage forState:UIControlStateNormal];
    [thirdImage release];
    [thirdImageData release];

    NSURL *fourthImageURL = [NSURL URLWithString:[row2 objectAtIndex:3]];
    NSData *fourthImageData = [[NSData alloc] initWithContentsOfURL:fourthImageURL];
    UIImage *fourthImage = [[UIImage alloc] initWithData:fourthImageData];
    [[cell fourthImage] setBackgroundImage:fourthImage forState:UIControlStateNormal];
    [fourthImage release];
    [fourthImageData release];
    } else if (indexPath.row == 3)
    {
    NSURL *firstImageURL = [NSURL URLWithString:[row3 objectAtIndex:0]];
    NSData *firstImageData = [[NSData alloc] initWithContentsOfURL:firstImageURL];
    UIImage *firstImage = [[UIImage alloc] initWithData:firstImageData];
    [[cell firstImage] setBackgroundImage:firstImage forState:UIControlStateNormal];
    [firstImage release];
    [firstImageData release];

    NSURL *secondImageURL = [NSURL URLWithString:[row3 objectAtIndex:1]];
    NSData *secondImageData = [[NSData alloc] initWithContentsOfURL:secondImageURL];
    UIImage *secondImage = [[UIImage alloc] initWithData:secondImageData];
    [[cell secondImage] setBackgroundImage:secondImage forState:UIControlStateNormal];
    [secondImage release];
    [secondImageData release];

    NSURL *thirdImageURL = [NSURL URLWithString:[row3 objectAtIndex:2]];
    NSData *thirdImageData = [[NSData alloc] initWithContentsOfURL:thirdImageURL];
    UIImage *thirdImage = [[UIImage alloc] initWithData:thirdImageData];
    [[cell thirdImage] setBackgroundImage:thirdImage forState:UIControlStateNormal];
    [thirdImage release];
    [thirdImageData release];

    NSURL *fourthImageURL = [NSURL URLWithString:[row3 objectAtIndex:3]];
    NSData *fourthImageData = [[NSData alloc] initWithContentsOfURL:fourthImageURL];
    UIImage *fourthImage = [[UIImage alloc] initWithData:fourthImageData];
    [[cell fourthImage] setBackgroundImage:fourthImage forState:UIControlStateNormal];
    [fourthImage release];
    [fourthImageData release];
    }
    }
    @catch (NSException *exception) {
    NSLog(@\"Message %@\", exception.reason);
    }
    return cell;


  • BrianSlickBrianSlick Posts: 9,302Tutorial Authors, Registered Users
    You're really not thinking this through. What if you have 400 images, what are you going to do then?

    If these images are being downloaded, that's why your scrolling is choppy. Worse, you're downloading at least some of the images multiple times if the user scrolls up and down.
  • stuffradiostuffradio Posts: 125Registered Users
    They are limited to 32 at a time on the PHP side.
  • BrianSlickBrianSlick Posts: 9,302Tutorial Authors, Registered Users
    That's not really the point. You have a lot of repetitive code, which means mistakes are multiplied that many times over. This code is not flexible at all, which means that when they inevitably increase the 32, you will have to do a lot of work. Better to write code that is capable of handling 300 images just as easily as it handles 3.
Sign In or Register to comment.