简体   繁体   中英

Popover inside a custom tableview cell

I have custom tableviewcell with three buttons and some other components.

在此输入图像描述 I have added datepicker inside the popover (I have used FPPopover library to do this) to get the value for "Start Date". Here I have done so far,

in the method, 方法中,

 MainTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MainTableViewCell"];

 if (!cell) {
    cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:@"MainTableViewCell"];

        NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:self options:nil];

        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell = (MainTableViewCell *)currentObject;
                break;
            }
        }

}
...

...

[cell.startDateMainBtn addTarget: self action: @selector(startDateButtonPressed:) forControlEvents: UIControlEventTouchUpInside];
cell.startDateMainBtn.tag = indexPath.row;
cell.startDateMainBtn.titleLabel.tag = 1;

and in the "startDateButtonPressed" method i have did the following.

- (void) startDateButtonPressed:(id) sender
{
NSUInteger senderId = [sender tag];

if (!self.popoverContent) {
    self.popoverContent = [[UIViewController alloc] init]; //ViewController
}
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];

datePicker = [[UIDatePicker alloc] init]; //Date picker
datePicker.frame = CGRectMake(0, 0, 320, 216);
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setTag:senderId];
[datePicker addTarget:self action:@selector(selectedDate:) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];

self.popoverContent.view = popoverView;
popover.alpha = 0.8;
popover = [[FPPopoverController alloc] initWithViewController:self.popoverContent];
popover.contentSize = CGSizeMake(340, 266);
popover.arrowDirection = FPPopoverArrowDirectionUp;
[popover presentPopoverFromView:sender];

switch ([[sender titleLabel] tag]) {

        //START DATE
    case 1:
    {
        self.buttonTypeTag = 1;
    }
        break;

    default:
        break;
}

}

And in the "selectedDate" method,

-(void)selectedDate:(id) sender
{
NSLog(@"SELECTED Date %@ , Row %i",datePicker.date, datePicker.tag);

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy,MM,dd"];

//    UIButton *btn = (UIButton *)sender;
//    [btn setTitle:@"date" forState:UIControlStateNormal];

switch (self.buttonTypeTag) {

       //START DATE 
    case 1:
    {          
//            [self.cell.startDateMainBtn setTitle:[df stringFromDate:datePicker.date] forState:UIControlStateNormal];
        NSString *dateStr = [df stringFromDate:datePicker.date];
        NSLog(@"SELECTED Date %@",dateStr);
    }
        break;

    default:
        break;
}

[popover dismissPopoverAnimated:YES];
}

All working fine! but my problem is I can't assign selected date string (by popover) value to the Start Date button title. How can I do that? Is there any easy way to do this?

try this code:

in .h file

 UIButton *btn;

in .m file

-(void)startDateButtonPressed:(id) sender
{

 btn=(UIButton *)sender;
}
-(void)selectedDate:(id) sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy,MM,dd"];
NSString *dateStr = [df stringFromDate:datePicker.date];
[btn setTitle:dateStr forState:UIControlStateNormal];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM