iphone - 如何维护 TableView 中行的选择状态?

标签 iphone ios objective-c uitableview

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

最佳答案

在我的应用程序中,我使用与 accessoryType 相同的复选标记代码,检查这个。

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

它会帮助你。

关于iphone - 如何维护 TableView 中行的选择状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374392/

相关文章:

ios - 添加到 NSCompoundPredicate

iphone - 创建一个将用户重定向到移动/HTML5 网站的 iPhone 应用程序

ios - 关闭游戏中心 View

iphone - 递归方法中的 NSMutableArray 内存泄漏

ios - 我可以在不受我控制的 VC 拥有的 View 中安装自动布局约束吗?

iphone - phonegap 2.6 iOS 程序中出现意外的 '@'

iphone - 另一个 ScrollView 内的 ScrollView 内的 TableView

ios - 如何从 Swift 中的字符串生成条形码?

ios - CMDeviceMotion 的磁场属性不正确?

ios - 在 ios 中创建水平虚线