gpt4 book ai didi

ios - 自定义表格 View 单元格内的弹出窗口

转载 作者:行者123 更新时间:2023-11-28 22:33:02 25 4
gpt4 key购买 nike

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

enter image description here我在弹出框内添加了日期选择器(我使用 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];
}

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

最佳答案

试试这段代码:

在.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];

}

关于ios - 自定义表格 View 单元格内的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16893642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com