Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-29-2011, 03:32 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 11
Kingpin is on a distinguished road
Lightbulb Weird behavior of iPhone tableview not occurring on iPad

I have build a grouped tableview with 2 sections.

There always have to be 1 selection in each of the sections, and there can't be more.

This is working rock steady when testing on the iPad.

I recently started developing the xib for the iPhone version.

Now things start getting weird. When scrolling the selections fly all over the place, disappears, and sometimes there are 4 selections in a section.

The exact same code is working flawlessly on the iPad.

Is there any known bugs that might make this occur?

Hope you can help me...

Code:
- (void)viewDidLoad
    {
         [super viewDidLoad];
         self.view.backgroundColor = [UIColor whiteColor]; 

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];

    NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Baskerville", @"Palatino", @"Times New Roman", @"Verdana", nil];

    NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

    NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", nil];

    NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];
[listOfItems addObject:countriesToLiveInDict];[listOfItems addObject:countriesLivedInDict];

    //Set the title
    self.navigationItem.title = @"Countries";  

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];    

     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    //SET Font 
    NSString *neverOpendFonts1 = [prefs objectForKey:@"neverOpendFonts1"];  

    if (![neverOpendFonts1 isEqualToString:@"1"]) {


            lastIndexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];

            UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1]; 

            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

            NSString *lastIndexPathString1 = [NSString stringWithFormat:@"1"];

            [prefs setObject:lastIndexPathString1 forKey:@"lastIndexPath1"];

            NSString *fontName = [NSString stringWithFormat:@"Palatino"];

            [prefs setObject:fontName forKey:@"fontName"];

            neverOpendFonts1 = [NSString stringWithFormat:@"1"];

            [prefs setObject:neverOpendFonts1 forKey:@"neverOpendFonts1"];

            [prefs synchronize];


    }

    else
    {
        NSInteger row = [[prefs objectForKey:@"lastIndexPath1"] intValue];

        lastIndexPath1 = [NSIndexPath indexPathForRow:row inSection:0];

        UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1]; 

        newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

    }

 //SET Font SIZE

    NSString *neverOpendFonts2 = [prefs objectForKey:@"neverOpendFonts2"];  

    if (![neverOpendFonts2 isEqualToString:@"1"]) {


        lastIndexPath2 =[NSIndexPath indexPathForRow:2 inSection:1];

        UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath2]; 

        newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

        NSString *lastIndexPathString2 = [NSString stringWithFormat:@"2"];

        [prefs setObject:lastIndexPathString2 forKey:@"lastIndexPath2"];

        NSString *fontSize = [NSString stringWithFormat:@"24.0"];  

        [prefs setObject:fontSize forKey:@"fontSize"];

        neverOpendFonts2 = [NSString stringWithFormat:@"1"];

        [prefs setObject:neverOpendFonts2 forKey:@"neverOpendFonts2"];

        [prefs synchronize];

    }

    else
    {

        NSInteger row2 = [[prefs objectForKey:@"lastIndexPath2"] intValue];

        lastIndexPath2 = [NSIndexPath indexPathForRow:row2 inSection:1];

        UITableViewCell *newCell2 = [myTableView cellForRowAtIndexPath:lastIndexPath2]; 

        newCell2.accessoryType = UITableViewCellAccessoryCheckmark; 


    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary =[listOfItems objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    return [array count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return[listOfItems count];
}

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


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

    //First get the dictionary object
    NSDictionary *dictionary =[listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    if (indexPath.section == 0)
    {
        switch (indexPath.row) {
            case 0:
            {
             cell.textLabel.font = [UIFont fontWithName:@"Baskerville" size:24];   
            }
                break;
            case 1:
            {
              cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:24];    
            }
                break;
            case 2:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:24];  
            }
                break;
            case 3:
            {
               cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:24];   
            }
                break;
        }

    }
    if (indexPath.section == 1) {

        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:16];   
            }
                break;
            case 1:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:20];    
            }
                break;
            case 2:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:24];  
            }
                break;
            case 3:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:28];   
            }
                break;
            case 4:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:32];   
            }
                break;
            case 5:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:36];   
            }
                break;
            case 6:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:40];   
            }
                break;
            case 7:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:44];   
            }
                break;
            case 8:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:48];   
            }
                break;
            case 9:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:52];   
            }
                break;
            case 10:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:56];   
            }
                break;
            case 11:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:60];   
            }
                break;
        }


    }


    return cell;
}
Kingpin is offline   Reply With Quote
Old 08-29-2011, 03:32 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 11
Kingpin is on a distinguished road
Default PART 2

