使用 UITbaleView 选择一个带有一个复选标记的特定行 [英] Select one speicific row with one checkmark using UITbaleView

查看:75
本文介绍了使用 UITbaleView 选择一个带有一个复选标记的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    - (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;

推荐答案

试试这个:

单行选择:

创建一个新变量来跟踪控制器中的索引:

create a new variable to track the index In Controller:

 int selectedIndex;

在 UITableView cellForRowAtIndexPath 方法中:

in UITableView cellForRowAtIndexPath method:

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

在 UITableView didSelectRowAtIndex 方法中:

and in UITableView didSelectRowAtIndex method:

 selectedIndex = indexPath.row;
 [tableView reloadData];

根据您的需要采用 2 种方式:

.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;
        }
    }

快乐编码!!

这篇关于使用 UITbaleView 选择一个带有一个复选标记的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