I am building a table with different sections but when I scroll down the checkmark disappears.
My table has around 10 sections divided into arrays (one array for each section).
Also my table does not reuse cells (it is set to "nil").
//
// ViewController.m
// 1234567
//
// Created by BTB Productionz on 8/17/14.
// Copyright (c) 2014 unknown. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
int info;
NSArray *section1;
NSArray *section2;
NSArray *section3;
NSArray *section4;
NSArray *section5;
NSArray *section6;
NSArray *section7;
NSArray *section8;
NSArray *section9;
NSArray *section10;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
section1 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9",@"Item 10",@"Item 11",@"Item 12", nil];
section2 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9", nil];
section3 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9",@"Item 10",@"Item 11",@"Item 12",@"Item 13",@"Item 14",@"Item 15",@"Item 16",@"Item 17",@"Item 18", nil];
section4 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9",@"Item 10", nil];
section5 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9",@"Item 10",@"Item 11",@"Item 12",@"Item 13",@"Item 14", nil];
section6 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9",@"Item 10",@"Item 11",@"Item 12",@"Item 13", nil];
section7 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3", nil];
section8 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8", nil];
section9 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7", nil];
section10 = [NSArray arrayWithObjects:@"Item 1", @"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9",@"Item 10", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section ==0) {
return [section1 count];}
else if (section ==1){return [section2 count];}
else if (section ==2){return [section3 count];}
else if (section ==3){return [section4 count];}
else if (section ==4){return [section5 count];}
else if (section ==5){return [section6 count];}
else if (section ==6){return [section7 count];}
else if (section ==7){return [section8 count];}
else if (section ==8){return [section9 count];}
else if (section ==9){return [section10 count];}
else{return 0;}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 10;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section ==0) {
return @"Section 1";
}
else if (section ==1){return @"Section 2";}
else if (section ==2){return @"Section 3";}
else if (section ==3){return @"Section 4";}
else if (section ==4){return @"Section 5";}
else if (section ==5){return @"Section 6";}
else if (section ==6){return @"Section 7";}
else if (section ==7){return @"Section 8";}
else if (section ==8){return @"Section 9";}
else if (section ==9){return @"Section 10";}
else {
return@" ";
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
if (indexPath.section ==0) {
cell.textLabel.text = [section1 objectAtIndex:indexPath.row];
}
if (indexPath.section ==1) {
cell.textLabel.text = [section2 objectAtIndex:indexPath.row];
}
if (indexPath.section ==2) {
cell.textLabel.text = [section3 objectAtIndex:indexPath.row];
}
if (indexPath.section ==3) {
cell.textLabel.text = [section4 objectAtIndex:indexPath.row];
}
if (indexPath.section ==4) {
cell.textLabel.text = [section5 objectAtIndex:indexPath.row];
}
if (indexPath.section ==5) {
cell.textLabel.text = [section6 objectAtIndex:indexPath.row];
}
if (indexPath.section ==6) {
cell.textLabel.text = [section7 objectAtIndex:indexPath.row];
}
if (indexPath.section ==7) {
cell.textLabel.text = [section8 objectAtIndex:indexPath.row];
}
if (indexPath.section ==8) {
cell.textLabel.text = [section9 objectAtIndex:indexPath.row];
}
if (indexPath.section ==9) {
cell.textLabel.text = [section10 objectAtIndex:indexPath.row];
}
/*
cell.accessoryType = UITableViewCellAccessoryNone;
*/
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
[theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
}
}
/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryView == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark){cell.accessoryType = UITableViewCellAccessoryNone;}
}
*/
@end
See this comment in your code // Reflect selection in data model
You need to add logic in you data model to determine if the cell should display a checkmark or not.
And yes you are reusing cells unlike you think, see this line:
[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
EDIT: You should declare a NSMutable array to hold all your section arrays:
NSMutableArray *data;
viewDidLoad
You will need to add a property to your data model in order to track if the cells should be selected or not. for example :
section1 = [NSMutableArray arrayWithObjects:@{@"title":@"Item 1",@"isSelected": @"N"},
@{@"title":@"Item 2",@"isSelected":@"N"},
@{@"title":@"Item 3",@"isSelected": @"N"},
@{@"title":@"Item 4",@"isSelected": @"N"}, nil];
After initializing your section arrays, append them to the data source
data = [NSMutableArray arrayWithObjects:section1, section2, section3, section4, section5, section6, nil];
numberOfRowsInSection
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [data[section] count];
}
numberOfSectionsInTableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [data count];
}
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
NSString * title = data[indexPath.section][indexPath.row][@"title"];
BOOL isSelected = [data[indexPath.section][indexPath.row][@"isSelected"] isEqualToString:@"Y"];
cell.textLabel.text = title;
if (!isSelected) {
cell.accessoryType = UITableViewCellAccessoryNone;
}else{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
[theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
[data[newIndexPath.section] replaceObjectAtIndex:newIndexPath.row withObject:@{@"title":cell.textLabel.text,@"isSelected": @"Y"}];
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
[data[newIndexPath.section] replaceObjectAtIndex:newIndexPath.row withObject:@{@"title":cell.textLabel.text,@"isSelected": @"N"}];
}
}