尝试将内联UIDatePicker添加到UITableViewCell [英] Attempting to add an inline UIDatePicker to a UITableViewCell

查看:107
本文介绍了尝试将内联UIDatePicker添加到UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在TableView单元格中创建内联DatePicker,类似于这个这个 SO线程。

I'm attempting to create an inline DatePicker inside a TableView cell, similar to this and this SO thread.

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

I create the date picker using the method below, which is called when the view is loaded:

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

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

I then check to see if the bottom row of the third section in my table view is selected. If it is, and the date picker already isn't showing, then I call the method to show the date picker's cell:

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

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

Below are the methods to show the date picker's cell:

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

...并隐藏单元格:

...and hide the cell:

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

以下是确定表视图是否需要添加额外单元格的方法显示UIDatePicker。

Below is the method to determine if the table view needs to add an extra cell to display the UIDatePicker.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)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;
        }
    }

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

Finally, the method below is what is supposed to determine the height of the date picker's cell:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)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. Does anyone have any suggestions on what I'm doing wrong?

推荐答案

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

You're adding the datePicker to the tableView, not to a cell within the tableView.

从showDatePickerCell中删除此行:

Remove this line from showDatePickerCell:

[self.tableView addSubview:_datePicker];

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

Then add it (or unhide it) within the proper cell within cellForItemAtIndexPath.

(类似于:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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;
}

这篇关于尝试将内联UIDatePicker添加到UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