tableView que muestre datos de tabla de base de datos DB
1) en el .h añadir un NSMutableArray
@property (strong, nonatomic) NSMutableArray *myData;
2) en viewWillAppear llamada a la rutina que cargará los datos en la array
-(void) viewWillAppear:(BOOL)animated {
self.myData = [[NSMutableArray alloc]init];
self.myData = [[DBManager sharedInstance] getAllChilds];
[self.tableView reloadData];
}
3) numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.myData count];
}
4) en cellForRowAtIndexPath
LOChild* child = [self.myData objectAtIndex:indexPath.row];
5) En DBManager.m
- (NSMutableArray*)getAllChilds{
LOChild * child = nil;
NSMutableArray * entities = [[NSMutableArray alloc] init];
@try {
@synchronized(lock){
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
//[self encrypt];
NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM %@", TABLE_CHILD];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
child = [[LOChild alloc] init];
for(int i = 0; i< sqlite3_column_count(statement);i++){
const char* column = sqlite3_column_name(statement, i);
NSString* c = [NSString stringWithUTF8String:column];
if([c isEqualToString:@"id"]){
[child setMemberId:[NSNumber numberWithInt:sqlite3_column_int(statement, i)]];
}
else if([c isEqualToString:@"name"]){
[child setName:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, i)]];
}
else if([c isEqualToString:@"email"]){
[child setEmail:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, i)]];
}
else if([c isEqualToString:@"photoURL"]){
[child setPhotoURL:[iNMTypeConversions stringToURL:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, i)]]];
}
}
[entities addObject:child];
}
sqlite3_finalize(statement);
}
}
sqlite3_close(database);
}
} @catch (NSException* e) {
NSLog(@"%@", [NSString stringWithFormat:@"Exception in getAllChilds: %@", e.reason]);
}
NSMutableArray *sortedArray;
sortedArray = [[entities sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
return sortedArray;
}
6) implementar un compare en el objeto:
- (NSComparisonResult)compare:(LOChild *)otherObject {
return [self.name compare:otherObject.name];
}
7) Habilitar que se puedan eliminar celdas:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
LOChild *child = [self.myData objectAtIndex:indexPath.row];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[LORestManager sharedInstance] deleteMember:child.memberId returnHandler:^(int messageId, id json) {
dispatch_async(dispatch_get_main_queue(), ^{
if (messageId>=0){
//recoger datos de ws
LOChild *child = [LOChild mapBasicChild:json];
//FIXME: Tendrá que guardar la URL de la imagen subida
child.photoURL = nil;
//remove the deleted object from your data source.
//If your data source is an NSMutableArray, do this
[self.myData removeObjectAtIndex:indexPath.row];
[tableView reloadData]; // tell table to refresh now
}
});
}];
});
}
}