3

I'm developing an iOS application, using Xcode 4.6 to code in Objective-C. I want to know how to put on a selected table view cell a check mark, and then to access to those cells already marked to reuse them, for example saving them in an array. Here is my table view class code so you can see:

#import "TablaMateriasViewController2.h"

@interface TablaMateriasViewController2 ()

@end

@implementation TablaMateriasViewController2
@synthesize materias,materiasKeys;
NSMutableArray *Materias;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

Materias = [[NSMutableArray alloc]init ];

Materia  *mat  = [[Materia alloc]init];
[mat setNombre:@"Matematicas I"];
[mat setCodigo:@"FBTMI01"];
[mat setGradoDificultad:3];
[mat setDescripcion:@"..."];
[Materias addObject:mat];

mat  = [[Materia alloc]init];
[mat setNombre:@"Matematicas II"];
[mat setCodigo:@"FBTMI02"];
[mat setGradoDificultad:4];
[mat setDescripcion:@"......"];
[Materias addObject:mat];

mat  = [[Materia alloc]init];
[mat setNombre:@"Fisica I"];
[mat setCodigo:@"FBTFI01"];
[mat setGradoDificultad:2];
[mat setDescripcion:@".."];
[Materias addObject:mat];

mat  = [[Materia alloc]init];
[mat setNombre:@"Fisica II"];
[mat setCodigo:@"FBTFI02"];
[mat setGradoDificultad:4];
[mat setDescripcion:@"!!!!"];
[Materias addObject:mat];

mat  = [[Materia alloc]init];
[mat setNombre:@"Fiscia III"];
[mat setCodigo:@"FBTFI03"];
[mat setGradoDificultad:5];
[mat setDescripcion:@"---"];
[Materias addObject:mat];

mat  = [[Materia alloc]init];
[mat setNombre:@"Estructura de Datos"];
[mat setCodigo:@"BPTPR12"];
[mat setGradoDificultad:4];
[mat setDescripcion:@"Orientacion a objetos"];
[Materias addObject:mat];

mat  = [[Materia alloc]init];
[mat setNombre:@"Algoritmos y Programacion"];
[mat setCodigo:@"BPTPR11"];
[mat setGradoDificultad:3];
[mat setDescripcion:@"estructurada"];
[Materias addObject:mat];

mat  = [[Materia alloc]init];
[mat setNombre:@"Matematicas III"];
[mat setCodigo:@"FBTMI03"];
[mat setGradoDificultad:3];
[mat setDescripcion:@"Mate 3"];
[Materias addObject:mat];    
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return Materias.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
static NSString *CellIdentifier = @"MateriaCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Materia *current = [Materias objectAtIndex:indexPath.row];
[cell.textLabel setText:current.Nombre];

return cell;
}


#pragma mark - Table view delegate

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

@end
Johnny Dahdah
  • 995
  • 5
  • 15
  • 21
  • See http://stackoverflow.com/questions/3040894/uitableview-multiple-selection – aqua Mar 10 '13 at 20:32
  • possible duplicate of [Displaying checkmark on cells iOS 5](http://stackoverflow.com/questions/9449568/displaying-checkmark-on-cells-ios-5) – Monolo Mar 10 '13 at 20:42

1 Answers1

3

If you want to keep track of multiple check marks, it would be easiest to add a property to your Materia object that is a BOOL, like isChecked or something. In the cellForRowAtIndexPath method, you would would check the state of the BOOL, and add the check mark if its YES, and not if it's NO. You shouldn't save cells in an array, your data source array, Materias, would then have a record of which cells are selected, and you can use that for whatever purpose you want. You would update the value of that BOOL in the didSelectRowAtIndexPath method.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Ok thank you, that sounds good. But how do I put the check mark in the table view cell on the screen? Is there any especific item? – Johnny Dahdah Mar 10 '13 at 20:46
  • If you're using one of the standard UITableViewCell types set the "accessoryType" property -- cell.accessoryType = UITableViewCellAccessoryCheckmark; -- and to clear it: cell.accessoryType = UITableViewCellAccessoryNone; – Charlie Price Mar 10 '13 at 21:05
  • @CharliePrice Thank you. And I have one final doubt. When I run the application, I want to touch on the table view cell that I want to put a check mark, and then make the check mark appears. How can I do that? Do I have to put an action button in each table cell that I want to put the check mark? – Johnny Dahdah Mar 10 '13 at 21:23