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 02-02-2012, 06:59 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 3
dolan is on a distinguished road
Default Multiplying Switch in Tableview Cell

Please help me learn what is happening and wrong.

The situation: Building a 4 section, varying rows tableview, using a subclass of Tableview Cell in storyboard with dynamic prototypes. Everything is running fine until I added a UISwitch (just in 2 rows). They appear 'initially' at the correct indexPath, but after scrolling, they begin appearing where they aren't supposed to be.

I've tried many different ideas, all of which change nothing... reuse identifiers & [cell.comboLabel addSubview:nil] in the 'non switch' rows.

Here's where I am.
(The 2 connected cell textFields are: nameLabel & comboLabel)

Code:
NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];

NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0];
NSString *value = [[a objectAtIndex:0] valueForKey:key];

static NSString *CellIdentifier = @"MasterCell";

UsersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil)
{
    // Do anything that should be the same on EACH cell here.  Fonts, colors, etc.
    // ** This is not being logged, ever  **
    NSLog(@" (%@,%@) Cell = nil", [indexPath section], [indexPath row]  );
}

// Do anything that COULD be different on each cell here.  Text, images, etc.
// Set the Item
cell.nameLabel.text = [NSString stringWithFormat:@"%@", key];

... other cell configuration

// Put On/Off switch in Password 'On' fields
if ([key isEqualToString:@"On"])
{
    UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(152, 4, 0, 0)];
    if (!value)
        [aSwitch setOn:YES animated:NO];
    else
        [aSwitch setOn:NO animated:NO];
    
    [aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    cell.comboLabel.text = nil;
    cell.comboLabel.clipsToBounds = NO;
    [cell.comboLabel addSubview:aSwitch];
}
else
{
    cell.comboLabel.text = [NSString stringWithFormat: @"%@",value];
}
Any suggestions?
dolan is offline   Reply With Quote
Old 02-02-2012, 07:11 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by dolan View Post
Please help me learn what is happening and wrong.

The situation: Building a 4 section, varying rows tableview, using a subclass of Tableview Cell in storyboard with dynamic prototypes. Everything is running fine until I added a UISwitch (just in 2 rows). They appear 'initially' at the correct indexPath, but after scrolling, they begin appearing where they aren't supposed to be.

I've tried many different ideas, all of which change nothing... reuse identifiers & [cell.comboLabel addSubview:nil] in the 'non switch' rows.

Here's where I am.
(The 2 connected cell textFields are: nameLabel & comboLabel)

Code:
NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];

NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0];
NSString *value = [[a objectAtIndex:0] valueForKey:key];

static NSString *CellIdentifier = @"MasterCell";

UsersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil)
{
    // Do anything that should be the same on EACH cell here.  Fonts, colors, etc.
    // ** This is not being logged, ever  **
    NSLog(@" (%@,%@) Cell = nil", [indexPath section], [indexPath row]  );
}

// Do anything that COULD be different on each cell here.  Text, images, etc.
// Set the Item
cell.nameLabel.text = [NSString stringWithFormat:@"%@", key];

... other cell configuration

// Put On/Off switch in Password 'On' fields
if ([key isEqualToString:@"On"])
{
    UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(152, 4, 0, 0)];
    if (!value)
        [aSwitch setOn:YES animated:NO];
    else
        [aSwitch setOn:NO animated:NO];
    
    [aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    cell.comboLabel.text = nil;
    cell.comboLabel.clipsToBounds = NO;
    [cell.comboLabel addSubview:aSwitch];
}
else
{
    cell.comboLabel.text = [NSString stringWithFormat: @"%@",value];
}
Any suggestions?
You should use a unique cell reuse identifier for cells that have a different structure. If you have a subset of your cells that have a segmented control in them, let's call those cells "segmentedCell". Ask for a "segmentedCell" when you try to dequeue a reused cell. Then, if no "segmentedCell" is available, create a cell using that identifier.

The idea is that you create cells of different types, and recycle them in that type. (You sort bottles, cans, and newspapers separately. If you need some newspaper to wrap fish, you ask for recycled newspaper. If you just ask for some recycled stuff, you might get a bottle instead, and you can't wrap up fish with a bottle.)

Once you have each type of cell being created and recycled in separate "recycling bins", you always get the correct type of cell, or are told there aren't any of that type.

Once you have a cell with the correct structure (layout of fields) you need to completely configure it, setting all values, including forcing fields to an empty/starting state if you don't use them.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-02-2012, 07:34 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 3
dolan is on a distinguished road
Default

Thanks for replying Duncan.

Quote:
Originally Posted by Duncan C View Post
The idea is that you create cells of different types, and recycle them in that type. (You sort bottles, cans, and newspapers separately. If you need some newspaper to wrap fish, you ask for recycled newspaper. If you just ask for some recycled stuff, you might get a bottle instead, and you can't wrap up fish with a bottle.)
Are you suggesting that my code for the cell needs to be (almost) duplicated?

In form:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];

    NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0];
    NSString *value = [[a objectAtIndex:0] valueForKey:key];

    static NSString *CellIdentifier;
    UsersTableViewCell *cell;
    // Put On/Off switch in Password 'On' fields
    if ([key isEqualToString:@"On"]) {
        CellIdentifier = @"SwitchCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Build the rest of the 'UISwitch' cell
        .... cell. {property} = ...
    }
    else {
        CellIdentifier = @"MasterCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 
       // Build the rest of the 'Usual' cell
       .... cell.  {property} = ...
    }
    return cell;
}
dolan is offline   Reply With Quote
Old 02-02-2012, 09:32 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by dolan View Post
Thanks for replying Duncan.


Are you suggesting that my code for the cell needs to be (almost) duplicated?

In form:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *a = [[userDetailData objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];

    NSString *key = [[[a objectAtIndex:0] allKeys] objectAtIndex:0];
    NSString *value = [[a objectAtIndex:0] valueForKey:key];

    static NSString *CellIdentifier;
    UsersTableViewCell *cell;
    // Put On/Off switch in Password 'On' fields
    if ([key isEqualToString:@"On"]) {
        CellIdentifier = @"SwitchCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Build the rest of the 'UISwitch' cell
        .... cell. {property} = ...
    }
    else {
        CellIdentifier = @"MasterCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 
       // Build the rest of the 'Usual' cell
       .... cell.  {property} = ...
    }
    return cell;
}

Sort of, yes.

You are writing a factory that creates table view cells cells. It first looks for a recycled cell of the correct type. If it finds one, it sets all it's values and returns it. If it doesn't find it, it needs to build it.

It may be that you can make your code modular. IF all your cells have common elements, and the only difference for your cells that have a segmented control is the segmented control, then you could structure your code so the shared setup is only done once (in a separate method, maybe?)
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
storyboard, subclassing, uitableview cell

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: 408
17 members and 391 guests
Atatator, chiataytuday, dre, FrankWeller, imac74, ipodphone, jeroenkeij, kukat, LunarMoon, MAMN84, n00b, QuantumDoja, reficul, Retouchable, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

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