我正在构建一个包含不同部分的表格,但是当我向下滚动时,复选标记消失了。
我的表有大约 10 个部分,分为数组(每个部分一个数组)。
我的表也不重用单元格(它设置为“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