在我的应用程序中我实现了如下功能,Tabelview单元格包含两个按钮,单击它显示选择器视图,当我选择选择器值时,它不反映在uitabelview对应的cell.label上,这里是代码。
Tableview单元格
if(tableView.tag==25)
{
static NSString *simpleTableIdentifier = @"ordercel";
ordercel *cell = (ordercel *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ordercel" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.pimg.image=[arr objectAtIndex:indexPath.row];
cell.sizlb.text=@"45";
cell.qtylb.text=[qtyary objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = indexPath.row+2000;
[button addTarget:self action:@selector(BtnPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor=[UIColor clearColor];
button.frame = CGRectMake(130, 20 , 70, 35);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.tag = indexPath.row+4000;
[button1 addTarget:self action:@selector(sizPressed:) forControlEvents:UIControlEventTouchUpInside];
button1.backgroundColor=[UIColor clearColor];
button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button1.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button1.frame = CGRectMake(230, 20 , 70, 35);
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:button1];
return cell;
}
按钮方法
-(void)BtnPressed:(UIButton*)sender
{
[self createActionSheet];
pickerType = @"qtypicker";
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.tag=sender.tag-2000;
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:chPicker];
}
选择器视图代码
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([pickerType isEqualToString:@"qtypicker"])
{
counti = [array count];
}
if ([pickerType isEqualToString:@"sizepicker"])
{
counti = [array1 count];
}
return counti;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *string;
if ([pickerType isEqualToString:@"qtypicker"])
{
string = [array objectAtIndex:row];
}
if ([pickerType isEqualToString:@"sizepicker"])
{
string = [array1 objectAtIndex:row];
}
return string;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
UITableViewCell *owningCell = (UITableViewCell*)[thePickerView superview];
NSIndexPath *pathToCell = [tabvu indexPathForCell:owningCell];
int index1=thePickerView.tag;//= thePickerView.tag-2000;
if ([pickerType isEqualToString:@"qtypicker"])
{
UILabel *obj=((UILabel *)[self.view viewWithTag:index1+1000]);
obj.text = [array objectAtIndex:row];
[qtyary addObject:obj.text];
[tabvu reloadData];
}
if ([pickerType isEqualToString:@"sizepicker"])
{
}
}
如何在所选单元格上获取所选的选择器值。我应该在代码中做出哪些更改?请帮我解决一下
答案 0 :(得分:0)
您的代码很难阅读。但这应该是这样的。
pickerView:(UIPickerView *)thePickerView didSelectRow
方法更新数组对象[qtyary objectAtIndex:indexPath.row]
。然后拨打[tableView reloadData];
这将更新您的cell.qtylb.text
。对于其他标签,您还需要一个要更新的阵列。我希望这有效。
答案 1 :(得分:0)
尝试在didSelectRow中更改行
UITableViewCell *owningCell = (UITableViewCell*)[thePickerView superview];
到
ordercel *owningCell = (ordercel*)[thePickerView superview];
如果你有用于放置按钮的自定义单元格。那么你应该使用自定义单元格的单元格实例而不是UITableViewCell。
尝试在这种情况下打印日志
if ([pickerType isEqualToString:@"qtypicker"])
{
UILabel *obj=((UILabel *)[self.view viewWithTag:index1+1000]);
obj.text = [array objectAtIndex:row];
[qtyary addObject:obj.text];
[tabvu reloadData];
}
它返回的是什么对象。将NSLog应用于obj。