Expanding and collapsing UITableViewCells with DatePicker

后端 未结 6 1437
一生所求
一生所求 2020-12-02 05:21

I\'m building an app that lets the user select dates from a UITableView. The tableView is static and grouped. I\'ve looked through many questions, including this one, trying

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 05:52

    I assume you're using storyboard, the example is with UIPickerView: Create a tableviewcell right under the cell that contains the textfield you want to fill and set the cells row height to 216.0 in the inspector and add a UIPickerView to that cell.

    see here

    Next connect the UIPickerView via Outlet to your viewcontroller and add the following property to your ViewController.h:

    @property (weak, nonatomic) IBOutlet UIPickerView *statusPicker;
    @property BOOL statusPickerVisible;
    

    In your ViewController.m do in viewWillAppear

    self.statusPickerVisible = NO;
    self.statusPicker.hidden = YES;
    self.statusPicker.translatesAutoresizingMaskIntoConstraints = NO;
    

    Add two methods:

    - (void)showStatusPickerCell {
        self.statusPickerVisible = YES;
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        self.statusPicker.alpha = 0.0f;
        [UIView animateWithDuration:0.25 
                     animations:^{
                         self.statusPicker.alpha = 1.0f;
                     } completion:^(BOOL finished){
                         self.statusPicker.hidden = NO;
                     }];];
    }
    
    - (void)hideStatusPickerCell {    
        self.statusPickerVisible = NO;
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
        [UIView animateWithDuration:0.25
                     animations:^{
                         self.statusPicker.alpha = 0.0f;
                     }
                     completion:^(BOOL finished){
                         self.statusPicker.hidden = YES;
                     }];
    }
    

    In heightForRowAtIndexPath

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        CGFloat height = self.tableView.rowHeight;
        if (indexPath.row == 1){
            height = self.statusPickerVisible ? 216.0f : 0.0f;
        }
        return height;
    }
    

    In didSelectRowAtIndexPath

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 0) {
            if (self.statusPickerVisible){
                [self hideStatusPickerCell];
            } else {
                [self showStatusPickerCell];
            }
        }
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

提交回复
热议问题