7

在互联网上搜索了很长时间,但没有得到适当的答案。我将 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;
  }
4

3 回答 3

13

一条线解决方案在这里)

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

cell.tintColor = UIColor.red

下面是快速代码:

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
}

这是选定和未选定单元格的结果在此处输入图像描述::

于 2016-07-01T07:42:13.810 回答
0

您不能更改复选标记颜色,因为它不受支持。您可以做的是制作图像并在您的 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];
}
于 2012-05-01T10:09:07.500 回答
-1

您可以更改复选标记图像。使用复选标记图像和颜色创建复选标记图像。然后在下面的代码中替换图像名称。这适用于 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];
                }
             }
          }
       }
    }
  }
}
于 2013-11-25T22:37:56.613 回答