Here is part 2 of my code...

Code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // CGFloat result;

    if (indexPath.section == 0)
    {
        return 50;

    }
    if (indexPath.section == 1) {

        switch (indexPath.row) {
            case 0:
            {
                return 50;   
            }
                break;
            case 1:
            {
                return 50;    
            }
                break;
            case 2:
            {
                return 50;  
            }
                break;
            case 3:
            {
                return 50;   
            }
                break;
            case 4:
            {
                return 50;   
            }
                break;
            case 5:
            {
                return 50;   
            }
                break;
            case 6:
            {
                return 50;   
            }
                break;
            case 7:
            {
                return 54;   
            }
                break;
            case 8:
            {
                return 58;   
            }
                break;
            case 9:
            {
                return 62;
            }
                break;
            case 10:
            {
                return 66;
            }
                break;
            case 11:
            {
                return 70;  
            }
                break;
        }


    }
    return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (section == 0) {
        return @"Font";
    }
    if (section == 1) {
        return @"Font Size";
    }
    else if (!section == 0 || !section == 1) {
        return @"Text";
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSInteger newRow = [indexPath row];

    if (indexPath.section == 0) {

        NSString *fontName;

        switch (indexPath.row) {
            case 0:
            {
                fontName = [NSString stringWithFormat:@"Baskerville"];   

            }
                break;
            case 1:
            {
               fontName = [NSString stringWithFormat:@"Palatino"];    

            }
                break;
            case 2:
            {
               fontName = [NSString stringWithFormat:@"Times New Roman"];  

            }
                break;
            case 3:
            {
                fontName = [NSString stringWithFormat:@"Verdana"];  


            }
                break;
        }

        NSInteger oldRow = (lastIndexPath1 != nil) ? [lastIndexPath1 row] : -1; 

        if(newRow != oldRow) 
        { 
            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath1]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            lastIndexPath1 = indexPath; 
        }   


        NSInteger lastRow = lastIndexPath1.row;

        NSString *lastIndexPathString = [NSString stringWithFormat:@"%i", lastRow];

        [prefs setObject:lastIndexPathString forKey:@"lastIndexPath1"];

        [prefs setObject:fontName forKey:@"fontName"];

        [prefs synchronize];

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }

    if (indexPath.section == 1) {

        NSString *fontSize;

        switch (indexPath.row) {
            case 0:
            {
               // cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:16];  

                fontSize = [NSString stringWithFormat:@"16.0"];   

               // UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
               // newCell = [tableView  cellForRowAtIndexPath:indexPath];
               // newCell.accessoryType = UITableViewCellAccessoryCheckmark;

            }
                break;
            case 1:
            {
            fontSize = [NSString stringWithFormat:@"20.0"];   


            }
                break;
            case 2:
            {
            fontSize = [NSString stringWithFormat:@"24.0"];   


            }
                break;
            case 3:
            {
            fontSize = [NSString stringWithFormat:@"28.0"];   


            }
                break;
            case 4:
            {
            fontSize = [NSString stringWithFormat:@"32.0"];   


            }
                break;
            case 5:
            {
            fontSize = [NSString stringWithFormat:@"36.0"];   


            }
                break;
            case 6:
            {
            fontSize = [NSString stringWithFormat:@"40.0"];   


            }
                break;
            case 7:
            {
            fontSize = [NSString stringWithFormat:@"44.0"];   


            }
                break;
            case 8:
            {
            fontSize = [NSString stringWithFormat:@"48.0"];   


            }
                break;
            case 9:
            {
            fontSize = [NSString stringWithFormat:@"52.0"];   


            }
                break;
            case 10:
            {
            fontSize = [NSString stringWithFormat:@"56.0"];   


            }
                break;
            case 11:
            {
            fontSize = [NSString stringWithFormat:@"60.0"];   

            }
                break;
        }

        NSInteger oldRow = (lastIndexPath2 != nil) ? [lastIndexPath2 row] : -1; 

        if(newRow != oldRow) 
        { 
            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath2]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            lastIndexPath2 = indexPath; 
        }   


        NSInteger lastRow2 = lastIndexPath2.row;

        NSString *lastIndexPathString2 = [NSString stringWithFormat:@"%i", lastRow2];

        [prefs setObject:lastIndexPathString2 forKey:@"lastIndexPath2"];

        //NSString *lastIndexPathString = [NSString stringWithFormat:@"@%", lastIndexPath2];

        //[prefs setObject:lastIndexPathString forKey:@"lastIndexPath2"];


        [prefs setObject:fontSize forKey:@"fontSize"];

        [prefs synchronize];

        [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    }



}
Kingpin is offline   Reply With Quote
Old 09-01-2011, 02:53 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 11
Kingpin is on a distinguished road
Default Anyone?

