1

何時間も費やして何の役にも立ちませんでした。検索(グーグル)がうまくいかなかったのか、それとも質問が少ないのか、専門家の回答を実装する際に間違いを犯したのかわかりません。

セクション内の 1 つの行にアクセサリ タイプのチェック マークを設定し、他の行には何も設定しないといういくつかの質問があることは知ってます

テーブル ビューに 2 つのセクションがあります。デフォルトでは、各セクションの 1 番目の行を選択します。つまり、アクセサリ ビューのチェック マークを付けます。ここから、ユーザーが行を選択すると、選択した行にチェック マークが表示されるようにします。各セクションで行の選択を追跡するために、2 つのインデックス パスを宣言しようとしました。実装コードは次のとおりです。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.row,indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (indexPath.section == 0)
    {
        if(self.firstSectionIndex == indexPath)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

    if (indexPath.section == 1)
    {
        if(self.secondSectionIndex == indexPath)
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];

        switch (indexPath.section)
        {
            case 0:
            {
                if (indexPath.row == 0)
                {
                    if(self.firstSectionIndex != indexPath)
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    }
                    else
                    {
                        cell.accessoryType = UITableViewCellAccessoryNone;
                    }
                    cell.textLabel.text = @"Yes";
                }
                if (indexPath.row == 1)
                {
                    cell.textLabel.text = @"No";
                }
            }
                break;
            case 1:
            {
                if (indexPath.row == 0)
                {
                    if(self.secondSectionIndex != indexPath)
                    {
                        cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    }
                    else
                    {
                        cell.accessoryType = UITableViewCellAccessoryNone;
                    }
                    cell.textLabel.text = @"Easy";
                }
                if (indexPath.row == 1)
                {
                    cell.textLabel.text = @"Medium";
                }
                if (indexPath.row == 2)
                {
                    cell.textLabel.text = @"Hard";
                }
            }
                break;

            default:
                break;
        }
    }

    tableView.backgroundColor = [UIColor clearColor];
    tableView.backgroundView = nil;

    return cell;
}

私の didSelectRowAtIndexPath では、次のことを行いました。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
      self.firstSectionIndex = indexPath;
    }
    if (indexPath.section == 1)
    {
       self.secondSectionIndex = indexPath;
    }
    [tableView reloadData];
}

長期参照用に保存するセル選択の状態を検索しましたが、それを求めて、便利なリンクhereを見つけました。

しかし、複数のセルを選択しており、セクションのすべての行にアクセサリ タイプのチェック マークが適用されています。何が問題なのかわかりません。どなたか教えてください!!

よろしくお願いします:)

4

4 に答える 4

3

次のコードの実装を使用してそれを実現できます:-

編集済み

.h ファイル

NSMutableArray *firstSelectedCellsArray;
NSMutableArray *secondSelectedCellsArray;
NSMutableArray *ThirdSelectedCellsArray;

.m ファイルで

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [firstSelectedCellsArray containsObject:rowNumber]  )
            {
                [firstSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [firstSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [firstSelectedCellsArray containsObject:rowNumber]  )
            {
                [firstSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [firstSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    if (indexPath.section == 1)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [secondSelectedCellsArray containsObject:rowNumber]  )
            {
                [secondSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [secondSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [secondSelectedCellsArray containsObject:rowNumber]  )
            {
                [secondSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [secondSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if (indexPath.section == 2)
    {
        NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            if ( [ThirdSelectedCellsArray containsObject:rowNumber]  )
            {
                [ThirdSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [ThirdSelectedCellsArray addObject:rowNumber];
            }
        }
        else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            if ( [ThirdSelectedCellsArray containsObject:rowNumber]  )
            {
                [ThirdSelectedCellsArray removeObject:rowNumber];
            }
            else
            {
                [ThirdSelectedCellsArray addObject:rowNumber];
            }
            selectedCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

}

cellForRowAtIndexPathに少しのコードを入れます:-

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    if(indexPath.section==0)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [firstSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if(indexPath.section==1)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [secondSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    if(indexPath.section==2)
    {

        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
        if ( [ThirdSelectedCellsArray containsObject:rowNsNum]  )
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

    return cell;
}

デモリンク

http://www.sendspace.com/file/z6oxg2

スクリーンショット:

ここに画像の説明を入力

于 2013-05-14T07:25:31.167 に答える
1

チェックマークを実装するため

  1. 選択したインデックスのインデックス配列を保持する
  2. cellFor Row メソッドで、選択したインデックス配列に現在のインデックスパスがあるかどうかを確認します。

真であれば

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

そうしないと

    cell.accessoryType = UITableViewCellAccessoryNone;
  1. 配列からインデックスを削除し、アクセサリ タイプを none に設定します。
  2. アクセサリがない場合は、チェックマークを追加し、配列にインデックスを追加します。

編集

セルを選択するたびに単一選択が必要な場合は、配列からすべてのインデックスを削除し、それのみを追加してから、テーブルビューをリロードします

于 2013-05-14T07:25:44.513 に答える