【问题标题】:how to select, deselect and individual table cell如何选择、取消选择和单个表格单元格
【发布时间】:2014-07-17 04:46:46
【问题描述】:

直到现在我还没有解决这个问题,

两个进程,

  1. 我在 tableview 中有一个按钮,如果我单击该按钮,则显示该部分中的所有复选标记,否则不显示(切换功能)。

  2. 每一个单元格被点击显示复选标记,否则没有。

    我已经完成了第一个案例,但我也想要第二个。

我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell* cell = nil;
   NSString *key = [_keys objectAtIndex:indexPath.section];
   NSArray *name = [_names objectForKey:key];
   static NSString *MyCellIdentifier = @"Cell";
   cell = [tableView dequeueReusableCellWithIdentifier:nil];

   if (cell == nil){
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:MyCellIdentifier];
       cell.textLabel.text = [name objectAtIndex:indexPath.row];
   
       if([selectAll.currentTitle isEqualToString:@"Deselect All"])
         {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
         }
       else
         {
            cell.accessoryType = UITableViewCellAccessoryNone;
         }
   }
  return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

 if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        selectAll.frame = CGRectMake(frame.size.width-90, 5, 86, 30);
    }
    else {
        selectAll.frame = CGRectMake(cmdSwitch.frame.origin.x, 0, 90, 30);
    }
    
    if([[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"]==nil)
    {
        [selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
        
    }
    else
    {
        if([[[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"] isEqualToString:@"1"])
        {
            [selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
        }
        else if([[[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"] isEqualToString:@"0"])
        {
            [selectAll setTitle:@"Select All" forState:UIControlStateNormal];
        }
        
    }
    
    selectAll.layer.cornerRadius = 7;
    selectAll.clipsToBounds = YES;
    selectAll.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    selectAll.backgroundColor = [UIColor whiteColor];
    selectAll.layer.borderColor = [UIColor colorWithRed:155.0f/255.0f green:155.0f/255.0f blue:155.0f/255.0f alpha:1.0f].CGColor;
    selectAll.layer.borderWidth = 1;
    [selectAll setTitleColor:[UIColor colorWithRed:43.0f/255.0f green:65.0f/255.0f blue:116.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    [selectAll setExclusiveTouch:YES];
    [selectAll addTarget:self action:@selector(mySelect:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:selectAll];
}

return headerView;
}

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

 [self checkedCellAtIndex:indexPath.row];
        
        if([self getCheckedForIndex:indexPath.row]==YES)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
 }

//我的按钮功能

- (void) mySelect:(NSIndexPath *)indexPath {

if([selectAll.currentTitle isEqualToString:@"Deselect All"])
{
    [selectAll setTitle:@"Select All" forState:UIControlStateNormal];
    [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"Sel_Check"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
    [selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
    [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"Sel_Check"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

[self.tableView reloadData];

}

像这样,

【问题讨论】:

  • 什么这么复杂?如果选择单元格检查以查看是否选择了所有单元格。 span>
  • 您想通过单击单元格来选择几个特定的​​单元格,或者您只想通过单击 abouve 按钮来选择所有单元格并再次单击它时取消选择??

标签: ios objective-c xcode


【解决方案1】:
Steps to follow: 
(1) Make an array (i.e arrCheckedItems) for holding checked cell texts (i.e. @"Five Cents" , @"Ten Cents").
(2) When user select any cell, add that cell's text to arrCheckedItems and when deselect remove that text from array. Then reload the table.
(3) in cellForRowAtIndexPath: method, check cell's text- 



 if([arrCheckedItems containsObject: cell.text])
    {
         // cell.text = @"Five Cents" or @"Ten Cents"
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

【讨论】:

    【解决方案2】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell* cell = nil;
        static NSString *MyCellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:nil];
    
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:MyCellIdentifier];
            cell.textLabel.text = [candidates objectAtIndex:indexPath.row];
    
            if([self.selectButton.titleLabel.text isEqualToString:@"Deselect All"])
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
        return cell;
    }
    

    【讨论】:

      【解决方案3】:

      您应该重新考虑拒绝的解决方案。 1 也一样。您应该有一个NSMutableSet,其中包含所选单元格的NSIndexPath

      当您只点击一个单元格时,将该索引路径添加到集合中或将其删除(如果它已经存在),然后重新加载选定的行。

      当您点击表格上方的按钮时,您要么将所有现有索引路径添加到集合中,要么清空集合。然后当然是致电[self.tableView reloadData]

      在您的tableView:cellForRowAtIndexPath: 中,条件将变为:

         if([_indexPathsOfSelectedRows containsObject:indexPath])
           {
              cell.accessoryType = UITableViewCellAccessoryCheckmark;
           }
         else
           {
              cell.accessoryType = UITableViewCellAccessoryNone;
           }
      

      【讨论】:

        【解决方案4】:

        如果您需要在点击时选择或取消选择单元格:

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            else
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
        

        【讨论】:

        • 这里我是按钮和单个单元格切换功能
        • 当他滚动表格时,选择就会消失
        • @user3571918 我刚刚给出了整个实施的想法:)
        最近更新 更多