2010-07-25 6 views
0

J'ai un contrôle segmenté en haut d'une tableVue que je voudrais modifier les données qui sont chargées dans la table. Le contrôle segmenté dispose d'un bouton pour le jour, la semaine, le mois et l'année. Si vous cliquez sur l'un des boutons, il doit uniquement afficher les données de la table pour la période appropriée. Cela fonctionne bien maintenant quand je me déplace sur les boutons, afin d'ajouter des données supplémentaires sur la vue de la table, mais quand je travaille en arrière de l'année, au mois, à la semaine, au jour. il ne supprime pas la ligne de la tableview. Le code pour le .m est ci-dessous ... Aussi je connais mon if/else si les instructions sont horribles mais quand j'essaye de le mettre dans sa propre fonction il a des problèmes avec le retour de la cellule à cette méthode. Si quelqu'un pouvait me donner une meilleure façon de faire ça, ce serait génial!Supprimer des lignes de tableview basé sur IBAction

- (IBAction)segmentTimePicker:(id)sender { 
// the segmented control was clicked, handle it here 
NSLog(@"segment clicked %d", [segmentedControl selectedSegmentIndex]); 
self.tableView.reloadData; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

// A date formatter for the creation date. 
    static NSDateFormatter *dateFormatter = nil; 
if (dateFormatter == nil) { 
    dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
} 

//A number formatter for the latitude and longitude 
static NSNumberFormatter *numberFormatter = nil; 
if (numberFormatter == nil) { 
    numberFormatter = [[NSNumberFormatter alloc] init]; 
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
    [numberFormatter setMaximumFractionDigits:3]; 
} 

    static NSString *CellIdentifier = @"Cell"; 

//Dequeue or create a new cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    UITableViewCellStyleSubtitle; 
    } 

//Calc how many days have passed since today, only display the right days for this segmented selector 
unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit; 
    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 



// Get the event corresponding to the current index path and configure the table view cell. 
    // Configure the cell... 
Feeding *food = (Feeding *)[eventsArray objectAtIndex:indexPath.row]; 
//Check how many days have gone by for 
NSDateComponents *comps = [gregorian components:unitFlags fromDate:[food feedingDate] toDate:[NSDate date] options:0]; 
int days = [comps day]; 

//Logic to test which segment is picked, need to tie it to the segmentedControl selector 
if (segmentedControl.selectedSegmentIndex==0) { 
    if (days<=1) { 

    if([[food feedingType]isEqualToString:@"Bottle"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]]; 
    cell.textLabel.text= string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if ([[food feedingType]isEqualToString:@"Breast"]) { 
    NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if([[food feedingType]isEqualToString:@"Solid"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else { 
    NSLog(@"Something went wrong with this food type '%@'",[food feedingType]); 
    } 
    } 
    else { 
    NSLog(@"Do nothing since its older then a day"); 
    } 
} 

else if(segmentedControl.selectedSegmentIndex==1) { 
    if (days<=7) { 

    if([[food feedingType]isEqualToString:@"Bottle"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]]; 
    cell.textLabel.text= string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if ([[food feedingType]isEqualToString:@"Breast"]) { 
    NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if([[food feedingType]isEqualToString:@"Solid"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else { 
    NSLog(@"Something went wrong with this food type '%@'",[food feedingType]); 
    } 
    } 
    else { 
    NSLog(@"Do nothing since its older then a week"); 
    } 
} 
else if(segmentedControl.selectedSegmentIndex==2) { 
    if (days<=30) { 

    if([[food feedingType]isEqualToString:@"Bottle"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]]; 
    cell.textLabel.text= string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if ([[food feedingType]isEqualToString:@"Breast"]) { 
    NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if([[food feedingType]isEqualToString:@"Solid"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else { 
    NSLog(@"Something went wrong with this food type '%@'",[food feedingType]); 
    } 
    } 
    else { 
    NSLog(@"Do nothing since its older then a month"); 
    } 
} 
else { 
    if([[food feedingType]isEqualToString:@"Bottle"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ oz of Formula",[food feedingAmount]]; 
    cell.textLabel.text= string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if ([[food feedingType]isEqualToString:@"Breast"]) { 
    NSString *string = [NSString stringWithFormat:@"Min on L Breast %@, Min on R Breast %@",[food feedingLBreast],[food feedingRBreast]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else if([[food feedingType]isEqualToString:@"Solid"]) { 
    NSString *string = [NSString stringWithFormat:@"%@ ounces of %@,%@d",[food feedingAmount],[food feedingSolidName],[food feedingSolidLike]]; 
    cell.textLabel.text = string; 
    cell.detailTextLabel.text = [dateFormatter stringFromDate:[food feedingDate]]; 
    } 
    else { 
    NSLog(@"Something went wrong with this food type '%@'",[food feedingType]); 
    } 
} 



    return cell; 
} 

Répondre

2

Je voudrais vous aider, mais c'est difficile sans voir toutes les méthodes UITableViewDataSource. Pourriez-vous poster votre implémentation de tableView:numberOfRowsInSection:?

+1

J'aurais commenté la question directement, mais je ne pense pas avoir assez de réputation. – Justin

+0

- (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { \t return 1; } - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: section (NSInteger) { retour [eventsArray compte]; } – adam0101

+0

Avez-vous déjà modifié le contenu de eventsArray? Il n'est pas inhabituel de sauvegarder un UITableView avec un tableau, mais votre approche est unique. Je vous recommande de réécrire 'cellForRowAtIndexPath:' pour afficher aveuglément les événements qui se trouvent dans le tableau events. Ensuite, vous pouvez définir le contenu de eventsArray lorsque votre segment sélectionné change et appeler 'reloadData' par la suite. – Justin

Questions connexes