如何选择,取消选择和单个表格单元格

时间:2014-05-28 05:37:37

标签: ios objective-c xcode

直到现在我没有得到解决方案,

两个过程,

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

  2. 点击每个单元格显示复选标记,否则无。

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

  3. 我的代码:

    - (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];
    
    }
    
    像这样,

    enter image description here enter image description here

4 个答案:

答案 0 :(得分:2)

您应该重新考虑解决方案否。 1也是。您应该有NSMutableSet包含所选单元格的NSIndexPath个。

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

当您点击表格上方的按钮时,您可以将所有现有索引路径添加到集合中或清空集合。然后当然打电话给[self.tableView reloadData]

在你的tableView:cellForRowAtIndexPath:条件会改变为:

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

答案 1 :(得分:1)

- (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;
}

答案 2 :(得分: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;
    }

答案 3 :(得分:0)

如果您需要选择或取消选择单元格而不是:

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