Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uitableview 滚动时,tableview中的复选标记将消失_Uitableview_Scroll_Accessory_Checkmark - Fatal编程技术网

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

Uitableview 滚动时,tableview中的复选标记将消失,uitableview,scroll,accessory,checkmark,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" @interf

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

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

在代码中查看此注释//在数据模型中反映选择

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

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

[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];
行数部分

NumberOfSectionsTableView

cellForRowAtIndexPath

didSelectRowAtIndexPath


哦,这是我试过但没用的评论。你发现问题了吗?meda我不知道为什么,但它没有显示数据。抱歉,我问得太多了,我是新来的。@SebastiánLópezPérez如果您再次需要我的帮助,欢迎您在下面留言。好卢卡一定会的。非常感谢。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [data[section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [data count];
}
- (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;


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