Hassam
Hassam

Reputation: 253

Change checkmark color in UITableView editing-mode?

After searching all over internet for a long time, but not getting the appropriate answer. I am putting the UITableView in editing mode and selecting multiple rows at a time. It is working great, but I wanted to change the color of checkmark from red to blue same as it is in the iPhone email app.

Any help would be appreciated.

Edited Version:

Here is my code...

in my ViewDidLoad function:

- (void)viewDidLoad
{
   ...

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

   [super viewDidLoad];
}

I have two UIButtons whhich set the editing mode for the tableview as follows:

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

The UITableView delegate methods are:

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

Upvotes: 7

Views: 9537

Answers (3)

Nikolay Shubenkov
Nikolay Shubenkov

Reputation: 3223

One line solution is here)

Just set cell tintColor in your cellForRow:atIndexPath method.

cell.tintColor = UIColor.red

Below is swift code:

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
}

And this is the result of selected and not selected cells:enter image description here:

Upvotes: 14

bademi
bademi

Reputation: 421

You can change the checkmark image. Create check mark image with check mark image and color. Then replace image name in the below code. This works fine for iOS 6 and below

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

Upvotes: -1

Dhara
Dhara

Reputation: 4089

You cant change checkmark color as its not supported.The thing which you can do is make an image and add following code in your 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];
}

Upvotes: 0

Related Questions