Удалить строки из табличного представления на основе IBAction - PullRequest
0 голосов
/ 25 июля 2010

У меня есть сегментированный элемент управления в верхней части tableView, который я хотел бы изменить данные, которые загружаются в таблицу.Сегментированный элемент управления имеет кнопку для дня, недели, месяца, года.Если вы нажмете одну из кнопок, она будет отображать данные таблицы только за соответствующий период времени.Это отлично работает сейчас, когда я перемещаюсь по кнопкам, чтобы добавить дополнительные данные в табличное представление, но когда я работаю задом наперед, из года в месяц, в неделю, в день.он не удаляет строку из таблицы.

Код для .m приведен ниже ... Также я знаю, что мои операторы if / else if ужасны, но когда я пытаюсь поместить это в свою собственную функцию, онпроблемы с возвратом ячейки в этот метод.Если бы кто-то мог дать мне лучший способ сделать это, было бы здорово!

- (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 Ответ

2 голосов
/ 26 июля 2010

Я бы хотел помочь, но это сложно, не увидев все методы UITableViewDataSource. Не могли бы вы опубликовать вашу реализацию tableView:numberOfRowsInSection:?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...