【问题标题】:UITableView selectable listUITableView 可选列表
【发布时间】:2014-09-15 13:17:13
【问题描述】:

我正在尝试实现一个可选择的列表视图。列表视图包含一些名称。您可以选择或取消选择多个名称。如果选择了名称,则单元格样式将更改为UITableViewCellAccessoryCheckmark

但是现在我有一个问题。在取消选择名称之前,您必须在单元格上按多次。这是因为didSelectRowAtIndexPath 总是首先被调用。

// cellForRowAtIndexPath function
    if ([assignedPlayers containsObject:player.persistentData])
    {
        [cell setSelected:true];
        [cell setAccessoryType: UITableViewCellAccessoryCheckmark];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];

    return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryNone];

    [self.eventController removePlayerFromEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryCheckmark];

    [self.eventController addPlayerToEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}

现在我不知道如何解决这个问题。

感谢您的帮助。

【问题讨论】:

  • 您的开头段落指出可以选择一个名称。这与您最后的更新相冲突。

标签: ios objective-c uitableview selection


【解决方案1】:

你需要让tableView支持多选。在viewDidLoad 中尝试以下代码:

self.myTableView.allowsMultipleSelection = YES;

那么您将不需要在各自的代表中使用这两行:

[[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];

[[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];

【讨论】:

  • 但是 OP 声明他们只想要单选。
  • 哦..对不起。当我看到复选标记时,我以为他需要多选。我通常这样做是为了多选。现在将删除答案。你的回答符合他的目的。 :)
【解决方案2】:

检查每个选定的索引,例如:使用一个作为选定索引

不,每次选择一个单元格时都将其添加到此数组中,例如:

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([selectedIndex containsObject:indexPath]) {

            [selectedIndexes removeObject:indexPath];

        }else{

            [selectedIndexes addObject:indexPath];
       }

       [tableView reloadData];
    }

然后在 CellForROwAtIndex 中

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            if ([selectedIndex containsObject:indexPath]) {
 [cell setSelected:TRUE];
    [cell setAccessoryType: UITableViewCellAccessoryCheckmark];

    [self.eventController addPlayerToEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}

}else{
 [cell setSelected:FALSE];
    [cell setAccessoryType: UITableViewCellAccessoryNone];

    [self.eventController removePlayerFromEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];


}


}

这会比从 DidSelect 方法中获取 Cell 更有效,只需确保使用 [tableView reloadData];在 didSelectRowAtIndexPath 中

【讨论】:

  • 为什么要在didDeselectRow 方法中这样做?它应该在didSelectRow 方法中。而且似乎 OP 只想要单选。
  • 所以单元格没有自己的选定状态?我认为函数[cell setSelected:true]; 应该这样做?
  • @HotPizzaBox 可能是..我不确定,但这个逻辑对我有用..我可能在我的应用程序中使用过它来完成类似于你正在做的事情跨度>
  • 我认为这种方法对于取消选择原始文件非常奇怪。您有 didDeselectRowAtIndexPath 用于执行取消选择单元格的任何操作。此代码不应用于 IMO。
  • @HarikrishnanT 你有更好的主意吗?我同意你的观点,但由于我的代码不起作用......
【解决方案3】:

如果不包含对象,您应该将附件类型设置为 UITableViewCellAccessoryNone。 因为细胞可以重复使用。 并且选择或取消选择时不需要设置selected。

if ([assignedPlayers containsObject:player.persistentData]){
    [cell setAccessoryType: UITableViewCellAccessoryCheckmark];
} else {
    cell.accessoryType= UITableViewCellAccessoryNone;
}

【讨论】:

    【解决方案4】:

    请看下面的示例,其中_selectedList 包含选定的球员,_datasoureArray 是包含所有球员姓名的数据源。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
      if(cell == nil)
       {
          cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
       }
       cell.textLabel.text = [_datasoureArray objectAtIndex:indexPath.row];
       if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]]) //hear selected list is an mutable array wich holds only selected player
        {
          cell.selected = YES;
          cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
          cell.selected = NO;
          cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
       if(cell)
       {
          if(cell.accessoryType == UITableViewCellAccessoryNone)
          {
            [_selectedList addObject:[_datasoureArray objectAtIndex:indexPath.row]];//add
          }
          else
          {
            if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]])
           {
               [_selectedList removeObject:[_datasoureArray objectAtIndex:indexPath.row]]; //remove
           }
         }
     }
     [tableView reloadData];
     NSLog(@"%@",_selectedList.description);
    }
    

    【讨论】:

      【解决方案5】:

      您可以通过以下方式实现功能, 当您选择一行时,您将检查您的数组以查找与该行相对应的人,如果它已经将其从数组中删除,否则将其添加到数组并重新加载您的表。并在 cellForRowAtIndexPath 方法中检查数组,如果人员存在,则设置为 true,否则设置为 false。

      // cellForRowAtIndexPath 函数

      {
          if ([assignedPlayers containsObject:player.persistentData])
          {
          [cell setSelected:true];
          [cell setAccessoryType: UITableViewCellAccessoryCheckmark];
          }
          else 
          {
          [cell setSelected:Flase];
          [cell setAccessoryType: UITableViewCellAccessoryCheckmarkNone];
          }
          cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];
          return cell;
      }
      
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if ([assignedPlayers containsObject:player.persistentData])
          {
          [assignedPlayers removeObject:player.persistentData];
          }
          else
          {
          [assignedPlayers addObject:player.persistentData];
          }
          [tableView reloadData];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        • 2013-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-08
        相关资源
        最近更新 更多