1

I have a tableview with custom cells in my App and each cell contains two checkbox buttons.

The problem is, when a scrolling event is triggered (up or down) it's reloading the tableview. Therefore, the checkbox buttons become to initial state.

Please give me a solution.

Thanks you

2
  • Do you use dequeueReusableCellWithIdentifier method for your cells? Commented Sep 1, 2011 at 12:41
  • yes, I'm using dequeueReusableCellWithIdentifier. is it the case? Commented Sep 2, 2011 at 3:54

5 Answers 5

1

You're going to have to maintain a list yourself to determine which cells should be checked or not. Remember that in tableView:cellForRowAtIndexPath:, a proper implementation will recycle cells so that you never have more than 10-15 cells instantiated. This can cause some funky results if you don't handle for it properly. When I've done a poor implementation, I've seen certain cell properties "carry over" from one cell to the next.

Anyway, here's what I'd recommend (based on what I think you're asking):
1. Create a class to back each UITableViewCell
2. Create a property in that class to determine which of the two checkboxes (or neither or both) should be checked.
3. In your ViewController/TableViewController, maintain an NSMutableArray/NSArray where 1 item in the array = 1 cell in the UITableView.
4. In your tableView:cellForRowAtIndexPath: method, get a reference to the appropriate item in your array.
5. Then, check that instance's properties and set the checkbox values appropriately.


Sample Code:

TableView.h

@interface TableView : UITableViewController 

@property (strong, nonatomic) NSMutableArray *itemArray;

@end

TableView.m

@implementation TableView

@synthesize itemArray;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Assume you get a valid, custom UITableViewCell at this point (named "cell")

    // Configure the cell...
    NSObject *classItem = [[self itemArray] objectAtIndex:[indexPath row]];
    [[cell checkBox1] setChecked:[classItem checkbox1Checked]];
    [[cell checkBox2] setChecked:[classItem checkbox2Checked]];

    return cell;
}

@end
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot for your help, Im using a separate class for custom cells. my cellForRowAtIndexPath:(NSIndexPath *)indexPath method is like this. NSString *CustomCellIdentifier = @"CustomCellIdentifier"; CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]]) cell = (CustomCell *)oneObject; }
0

You should set the state of the button in the datasource and load this state when creating the cell. I wrote a small Xcode project to demonstrate this.

Comments

0

Well you should not use the TableView as the datasource.

Every time the cell comes into view the UITableViewDataSource is asked for the UITableViewCell at as indexpath.

- (void) tableView:(UITableView *)tableView setImage:(UIImage *)image forCellAtIndexPath:(NSIndexPath *)indexPath 

In the is method you should set the checkbox state as it is reflected in your dataSource. When the checkbox is changed save it in the data source with the selected state.

Example:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CheckedTableViewCell";

    CheckedTableViewCell *cell = (CheckedTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
        cell = (CheckedTableViewCell *)self.nibCell;
        self.nibCell = nil;
    }

    item *item = [self objectAtIndexPath:indexPath];

    cell.titleLabel.text = item.title;
    cell.switch.on = item.selected;

    return cell;
}

2 Comments

thanks for the help. but there is a problem when use this if (!cell) { [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil]; cell = (CheckedTableViewCell *)self.nibCell; self.nibCell = nil; } because, every time it scrolls the tabelview, its create new cell similar to checked cell.
Did you set the cellIdentifier in for the UITableViewCell in the nib.
0

You could save the state in NSUserDefaults right when you click it. Just add a target with @selector(changedOne: ) and add the void statement:

- (void)changedOne: (id)sender  {
    NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
    NSString *row = [NSString initWithFormat:@"toggleOneRow%i",indexPath.row];
    if (sender.on) {
        [df setBool:YES forKey:row];
    }
    else {
        [df setBool:NO forKey:row];
    }
}

1 Comment

I think this answer needs a bit more information to be useful. Could you add more details, such as "To what do I add the target?" or maybe "How do I use this information once it's set?". Basically, I think you're using advanced concepts which, based on the question, might be beyond the OP's Obj-C skills (without further explanation).
0

Are you using cellForRowAtIndexPath. If yes, then instead of

 static NSString CellIdentifier=@"CellIdentifier"

use

 NSString *CellIdentifier=[NSString stringWithFormat=@"CellIdentifier%d",indexPath.row];

Other approach you can take is assign tags to checkboxbuttons and take one dictionary in appDelegate file and set value for checkbox tag.initially you may set is either checked or unchecked.and set the values in cellforrowatindexpath method. set the values of checkboxes as per appdelegate dictionary.and update the state in appdelegate dictionary when user selects or deselects the button.

3 Comments

hi, I couldn't understand what is the effect of using this, please tell me. thanks a lot
You should not doe this, this will make the TableView react very slow, scrolling will lag.
if you use static CellIdentifier then this identifier will be used for next rows.But if you make %d then diffrent identifier will be created for each row.but as the rckoess says scrolling will be slower.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.