0

我是 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;
}
4

4 回答 4

2

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

将其放入 .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
 }

它会帮助你。

于 2013-10-15T06:32:38.167 回答
0

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

于 2014-08-07T11:49:33.533 回答
0

I think you need to use this api below:-

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
于 2013-10-15T06:49:46.430 回答
0

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

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

于 2013-10-15T06:23:02.090 回答