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

标签 ios objective-c uitableview

我正在尝试在选择 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/

相关文章:

iphone - 存在性能问题的 UITableView

ios - 如何在 Swift 3 中计算或自动调整 UiTableView 的高度

ios - 允许最后一个 UITableViewCell 滚动到第一行

ios - 逐渐改变状态栏颜色

ios - 使用分配是否会减少使用的内存量?

ios设置cornerRadius与尾随空格,圆角半径不起作用

ios - -[UITableView layoutSublayersOfLayer :] on iOS 7 断言失败

ios - 按特定顺序排列单元格

ios - 如果我快速选择其中一项,如何删除所有数组

iphone - ALAssetsLibrary enumerateGroupsWithTypes 不返回任何数据