• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 尝试将内联 UIDatePicker 添加到 UITableViewCell

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:16:13 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试在 TableView 单元格中创建一个内联 DatePicker,类似于 thisthis所以线程。

我使用下面的方法创建日期选择器,在加载 View 时调用:

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

    NSLog(@"date picker created");
}

然后我检查我的表格 View 中第三部分的底行是否被选中。如果是,并且日期选择器已经没有显示,那么我调用该方法来显示日期选择器的单元格:

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    if (indexPath.row == HourTimeZoneRow && self.datePickerIsShowing == NO)
    {
        NSLog(@"Time of day section");
        [self showDatePickerCell];
    } else
    {
        [self hideDatePickerCell];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

以下是显示日期选择器单元格的方法:

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

...并隐藏单元格:

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

下面是判断table view是否需要额外添加cell来显示UIDatePicker的方法。

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
    {
        switch (section) {
            case TableSectionOne:
                return TotalRowsSection1;
                break;
            case TableSectionTwo:
                return TotalRedZoneRows;
                break;
            case TableSectionThree:
                if (self.datePickerIsShowing == YES) {
                    return TableSectionThree + 1;
                }
                else {
                    return TableSectionThree;
                }
                break;
            default:
                return 0;
                break;
        }
    }

最后,下面的方法应该是确定日期选择器单元格的高度:

- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath {

    CGFloat rowHeight = self.tableView.rowHeight;

    if (indexPath.row == HourTimeZoneRow){
        rowHeight = 164;
    }
    else {
        rowHeight = self.tableView.rowHeight;
    }
    return rowHeight;
}

However, what's happening is that when the last row of the third section is selected, the date picker is displayed in the first row of the first section.有人对我做错了什么有任何建议吗?



Best Answer-推荐答案


您将 datePicker 添加到 tableView,而不是 tableView 中的单元格。

从 showDatePickerCell 中删除这一行:

[self.tableView addSubview:_datePicker];

然后在 cellForItemAtIndexPath 的适当单元格中添加(或取消隐藏)。

(类似:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    if (indexPath.row == HourTimeZoneRow) {
        // show the datePicker here
    }

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

)

关于ios - 尝试将内联 UIDatePicker 添加到 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28353639/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap