views:

37

answers:

1

I have a segmented control on the top of a tableView that I would like to change the data that is loaded into the table. The segmented control has a button for day, week, month, year. If you click on one of the buttons it should only display that table data for the appropriate time period. This works fine right now when I move across the buttons, in order to add extra data onto the table view, but when i work backwards from year, to month, to week, to day. it doesnt remove the row from the tableview.

The code for the .m is below...Also i know my if/else if statements are horrid but when i try and put that into its own function it has issues with returning the cell to this method. If someone could give me a better way to do that too, would be great!

- (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;
}
+2  A: 

I'd like to help, but it's difficult without seeing all of the UITableViewDataSource methods. Could you post your implementation of tableView:numberOfRowsInSection:?

Justin
I'd have commented on the question directly, but I don't think I have enough reputation.
Justin
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [eventsArray count];}
adam0101
Do you ever change the contents of eventsArray? It's not unusual to back a UITableView with an array, but your approach is unique. I'd recommend rewriting `cellForRowAtIndexPath:` to blindly display whatever events are in the eventsArray. Then you can set the contents of eventsArray when your selected segment changes and call `reloadData` afterwards.
Justin