我有一个表视图,我在其中添加一个自定义单元格。在自定义单元格中,我有一个文本字段。在该文本字段中,我想从选择器视图中插入值。当用户单击文本字段时,将打开选择器视图。我为此目的有以下代码,但我没有在文本字段中获得价值。代码..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
NSString *identifier = [NSString stringWithFormat:@"%d-%d",indexPath.section,indexPath.row];
cell_object = (Custom_cell2 *)[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell_object == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"Custom_cell2" owner:self options:nil];
}
// Configure the cell...
cell_object.selectionStyle=UITableViewCellSelectionStyleNone;
cell_object.txt_name.backgroundColor=[UIColor clearColor];
cell_object.txt_name.userInteractionEnabled=NO;
NSArray *array = [array_from objectAtIndex:indexPath.section];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell_object.txt_name.text = [NSString stringWithFormat:@"%@",cellValue];
cell_object.txt_time.backgroundColor=[UIColor whiteColor];
cell_object.txt_time.textColor=[UIColor blackColor];
//cell_object.txt_time.userInteractionEnabled=NO;
//cell_object.txt_time.text =@"";
return cell_object;
}
上面的自定义单元格代码。
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
myPicker.hidden=FALSE;
UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320, 44)]; //better code with variables to support view rotation
tool.tintColor=[UIColor blackColor];
UIBarButtonItem *space=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];
UIBarButtonItem *doneButton =[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)] autorelease];
//using default text field delegate method here, here you could call
//myTextField.resignFirstResponder to dismiss the views
[tool setItems:[NSArray arrayWithObjects: space,doneButton,nil] animated:NO];
cell_object.txt_time.inputAccessoryView = tool;
[myPicker addSubview:tool];
//you can -release your doneButton and myToolbar if you like
[[[UIApplication sharedApplication] keyWindow] addSubview:myPicker];
return NO;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
if (pickerView == myPicker)
{
[cell_object.txt_time setText:[NSString stringWithFormat:@"%@",[array_time objectAtIndex: [pickerView selectedRowInComponent:0]]]];
[table_routine reloadData];
}
}
当我点击cell_object.txt_time然后出现一个选择器视图,但它没有从文本提交的选择器中插入值。
这段代码中有什么错误?
提前致谢...
答案 0 :(得分:0)
[pickerView selectRow:1 inComponent:0 animated:NO]; textField.text = [array_name objectAtIndex:[pickerView selectedRowInComponent:0]];
您可以尝试使用此代码从pickerview选择中添加文本...
答案 1 :(得分:0)
当您要将myPicker添加为subView时,请确认您已设置并在代码中添加以下行。
myPicker.delegate = self;
答案 2 :(得分:0)
实际上你不能像这样设置UITableViewCell textlLabel
的值。对于tableView中的任何更改,您必须重新加载它或单独重新加载Cell。
解决方案是创建一个数组并将pickerView
值存储在该数组中。
然后在cellForRowAtIndexPath
:
cell_object.txt_time setText: [array objectAtIndex: indexPath];
试试这个..