Reputation: 3212
I'm developing an application using Xcode 4.6.1 for >iOS 5.0 version.
The application is simply displaying questionnaires which were fetched from server side, saved in database and allow its users to fill it.
To show questions, i've a UIScrollView
and i'm using it in pagingEnabled mode. I've three kinds of question choice type which are checkbox
,radio button
and textfield
. For UI Controls, i've created CheckBoxButton
and RadioButton
class which are simply inherited from UIButton
. I've set selection property by changing background image. So each page contains a question and checkboxes | radio buttons
according to choice count. If choice count is 4, it's displaying 4 checkboxes.
If question and choices doesn't fit in the page, i'm creating a UITableView
and i'm adding it to UIScrollView
as a subview. Instead of using UITableView
, i tried to use UIScrollView
but it makes things worse. So,it's not really an option. To create UITableViewCell
, i'm using this code.
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone ;
}
QuestionChoice *currentChoice = [[[[_questionArray objectAtIndex:currentQuestionIndex] choices] allObjects ] objectAtIndex:indexPath.row -1 ];
CheckBoxButton *checkBox = [[CheckBoxButton alloc] initWithXValue:20 yValue:0 tag:0];
[cell addSubview:checkBox];
UIFont *aeroSmall = [UIFont fontWithName:@"Aero" size:22.0f];
NSString *choiceText = currentChoice.choiceText;
CRTLabel *choiceLabel = [[CRTLabel alloc] initWithFrame:CGRectMake(80, 0, 700, 44) andText:choiceText font:aeroSmall tag:0];
[cell addSubview:choiceLabel];
I know that if a cell isn't present on the screen, it doesn't exist. My problem is with that. Let's say that i've checked fourth checkbox and scroll up, so that, fourth cell doesn't anymore display. When i scroll down, the fourth cell is going to be recreating by using above code. My problem is it's getting created with old status. It means that when i checked before scroll up, it's recreated in checked.
How is it possible ? What am i missing ? I didn't use any cache mechanism.
Upvotes: 0
Views: 139
Reputation: 154523
For efficiency's sake, iOS reuses table cells. When your cell scrolls off the screen, it isn't deallocated. Instead, it is thrown into the reuse queue. When the cell reappears on the screen, the system grabs a cell from the reuse queue, if available, and hands it to you. All of the subviews that you previously added to that cell are still there, so you need to clean up that cell and initialize it properly, otherwise you will see whatever was in that cell previously.
A UITableView is used to display a table. It is not used to store any information. In your case, you will want to know the state of a checkbox if someone checks it. To get that state, you will want to add target
and action
to your checkbox so that you will get called if someone touches the checkbox and changes its state.
In your action method, you will need to store the value of the checkbox in a data structure that is a part of your view controller (not the table view). You would also need to look at this data structure when recreating a cell that was off screen but has now reappeared.
Upvotes: 1