BUMB!
Kingpin is offline   Reply With Quote
Old 09-01-2011, 12:06 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 11
Kingpin is on a distinguished road
Default

BUMP!

Have tried all kinds of solutions... Working flawlessly on the iPad but totally useless on my iPhone. Is anyone able to duplicate this?
Kingpin is offline   Reply With Quote
Old 09-01-2011, 12:36 PM   #5 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

I haven't gone through all of your code, but you have loads of pointless stuff there. For example, this whole block:

Code:
switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:16];   
            }
                break;
            case 1:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:20];    
            }
                break;
            case 2:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:24];  
            }
                break;
            case 3:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:28];   
            }
                break;
            case 4:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:32];   
            }
                break;
            case 5:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:36];   
            }
                break;
            case 6:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:40];   
            }
                break;
            case 7:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:44];   
            }
                break;
            case 8:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:48];   
            }
                break;
            case 9:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:52];   
            }
                break;
            case 10:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:56];   
            }
                break;
            case 11:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:60];   
            }
                break;
        }
Can be just this one line:

Code:
cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:(16 + indexPath.row * 4)];
Can you post a few screenshots of the problem on iPhone and how it looks when it's OK on iPad?
baja_yu is offline   Reply With Quote
Old 09-01-2011, 12:36 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

At quick glance, you don't seem to be setting the checkmark in cellForRow. You should be.

So I'm going to guess that the difference here is size. The iPad is able to show more cells at once, maybe even all of them, so you never recycle the cells there. The iPhone shows fewer cells, and you do recycle them there. But since you don't define the checkmark in cellForRow, you get whatever the cell looked like when it scrolled off the screen.

In essence, you are getting lucky on the iPad, and the iPhone exposed the flaw with your logic.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-01-2011, 05:05 PM   #7 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 11
Kingpin is on a distinguished road
Default GREAT!

Thanks for the tip baja_yu, I will optimize the code before release...

BrianSlick you are spot on, and I have now fixed my problem...

I added a
Code:
 if([self.lastIndexPath1 isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else 
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
to the cellForRowAtIndexPath

and added
Code:
 if(self.lastIndexPath1)
        {
            UITableViewCell* uncheckCell = [tableView
                                            cellForRowAtIndexPath:self.lastIndexPath1];
            uncheckCell.accessoryType = UITableViewCellAccessoryNone;
        }
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.lastIndexPath1 = indexPath;
}
to didSelectRowAtIndexPath...

I removed the old
Code:
 /*
        
        NSInteger oldRow = (lastIndexPath1 != nil) ? [lastIndexPath1 row] : -1; 
        
        if(newRow != oldRow) 
        { 
            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath1]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            lastIndexPath1 = indexPath; 
        }   
        
               */
from didSelectRow, and now everything works perfectly...

THANK YOU...
Kingpin is offline   Reply With Quote
Reply

Bookmarks

Tags
ipad, iphone, iphone dev, table view, tableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 397
6 members and 391 guests
JackReidy, jeroenkeij, Sami Gh, yomo710, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:49 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0