1

複数のセクションを含むテーブル ビューがあります。1行だけ選択できるようにしたい...私のテーブルビューの選択プロパティは次のように設定されています:SingleSelectioin

今、選択した行にチェックマークを設定するためにこれを行っています

if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)

{
       [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

else
{
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

今私が欲しいのは、他のセルを選択すると、以前に選択したセルを選択解除に戻す必要があるということです。

私はこれで試しました

lastIndexpath=indexPath;

その後

[tableView deselectRowAtIndexPath:lastIndexpath animated:YES]; 

しかし、それは私にlasIndexPathへの悪いアクセスを与えます

EXC_BAD_ACCESS (code=1,address= 0xfe000008)

親切にこれを手伝ってください

新しい提案も歓迎します

4

4 に答える 4

3

選択したセルをマークしたい場合は、didSelectRowAtIndexPath で次のようなコードを記述できます。

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

      cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:previousSelectedIndexPath];
        [cell setAccessoryType: UITableViewCellAccessoryNone];

     if(previousSelectedIndexPath!=nil)
    {
       [previousSelectedIndexPath release];
       previousSelectedIndexPath = nil;
    }
     previousSelectedIndexPath = [indexPath retain];
}

ここで、previousSelectedIndexPath は以前に選択されたインデックスです。

于 2012-06-13T11:49:10.677 に答える
2

コントローラーの .h でNSIndexPath *lastIndexpathを宣言します。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
  if( lastIndexpath){
  if([tableView cellForRowAtIndexPath:lastIndexpath].accessoryType == UITableViewCellAccessoryCheckmark)

 {

  [tableView cellForRowAtIndexPath:lastIndexpath].accessoryType = UITableViewCellAccessoryNone;
 }
 else
 {
   [tableView cellForRowAtIndexPath:lastIndexpath].accessoryType = UITableViewCellAccessoryCheckmark;

 }}
  lastIndexpath=[indexPath retain]
 if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
 {
   [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
 }
 else
 {
  [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
 }
}
于 2012-06-13T11:57:57.420 に答える
1
//Take a variable of type indexpath in .h   

NSIndexPath    *myIndexPath

//Now check it in your cellForRowAtIndexPath: delegate method ..

if(indexPath == myIndexPath)
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
else 
     cell.accessoryType = UITableViewCellAccessoryNone;

//and in your didSelect: Delegate method ...

[tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark
myIndexPath = indexPath;
[tableView reloadData];

これがあなたを助けることを願っています。

于 2012-06-13T12:27:32.590 に答える
1

デリゲート メソッド willSelectRowAtIndexPath で、現在選択されているインデックス パスを取得し、accerroryType を none に設定します。

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];

    [tableView cellForRowAtIndexPath:currentSelectedIndexPath].accessoryType = UITableViewCellAccessoryNone;

    if ([indexPath isEqualTo: currentSelectedIndexPath])
    {
        return nil;
    }
    return indexPath;
}
于 2012-06-13T12:41:01.843 に答える