Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

tableview repeat values when displaying

guns65guns65 tunisiaPosts: 2New Users Noob
hi i have a tableview of patients and i want to display the consultations of each patient but my table view repeat the values what i do is that i pass the id of the patient to the second table view and i get his consultation by hid id == i don't know why i get repeated values in the second tableview i need help thx :)

#import "consultationViewController.h"
#import "detailsPatientViewController.h"
#import "Consultation.h"
#import "AFNetworking.h"
#import "AppDelegate.h"
#import "ajouterConsultationViewController.h"

@interface consultationViewController ()

@implementation consultationViewController
@synthesize idPatientConsultation;
@synthesize labelTaille;
@synthesize labelMotif;
@synthesize labelObservation;
@synthesize labelPoids;
@synthesize labelPouls;
@synthesize labelTA;
@synthesize labelTemperature;
@synthesize textViewObservation;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
return self;


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://localhost:8888/services/consultation.php"; parameters:nil

success:^(AFHTTPRequestOperation *operation, id responseObject)

NSLog(@JSON out : %@",responseObject);

ConsultationsTab = (NSArray *) [responseObject valueForKeyPath:@items];

NSLog(@nombre des elements : %d, ConsultationsTab.count );

for (Consultation *consultation in consultationsList) {
[consultation.managedObjectContext deleteObject:consultation];

[self.managedObjectContext1 save:nil];

for (int i=0; i<ConsultationsTab.count; i++) {

Consultation *consultation = [NSEntityDescription insertNewObjectForEntityForName:@Consultation

NSDictionary *consultationDic = [ConsultationsTab objectAtIndex:i];

// NSLog(@your name is : %@ %@", [consultationDic objectForKey:@id_consultation], [consultationDic objectForKey:@id_patient]);

//id consultation//
NSString *idConsultation = [NSString stringWithFormat:@%@",[consultationDic objectForKey:@id_consultation]];
int idValue = [idConsultation intValue];
NSNumber *idNumber = [NSNumber numberWithInt:idValue];

consultation.id_consultation = idNumber;

NSString *idPatient = [NSString stringWithFormat:@%@",[consultationDic objectForKey:@id_patient]];
int idVal = [idPatient intValue];
NSNumber *idNum = [NSNumber numberWithInt:idVal];

consultation.id_patient = idNum;

NSString* strDate = [consultationDic objectForKey:@date_consultation];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:@yyyy-MM-dd HH:mm:ss];

NSDate *date = [formatter dateFromString:strDate];
// NSLog(@Date de consultation : %@",date);

consultation.date_consultation = date;

consultation.taille = [consultationDic objectForKey:@taille];
consultation.poids = [consultationDic objectForKey:@poids];
consultation.pouls = [consultationDic objectForKey:@pouls];
consultation.motif = [consultationDic objectForKey:@motif_consultation];

consultation.observation = [consultationDic objectForKey:@observation];
consultation.temperature = [consultationDic objectForKey:@temperature];
consultation.tension_arterielle = [consultationDic objectForKey:@tension_arterielle];

consultationsList = [self getAllConsultations];
[self.tableConsultation reloadData];

NSError *error;
if (![self.managedObjectContext1 save:&error])
NSLog(@Problème d'enregistrement : %@",[error localizedDescription ] );


failure:^(AFHTTPRequestOperation *operation, NSError *error)

NSLog(@Error : %@",error);

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@error
message:[NSString stringWithFormat:@" %@",error]
[alert show];


-(NSMutableArray *)getAllConsultations
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@Consultation inManagedObjectContext:self.managedObjectContext1];
[fetchRequest setEntity:entity];

NSString *Id = idPatientConsultation;
NSLog(@" id Patient Consultation : %@",Id);

NSPredicate *predicate = [NSPredicate predicateWithFormat:@id_patient ==[c] %@",Id];
[fetchRequest setPredicate:predicate];

NSError *error;
NSArray *arrayConsultations = [self.managedObjectContext1 executeFetchRequest:fetchRequest error:&error];
NSLog(@Nombre de Consultations : %d,arrayConsultations.count);
NSMutableArray *mutConsultationsList = [NSMutableArray arrayWithArray:arrayConsultations];

return mutConsultationsList;


- (void)viewDidLoad
[super viewDidLoad];
self.tableConsultation.dataSource = self;
self.tableConsultation.delegate = self;
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
self.managedObjectContext1 = appDelegate.managedObjectContext;

NSLog(@ id view did load);

consultationsList = [self getAllConsultations];
[self listeDesConsultations];


[super viewDidAppear: animated];
NSLog(@ id view did appear);
consultationsList = [self getAllConsultations];
[self.tableConsultation reloadData];


-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
NSLog(@ Nombre des consultations : %d ,consultationsList.count);
return consultationsList.count;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @SimpleTableItem;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if(cell == nil)
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

Consultation *consult = [consultationsList objectAtIndex:indexPath.row];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@yyyy-MM-dd HH:mm:ss];
NSString *stringFromDate = [formatter stringFromDate:consult.date_consultation];

cell.textLabel.text = [NSString stringWithFormat:@Date : %@", stringFromDate ];

return cell;

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
UIView *view = [[UIView alloc]init];
return view;

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
headerView.backgroundColor = [UIColor whiteColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 320, 20)];
title.text = @Liste des Consultations;
[headerView addSubview:title];

return headerView;


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

Consultation *consult = [consultationsList objectAtIndex:indexPath.row];

labelTemperature.text = consult.temperature;
labelTaille.text = consult.taille;
labelTA.text = consult.tension_arterielle;
labelPouls.text = consult.pouls;
labelPoids.text = consult.poids;
labelMotif.text = consult.motif;

labelObservation.text = consult.observation;


- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

- (IBAction)refreshConsultations:(id)sender {
// consultationsList = [self getAllConsultations];
[self listeDesConsultations];

- (IBAction)reloadConsultation:(id)sender {
[self listeDesConsultations];

- (IBAction)ajouterConsultation:(id)sender {

ajouterConsultationViewController *ajouterConsultation = [[ajouterConsultationViewController alloc]init];

[self.navigationController pushViewController:ajouterConsultation animated:YES];



  • Duncan CDuncan C Posts: 9,112Tutorial Authors, Registered Users @ @ @ @ @ @ @
    That is way too much code to wade through without code tags. Go back and add code tags. (There should be a sticky thread at the top of the board called something like "making your code look nice")

    The usual reason for table views repeating data is a mis-use of cell recycling.

    Remember that any time you dequeue a cell, it is likely a cell that was used before, and still has the old data in it.

    Your code should be structured like this:

    cell = dequeue old cell.
    if (!cell)
       cell = new cell
       add any fields that need to be added to the "bare" new cell"
    //Now configure EVERY field in the cell. If this cell does not use the label field, explicitly set it to blank, since it might have text from before.
    Duncan C

    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • guns65guns65 tunisiaPosts: 2New Users Noob
    how do i set the label field to blank ==
  • Ice_2kIce_2k Posts: 154Registered Users @ @
    label.text = @";";
    <a href=""; target="_blank">My 1st app</a><br />
    <a href=""; target="_blank">My 2nd app</a>
Sign In or Register to comment.