Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:51:13+00:00 2026-05-16T00:51:13+00:00

I have a segmented control on the top of a tableView that I would

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:51:13+00:00Added an answer on May 16, 2026 at 12:51 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to have a button on the self.navigationItem.rightButton that toggles a segmented control
I have a UIScrollView that contains an image and a segmented control that allows
I have two array and three button in segment control i want that when
I have a segmented control that allows both short and long gestures. The short
I have a table that has its content filled using one of the two
https://i.stack.imgur.com/2KxdM.png I have this segmented control that I made, however I don't know how
I have four NSDictionaries that I would like to use to populate a pickerview
I am developing an iphone app in which i have a segmented control that
I have created an up/down arrow segmented control button on the right side of
I have a login/signup page that is changed using a segmented control. The login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.