0

我有带有三个按钮和其他一些组件的自定义 tableviewcell。

在此处输入图像描述 我在弹出框内添加了 datepicker(我使用了 FPPopover 库来执行此操作)来获取“开始日期”的值。到目前为止,我已经做到了,

“cellForRowAtIndexPath”方法中,

 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;

“startDateButtonPressed”方法中,我做了以下事情。

- (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;
}

}

而在“selectedDate”方法中,

-(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];
}

一切正常!但我的问题是我无法将选定的日期字符串(通过弹出框)值分配给“开始日期”按钮标题。我怎样才能做到这一点?有什么简单的方法可以做到这一点?

4

1 回答 1

2

试试这个代码:

在 .h 文件中

 UIButton *btn;

在 .m 文件中

-(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];

}
于 2013-06-03T09:42:15.447 回答