7

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;
  }
Hassam
  • 253
  • 6
  • 16

3 Answers3

13

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:

Nikolay Shubenkov
  • 3,133
  • 1
  • 29
  • 31
0

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];
}
Dhara
  • 4,093
  • 2
  • 36
  • 69
  • Thanks for the suggestion, but I know this method, but it add the image in the accessory view. I need the image changed in the default editing control which is on the left most side of the `UITableViewCell` – Hassam May 01 '12 at 10:13
  • y ur doing in accessory view. can u send me ur code i can help u – Dhara May 01 '12 at 10:30
  • i have edited my answer.r u using any background color for ur table – Dhara May 01 '12 at 12:49
-1

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];
                }
             }
          }
       }
    }
  }
}
bademi
  • 421
  • 5
  • 8
  • Is there a solution similar to this for iOS 7+? – Jacob Aug 13 '14 at 15:50
  • Since there is change in UITableViewCell from iOS 6 to iOS 7, you may need to change the way above code is iterating subviews. You may need to iterate till you get correct subview. Once you get subview, change it layer content with new image. – bademi Aug 15 '14 at 20:06