原型tableviewcell里面的UIPickerView没有选择指标

时间:2016-11-19 18:35:30

标签: ios objective-c uitableview uipickerview

我有一个表视图控制器,它有多个原型单元用于不同的输入控件,如UISwitch,UITextField和UIPickerView,当加载视图时,我确定需要哪个控件,只显示一个原型单元。

但对于带有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];

为什么选择指示器没有显示?

1 个答案:

答案 0 :(得分:1)

我有类似的问题。我注意到每次我刷到不同的标签时,这个问题都会消失。这意味着当tableview(单元格)出现在视图中时,会发生一些有趣的事情。

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

以下是您可以尝试的内容:

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)])