gpt4 book ai didi

ios - 将 subview 添加到 UITableView 单元格的 View

转载 作者:行者123 更新时间:2023-11-28 21:50:48 26 4
gpt4 key购买 nike

我正在尝试在选择 UITableView 单元格时将 subview (包含 UIDatePicker)添加到该单元格的 View 。但是,当我选择该单元格时, subview 的内容并未显示。

下面是我检查用户是否选择了我要将 subview 添加到的单元格的地方:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == SectionOne && indexPath.row == RowOne && self.datePickerIsShowing == NO){

[self.RowOne addSubview:_datePicker];
[self showDatePickerCell];
} else{
[self hideDatePickerCell];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

此外,下面是我如何创建日期选择器并显示它:

- (void)createDatePicker{
_datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
_datePicker.datePickerMode = UIDatePickerModeTime;
_datePicker.clipsToBounds = YES;
_datePicker.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
}


- (void)showDatePickerCell {
self.datePickerIsShowing = YES;
self.datePicker.hidden = NO;
[self.tableView reloadData];
NSLog(@"Show date picker");
}

下面是我如何将所选行设置为显示日期选择器所需的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat rowHeight = self.tableView.rowHeight;
if (indexPath.section == SectionOne && indexPath.row == RowOne && self.datePickerIsShowing == YES){
rowHeight = kDatePickerCellHeight;
}
else{
rowHeight = self.tableView.rowHeight;
}
return rowHeight;
}

最佳答案

我猜你错过了 UIDatePicker 的框架。您需要使用框架进行初始化。或者您是否在代码中的其他位置设置了框架。

    - (void)createDatePicker{
_datePicker = [[UIDatePicker alloc]initWithFrame:CGRect(0,0,100,20)];
_datePicker.datePickerMode = UIDatePickerModeTime;
_datePicker.clipsToBounds = YES;
_datePicker.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell =[UITableViewCell cellForItemAtIndexPath:indexPath];

if (indexPath.section == SectionOne && indexPath.row == RowOne && self.datePickerIsShowing == NO){

[cell addSubview:_datePicker];
[self showDatePickerCell];
} else{
[self hideDatePickerCell];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

关于ios - 将 subview 添加到 UITableView 单元格的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28416953/

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