【问题标题】:Select one speicific row with one checkmark using UITbaleView使用 UITableView 选择一个带有一个复选标记的特定行
【发布时间】:2013-12-01 11:56:02
【问题描述】:

请任何人告诉我如何使用复选标记一次选择行,而另一行没有复选​​标记。我尝试过,但在我的情况下,选择了多行并带有复选标记。基本上,当我选择另一行然后取消选择前一行并使用复选标记选择这一行时,我想用复选标记保存一行。这是我的代码

    - (NSString *)getKeyForIndex:(int)index
    {
    return [NSString stringWithFormat:@"KEY%d",index];
    }
    - (BOOL) getCheckedForIndex:(int)index
    {
     if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]]boolValue]==YES)
     {
     return YES;
     }
     else
    {
    return NO;

       } }


- (void) checkedCellAtIndex:(int)index
    {    BOOL boolChecked = [self getCheckedForIndex:index];
        [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
      return List.count;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {
      static NSString *subviewCells = @"Cells";

       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells];
      if (cell == nil)
     {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells];
      }
        cell.textLabel.text = [List objectAtIndex:indexPath.row];
          return cell;
    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
       UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

         [self checkedCellAtIndex:indexPath.row];

      if([self getCheckedForIndex:indexPath.row]==YES)
       {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }
      else
     {
        cell.accessoryType = UITableViewCellAccessoryNone;
     }
      selectLanguage = [List objectAtIndex:indexPath.row];
    }
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
       [self checkedCellAtIndex:indexPath.row];

        if([self getCheckedForIndex:indexPath.row]==NO)
      {

        cell.accessoryType = UITableViewCellAccessoryNone;
   }
    else

      {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }

     selectLanguage = [List objectAtIndex:indexPath.row];
    }

    @end;

【问题讨论】:

  • 为什么,是的。这正是你将如何做到的。现在呢?

标签: ios iphone objective-c xcode4 xcode5


【解决方案1】:

试试这个:

单行选择:

在Controller中创建一个新变量来跟踪索引:

 int selectedIndex;

在 UITableView cellForRowAtIndexPath 方法中:

if(indexPath.row == selectedIndex)
{
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
   cell.accessoryType = UITableViewCellAccessoryNone;
}

在 UITableView didSelectRowAtIndex 方法中:

 selectedIndex = indexPath.row;
 [tableView reloadData];

随心所欲的两种方式:

.h 文件:

NSIndexPath* checkedIndexPath;

@property (nonatomic, retain) NSIndexPath* checkedIndexPath;

.m 文件:

 @synthesize checkedIndexPath;

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

         //do you stuff here
        if([self.checkedIndexPath isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

        return cell;
    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        //do work for checkmark
        if(self.checkedIndexPath)
        {
            UITableViewCell* uncheckCell = [tableView
                                    cellForRowAtIndexPath:self.checkedIndexPath];
            uncheckCell.accessoryType = UITableViewCellAccessoryNone;
        }
        if([self.checkedIndexPath isEqual:indexPath])
        {
            self.checkedIndexPath = nil;
        }
        else
        {
            UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            self.checkedIndexPath = indexPath;
        }
    }

编码愉快!!

【讨论】:

  • ok 兄弟 那么如果我第一次选择行然后在第二次我重新加载表时我的行已经被检查了,那么保存检查标记呢? :)
  • 你必须删除 didselectrow 方法中的所有复选标记并重新设置。
  • 此代码不能确保始终选择一个。对于至少一个选择,在 didSelectRowAtIndexPath 中使用以下代码:-- [tableView cellForRowAtIndexPath:self.selectedIndexPath].accessoryType = UITableViewCellAccessoryNone; [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; self.selectedIndexPath = indexPath;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多