Вопрос

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
Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top