Objective c 选择行时,如何从单元格中删除UITextField?
每当第一次选择Objective c 选择行时,如何从单元格中删除UITextField?,objective-c,uitableview,ios5,Objective C,Uitableview,Ios5,每当第一次选择UITableView的任何一行时,我都会在单元格中添加一个UITextField,现在我想在第二次选择行时删除该文本字段 任何建议或样本代码将不胜感激。 谢谢 用于在单元格中添加文本字段的代码:在cellForAtIndexPath方法中 if (indexPath.row == selectedRow) { numOfBottles =[[UITextField alloc] initWithFrame:CGRec
UITableView
的任何一行时,我都会在单元格中添加一个UITextField
,现在我想在第二次选择行时删除该文本字段
任何建议或样本代码将不胜感激。
谢谢
用于在单元格中添加文本字段的代码:在cellForAtIndexPath方法中
if (indexPath.row == selectedRow)
{
numOfBottles =[[UITextField alloc] initWithFrame:CGRectMake(240,9.0f,50, 25)];
numOfBottles.tag = indexPath.row;
[numOfBottles setBorderStyle:UITextBorderStyleNone];
[numOfBottles setBackgroundColor:[UIColor clearColor]];
[numOfBottles setTextColor:[UIColor whiteColor]];
[numOfBottles setTextAlignment:UITextAlignmentLeft];
[numOfBottles setBackground:[UIImage imageNamed:@"blue_dropdown_normal.png"]];
[numOfBottles setFont:[UIFont systemFontOfSize:16.0f]];
[numOfBottles setDelegate:self];
NSString* quantity = [[NSString alloc] initWithString:[subtotalObj.qtyArray objectAtIndex:(indexPath.row - 1)]];
[numOfBottles setText:quantity];
[numOfBottles setTextAlignment:UITextAlignmentCenter];
[numOfBottles setBackgroundColor:[UIColor whiteColor]];
numOfBottles.keyboardType = UIKeyboardTypeDefault;
numOfBottles.tag = indexPath.row;
[cell.contentView addSubview:numOfBottles];
[numOfBottles release];
}
在中选择了OWATINDEXPATH
selectedRow = indexPath.row;
[mainTable reloadData];
为什么不把它藏起来
[yourTextField setHidden:YES];
或者,如果textField是tableview单元格的子视图,只需将其删除即可
[yourTextField removeFromSuperview];
通过为用于填充单元格的模型对象提供一个整数变量,可以轻松实现这一点。每次用户选择该单元格时,该变量将递增1 然后在–tableView:didDeceloseRowatineXpath:method(或在应用程序中以何种方式调用)中,您可以制作如下内容:
if (selectedCellModel.selectCnt == 1) {
//create the text field
} else if (selectedCellModel.selectCnt == 2) {
//delete the text field
}
我强烈建议您创建自己的UITableViewCell子类。 在这个类中,添加这个UITextView对象,并使用方法
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
显示/隐藏文本字段
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
[textField setHidden:!textField.hidden]; //this alternately show and hide textField
}
你能试着更好地解释一下你想要达到的目标吗?告诉我们你以前尝试过什么,例如,你在单元格中添加
UITextField
。@TomaszWojtkowiak,我已经编辑了我的问题,现在如果第二次选中该行,我必须删除第一次选中该行时添加的文本字段。@timjver,是的,UITableView
中的一行,我编辑了我的问题。我的TL给出了不使用自定义UITableView单元格的说明,现在我能做什么?你可以在didselectrowatinexpath
中这样做,你可以使用cellforrowatinexpath
来获取你的单元格,但在我看来,更好的选择是将此代码封装在专用对象中。