在自定义tableview单元格中弹出

时间:2013-06-03 09:27:39

标签: ios uitableview popover

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

enter image description here 我在popover中添加了datepicker(我使用了FPPopover库来执行此操作)以获取“Start Date”的值。到目前为止,我已经完成了,

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

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

1 个答案:

答案 0 :(得分:2)

试试这段代码:

<。>文件中的

 UIButton *btn;
<。>文件中的

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

}