【问题标题】:How to maintain state of selection of row in table view?如何保持表格视图中行的选择状态?
【发布时间】:2013-10-15 06:13:08
【问题描述】:

我是 xcode 的初学者。我正在创建一个带有多行滚动的问卷类型列表。但是当我选择一行并滚动它时,它不会保留它的状态,当我回来时,它也会失去它的选择。所以任何人请让我知道如何实现这一点,我已经尝试过这样的事情,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(val==1)
    {
        checkedArr=[[NSMutableArray alloc] init];
        for (int i = 0; i<17; i++)
        {
            [checkedArr addObject:@"1"];
        }
        NSLog(@"Checked arr size %i",[checkedArr count]);

        return 17;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
    }
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0];

    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:99/255.0f green:209/255.0f blue:248/255.0f alpha:1.0];
    cell.selectedBackgroundView = selectionColor;

    if([[checkedArr objectAtIndex:indexPath.row] isEqual:@"0"])
    {
        cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];
        NSLog(@"checkedArr 0000000");
    }
    else if ([[checkedArr objectAtIndex:indexPath.row] isEqual:@"1"])
    {
        cell.accessoryView=nil;
        NSLog(@"checkedArr 111111");
    }


    cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
    cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
    NSLog(@"Checked arr size %i",[checkedArr count]);

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell=[questionTable cellForRowAtIndexPath:indexPath];
  [checkedArr replaceObjectAtIndex:indexPath.row withObject:@"0"];
    if([[checkedArr objectAtIndex:indexPath.row] isEqual:@"0"])
    {
        cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];

        NSLog(@"checkedArr 0000000");

    }
    else if ([[checkedArr objectAtIndex:indexPath.row] isEqual:@"1"])
    {[questionTable deselectRowAtIndexPath:indexPath animated:YES];
        cell.accessoryView=nil;
        NSLog(@"checkedArr 111111");
    }

    NSLog(@"Val is %i",val);
    NSLog(@"selected is %@",[listArray objectAtIndex:indexPath.row]);
//    NSLog(@"Checked arr descripton %@",[checkedArr description]);
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryView = UITableViewCellAccessoryNone;
}

【问题讨论】:

  • 您只想要选中表格视图中选定行的复选标记吗?
  • 检查这个链接..stackoverflow.com/questions/17419214/…我认为这个逻辑对你很有帮助..
  • 是的。我使用了小图像而不是复选标记。因此,如果选择了一行,图像将出现在右侧的该行中。
  • 检查我的单选编辑答案。

标签: iphone ios objective-c uitableview


【解决方案1】:

在我的应用程序中,我使用与附件类型相同的复选标记代码,请检查。

把它放在.h文件中的可变数组arSelectedRows中;

 - (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];
  }
  [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

   //Do anything you want for cell here
  if([arSelectedRows containsObject:indexPath]) {
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
 }
  else {
        cell.accessoryType = UITableViewCellAccessoryNone;
  }
 return cell;
}

 #pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   if(cell.accessoryType == UITableViewCellAccessoryNone) {
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
       [arSelectedRows addObject:indexPath];
   }
   else {
      cell.accessoryType = UITableViewCellAccessoryNone;
     [arSelectedRows removeObject:indexPath];
 }
 NSLog(@"arSelectedRows are  :%@",arSelectedRows);

 [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

编辑

  //if you want only single check mark 

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

     [arSelectedRows removeAllObjects];

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     if(cell.accessoryType == UITableViewCellAccessoryNone) {
          cell.accessoryType = UITableViewCellAccessoryCheckmark;
         [arSelectedRows addObject:indexPath];
     }
      else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [arSelectedRows removeObject:indexPath];

      }

      NSLog(@"arSelectedRows are:%@",arSelectedRows);


     [self.tableview reloadData];//Reload your tableview here
 }

它会帮助你。

【讨论】:

  • 非常感谢。如果我必须单选而不是多选怎么办?
  • 多项选择像魅力,但单选不起作用
  • 在单选中,没有选择行。它不显示图像。
【解决方案2】:

首先,您的cellForRowAtIndexPath 方法每次向上/向下滚动tableView 时都会创建新单元格,这对内存管理非常不利。

只需在您的 cellForRowAtIndexPath 方法中删除 [questionTable deselectRowAtIndexPath:indexPath animated:YES]

【讨论】:

  • 那么有什么不同的解决方案吗?
【解决方案3】:

我认为你需要在下面使用这个 api:-

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition

【讨论】:

    【解决方案4】:

    如果您使用动态单元格,并使用某种类变量(如 NSArray)更新您的行,您可能已将变量设置为弱而不是强。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多