在 UITableView 编辑模式中更改复选标记颜色?

Posted

技术标签:

【中文标题】在 UITableView 编辑模式中更改复选标记颜色?【英文标题】:Change checkmark color in UITableView editing-mode? 【发布时间】:2012-04-30 06:38:38 【问题描述】:

在互联网上搜索了很长时间,但没有得到适当的答案。我将 UITableView 置于编辑模式并一次选择多行。它工作得很好,但我想将复选标记的颜色从红色更改为蓝色,就像在 iPhone 电子邮件应用程序中一样。

任何帮助将不胜感激。

编辑版本:

这是我的代码...

在我的 ViewDidLoad 函数中:

- (void)viewDidLoad

   ...

   [deviceTableVIew setAllowsSelectionDuringEditing:YES];
   [deviceTableVIew setAllowsMultipleSelectionDuringEditing:YES];

   [super viewDidLoad];

我有两个 UIButtons 设置 tableview 的编辑模式如下:

-(IBAction)control:(id)sender
   btnControl.enabled = false;
   btnControl.hidden = true;        
   btnCancel.enabled = true;
   btnCancel.hidden = false;    
   stateToggleToolbar.hidden = false;    
   [self.deviceTableVIew setEditing:YES animated:YES];


-(IBAction)cancel:(id)sender
   btnCancel.enabled = false;
   btnCancel.hidden = true;
   btnControl.enabled = true;
   btnControl.hidden = false;    
   stateToggleToolbar.hidden = true;
   [self.deviceTableVIew setEditing:NO animated:YES];

UITableView 的委托方法是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

   //setting up the cells here

   return cell;



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

     if ([tableView isEditing] == YES) 
       // Do Nothing
     else
       [self.navigationController pushViewController:viewController animated:YES];
     

  

  -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

  

  - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

      return 3;
  

【问题讨论】:

看看这个希望对你有帮助***.com/questions/7641228/… 我以前看过参考资料。但是你能告诉我怎么做,因为我是 iPhone 开发的新手。 U 制作一个蓝色的自定义图像并使用该 code.cell.accessoryView = checkmark; [复选标记发布]; 你需要在 cellForRowAtIndex 中写下这个 是否可以将左侧文本标签前的复选标记颜色从红色更改为蓝色,就像在 iPhone 电子邮件应用程序中一样。 【参考方案1】:

一条线解决方案在这里)

只需在 cellForRow:atIndexPath 方法中设置单元格 tintColor

cell.tintColor = UIColor.red

下面是swift代码:

func tableView(tableView: UITableView,
        cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell 

    //get cell to configure from tableView
    let cell = tableView.dequeueReusableCellWithIdentifier("myCellIdentifier", forIndexPath: indexPath) as UITableViewCell

    //This line will change checkmark color of your awesome cell
    cell.tintColor = UIColor.redColor()

    // Configure cell  
    // ...

    //work is done! Let put the cell back to the tableView))
    return cell

这是选中和未选中单元格的结果::

【讨论】:

【参考方案2】:

您不能更改复选标记的颜色,因为它不受支持。您可以做的是制作图像并在您的 cellForRowAtIndexPath 中添加以下代码

 - (void)viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];
    selected=[[NSMutableArray alloc] init];
    for(int i=0;i<10;i++)
        [selected addObject:@"0"];
    

- (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] autorelease];
    
    cell.textLabel.text=[NSString stringWithFormat:@"hello %d",indexPath.row];
    if([[selected objectAtIndex:indexPath.row] isEqualToString:@"1"])
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
    
    else
        cell.accessoryType=UITableViewCellAccessoryNone;
    
    // Configure the cell...

    return cell;

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

    if([[selected objectAtIndex:indexPath.row] isEqualToString:@"0"])
        [selected replaceObjectAtIndex:indexPath.row withObject:@"1"];
    
    else
        [selected replaceObjectAtIndex:indexPath.row withObject:@"0"];
    
    [tbl reloadData];

【讨论】:

感谢您的建议,但我知道这种方法,但它会在附件视图中添加图像。我需要在UITableViewCell最左侧的默认编辑控件中更改图像 您在附件视图中进行操作。你能把你的代码发给我吗,我可以帮助你 我已经编辑了我的 answer.r 你为你的表格使用任何背景颜色【参考方案3】:

您可以更改复选标记图像。使用复选标记图像和颜色创建复选标记图像。 然后在下面的代码中替换图像名称。这适用于 ios 6 及更低版本

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

     [super setSelected:selected animated:animated];
     if (self.isEditing) 
          if (selected)
              for (UIView *subview in self.subviews) 
                if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) 
                for (UIView *aView in subview.subviews) 
                    if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) 
                        [aView.layer setContents:(id)[UIImage imageNamed:@"delete_check.png"].CGImage];
                
             
          
       
    
  

【讨论】:

iOS 7+ 有类似的解决方案吗? 由于 UITableViewCell 从 iOS 6 到 iOS 7 发生了变化,您可能需要更改上述代码迭代子视图的方式。您可能需要迭代直到获得正确的子视图。获得子视图后,将其更改为新图像的图层内容。

以上是关于在 UITableView 编辑模式中更改复选标记颜色?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UITableView 的编辑模式下只显示复选标记选择而不选择整个 UITableViewCell?

tableview 编辑模式单元格选择复选标记在滚动时消失

UITableView更改多选复选标记的颜色

如何为uitableview自定义多选编辑模式

UITableviewCell - 在编辑模式下单击空复选标记圆圈不会选择单元格

UITableView 中需要多次点击以进行复选标记