我在视图控制器内部有一个实现-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
的UIPickerView
我用UITableViewCell
来利用accessoryType
,UITableViewCellAccessoryCheckmark
。我在措辞整个视图控制器的过程时遇到问题,所以我将使用代码:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UITableViewCell *cell = (UITableViewCell *)view;
if( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[cell setBackgroundColor:[UIColor clearColor]];
if([[[self.currentData objectAtIndex:row] valueForKey:@"uuid"] isEqualToString:self.currentProfile] || [[[self.currentData objectAtIndex:row] valueForKey:@"description"] isEqualToString:self.currentSelectedRow]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
cell.tag = 10;
self.Switch.on = TRUE;
}
[cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)];
cell.textLabel.text = [[self.currentData objectAtIndex:row] valueForKey:@"description"];
return cell;
}
UIPickerView
上的一个UITapGestureRecognizer
这样称呼:
-(void)pickerTapped:(UITapGestureRecognizer *) sender{
CGPoint touchPoint = [sender locationInView:self.view];
CGRect frame = self.pick.frame;
CGRect selectFrame = CGRectInset(frame, 0.0, self.pick.bounds.size.height * 0.85 / 2.0);
//if the tap gesture is onto of the picker view, & the enabled switch is on
if(CGRectContainsPoint(selectFrame, touchPoint) && self.Switch.on){
//clear all cells of check mark
for(int i = 0; i < [self.pick numberOfRowsInComponent:0]; i++){
UITableViewCell *tmpCell = (UITableViewCell *) [self.pick viewForRow:i forComponent:0];
tmpCell.accessoryView = UITableViewCellAccessoryNone;
}
[self.pick reloadAllComponents];
UITableViewCell *cell = (UITableViewCell *)[self.pick viewForRow:[self.pick selectedRowInComponent:0] forComponent:0];
self.currentSelectedRow = cell.textLabel.text;
if(cell.tag == 10){
//user tapped on already ticked cell, wishes to untick
cell.accessoryType = UITableViewCellAccessoryNone;
cell.tag = 0;
self.currentSelectedRow = nil;
} else{
//user has selected un-ticked row, wishes to tick
//clear all cells of tags
for(int i = 0; i < [self.pick numberOfRowsInComponent:0]; i++){
UITableViewCell *tmpCell = (UITableViewCell *) [self.pick viewForRow:i forComponent:0];
tmpCell.tag = 0;
}
//assign new ticked cell & update currentProfile
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.tag = 10;
self.currentProfile = [[self.currentData objectAtIndex:[self.pick selectedRowInComponent:0]] valueForKey:@"uuid"];
}
}
//[self.pick reloadAllComponents]; <-- this is a key part. take note.
}
问题来了:
这样做的总体目标是用户可以不勾选任何行,或者一次勾选一行。如果视图控制器加载并设置了当前配置文件,即现有数据的表示。然后勾选相关行。
简单。然而。。。
选取器底部的 [self.pick reloadAllComponents]
Tapped: 函数在取消注释时,会导致选取器视图能够处理未勾选或仅选中 1 行的行。 但无法处理行取消选择/取消勾选。
被注释时,选取器能够处理未选择的行,但对单个选定的行的行为很奇怪。 我可以很好地选择和取消选择一行,但是当我选择另一行时,AnnexoryType 会更新为选取器视图的"选择框"部分中的单元格(中间两行), 但是当我在选择器上滚动时,有问题的单元格显示好像附件尚未更新。澄清一下,当我将单元格滚动回"选择框"时,它看起来很好。
尴尬地在发布后不久就解决了这个问题。
草皮法和所有这些
在pickerTapped:
,其中currentSelectedRow = nil
,self.currentProfile
也应该设置为nil。reloadAllComponents
应未被引用