滚动时,tableview中的选中标记消失

时间:2014-08-19 02:05:47

标签: uitableview scroll accessory checkmark

我正在构建一个包含不同部分的表格,但当我向下滚动时,复选标记会消失。

我的表有大约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

1 个答案:

答案 0 :(得分:0)

在代码// Reflect selection in data model

中查看此评论

您需要在数据模型中添加逻辑,以确定单元格是否应显示复选标记。

是的,你不像你想象的那样重复使用细胞,请看这一行:

[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

编辑: 您应声明一个NSMutable数组来保存所有节数组:

NSMutableArray *data;

<强> viewDidLoad中

您需要向数据模型添加属性,以便跟踪是否应选择单元格。例如:

section1 = [NSMutableArray arrayWithObjects:@{@"title":@"Item 1",@"isSelected": @"N"},
                                            @{@"title":@"Item 2",@"isSelected":@"N"},
                                            @{@"title":@"Item 3",@"isSelected": @"N"},
                                            @{@"title":@"Item 4",@"isSelected": @"N"}, nil];

初始化节数组后,将它们附加到数据源

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"}];
    }
}