gpt4 book ai didi

ios - 原型(prototype) tableviewcell 内的 UIPickerView 没有选择指示器

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:14 24 4
gpt4 key购买 nike

我有一个 TableView Controller ,它有多个原型(prototype)单元格用于不同的输入控件,如 UISwitch、UITextField 和 UIPickerView,当加载 View 时,我确定需要哪个控件并只显示一个原型(prototype)单元格。

但是对于带有 UIPickerView 的单元格,我无法让选择指示器出现并且 pickerview 最终看起来像这样:

Screenshot of a UIPickerView with no selection indicator

在我的 tableView:cellForRowAtIndexPath: 方法中,我明确启用了选择指示器:

UIPickerView * pickerView = [cell viewWithTag:InputTagPicker];
pickerView.dataSource = self;
pickerView.delegate = self;
[pickerView selectRow:self.pickerInitialIndex inComponent:0 animated:YES];
[pickerView setShowsSelectionIndicator:YES];

为什么选择指示器不显示?

最佳答案

我遇到了类似的问题。我注意到每次我滑动到不同的选项卡时,这个问题都会消失。这意味着当 tableview(cell) 出现在 View 中时发生了一些有趣的事情。

由于 UITableViewCell 没有 viewWillAppear() 方法,我设法通过从单元格的 subview 列表中强制删除 pickerView 然后再将其添加回来来解决这个问题。这模拟了在将pickerView作为 subview 添加到单元格之前设置选择的过程,并修复了问题。

以下是您可以尝试的方法:

UIPickerView * pickerView = [cell viewWithTag:InputTagPicker];
pickerView.dataSource = self;
pickerView.delegate = self;
[pickerView selectRow:self.pickerInitialIndex inComponent:0 animated:YES];
[pickerView setShowsSelectionIndicator:YES];

// hack to make sure the selection indicator shows
// first remove the picker
[pickerView removeFromSuperview];

// then add it back
[cell.contentView addSubview:pickerView];

但是,这会创建粘在单元格左边缘的选择器,因此您需要以编程方式设置约束

// finally set the constraints to make sure it fits horizontally centered
//Trailing
NSLayoutConstraint *trailing =[NSLayoutConstraint
constraintWithItem:pickerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.f];

//Leading
NSLayoutConstraint *leading = [NSLayoutConstraint
constraintWithItem:pickerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.f];

//Center Y
NSLayoutConstraint *centerY = [NSLayoutConstraint
constraintWithItem:pickerView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.f];

[cell.contentView addConstraints:trailing];
[cell.contentView addConstraints:leading];
[cell.contentView addConstraints:centerY];

这是一个 Swift 4 版本:

// first remove the picker
pickerView?.removeFromSuperview()

// then add it back
cell.contentView.addSubview(pickerView!)

// and finally set the constraints to make sure it fits horizontally centered
cell.contentView.addConstraints([NSLayoutConstraint.init(item: pickerView as Any, attribute: .centerY, relatedBy: .equal, toItem: cell.contentView, attribute: .centerY, multiplier: 1, constant: 0),
NSLayoutConstraint.init(item: pickerView as Any, attribute: .trailing, relatedBy: .equal, toItem: cell.contentView, attribute: .trailing, multiplier: 1, constant: 0),
NSLayoutConstraint.init(item: pickerView as Any, attribute: .leading, relatedBy: .equal, toItem: cell.contentView, attribute: .leading, multiplier: 1, constant: 0)])

关于ios - 原型(prototype) tableviewcell 内的 UIPickerView 没有选择指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40696536/

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