Iphone 单击按钮在表视图中添加文本字段
我有一个表格视图,每行都有一个“添加”按钮,单击下面添加的新行。我希望单击该行后,该行中有一个文本字段,用户可以在其中输入texh。您能帮助我如何在添加行中添加文本字段吗。 这是我的密码Iphone 单击按钮在表视图中添加文本字段,iphone,uitableview,addsubview,Iphone,Uitableview,Addsubview,我有一个表格视图,每行都有一个“添加”按钮,单击下面添加的新行。我希望单击该行后,该行中有一个文本字段,用户可以在其中输入texh。您能帮助我如何在添加行中添加文本字段吗。 这是我的密码 -(void)buttonclicked:(UIButton *)sender { UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag]; NSLog(@"clickedCell=%i", btn.tag); [
-(void)buttonclicked:(UIButton *)sender
{
UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
NSLog(@"clickedCell=%i", btn.tag);
[[NSUserDefaults standardUserDefaults]setValue:[NSString stringWithFormat:@"%i", btn.tag] forKey:@"btntag"];
[self.choices insertObject:@"newrow" atIndex:btn.tag+1];
[self.tableView reloadData];
}
和表视图方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.tag = indexPath.row;
button.frame = CGRectMake(280.0, 10, 25, 30.0); // x,y,width,height
[button addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
longPress.delegate = self;
[cell addGestureRecognizer:longPress];
int count = 0;
if(self.editing && indexPath.row != 0)
count = 1;
NSLog([NSString stringWithFormat:@"%i,%i",indexPath.row,(indexPath.row-count)]);
// Set up the cell...
if(indexPath.row == ([_choices count]) && self.editing)
{
cell.textLabel.text = @"ADD";
return cell;
}
NSString *choice = [self.choices objectAtIndex:indexPath.row];
cell.textLabel.text = choice;
return cell;
}
试着这样可能对你有帮助 1.以一个全局变量类型整数为例 2.当您单击特定按钮-(void)buttonclicked:(UIButton*)sender方法将调用此方法时,您将为该位置分配btn.tag值 3.之后,您将在tableview CellForRowatineXpath方法中重新加载表视图,并采取如下条件
if(position==indexpath.row)
{
//add your textfield here
}.
哦,好吧,这不是我的答案,我猜…如果(indexPath.row==([[u选项计数]&&self.editing)条件仍然存在,请检查断点。这个值([[u选项计数]&&self.editing)如何?