0

さまざまなセクションでテーブルを作成していますが、下にスクロールするとチェックマークが消えます。

私のテーブルには、配列に分割された約 10 のセクションがあります (セクションごとに 1 つの配列)。

また、私のテーブルはセルを再利用しません (「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
4

1 に答える 1

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"}];
    }
}
于 2014-08-19T04:05:48.420 に答える