Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在UIScrollView中生成UITextField和UIButton并动态删除这些项_Iphone_Objective C_Ios4 - Fatal编程技术网

Iphone 如何在UIScrollView中生成UITextField和UIButton并动态删除这些项

Iphone 如何在UIScrollView中生成UITextField和UIButton并动态删除这些项,iphone,objective-c,ios4,Iphone,Objective C,Ios4,嗨,朋友们 我的情况是,当我点击一个按钮时,它会动态生成一个带有删除按钮的uitextfield。如果我不需要这个文本字段,那么我点击删除按钮,它就会关闭。更多信息,用户可以生成这些对的无限编号。所以我认为应该在scrollview中添加它。这有可能吗。有人知道怎么做吗 任何帮助都是值得的 发布了一些代码,可以帮助您: // alloc + init + set appropriate size and origin UIScrollView *scroll; UITextField *fiel

嗨,朋友们


我的情况是,当我点击一个按钮时,它会动态生成一个带有删除按钮的uitextfield。如果我不需要这个文本字段,那么我点击删除按钮,它就会关闭。更多信息,用户可以生成这些对的无限编号。所以我认为应该在scrollview中添加它。这有可能吗。有人知道怎么做吗


任何帮助都是值得的

发布了一些代码,可以帮助您:

// alloc + init + set appropriate size and origin
UIScrollView *scroll;
UITextField *field;
UIButton *button;
// add subview
[scroll addSubview:field];
[scroll addSubview:button];
// remove subview
[field removeFromSuperview];
[button removeFromSuperview];
// memory managment
[field release];
[button release];

在TableView cellForRowAtIndexpath方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UITextField* textField;
    if (cell == nil) 
    { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      textField = [[UITextField alloc] initWithFrame:CGRectMake(10,20,30,30)];
      textField.tag = 10; 
      [[cell contentView] addSubView:textField];
      [textField release];
    }
    else
    {
      textField = (UITextField *)[cell.contentView viewWithTag:10];
    }

    // Do something with TextField
    return cell;
}
您只需在导航栏右上角有一个add按钮,它将添加到NSMutableArray addObject textField并重新加载表。在cellForRowAtIndexPath内部,获取该数组的objectAtIndex并添加到CellContentView。要删除单元格,您可以对单元格执行“滑动删除…”

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     // If row is deleted, remove it from the Array.
     if (editingStyle == UITableViewCellEditingStyleDelete)
     {
      // delete your data item here
     }
}

“更多信息用户可以生成这些对的无限编号”->,因此我建议使用UITableView而不是ScrollView。TableView可以重用单元格,而scroll view没有单元格重用的概念。你是对的,但是发布的代码太大了。所以我只是展示了使用scroll View的方法。这段代码在滚动后会在每个单元格中创建大量的文本字段,因为每次重复使用单元格时都会创建一个新的文本字段。Textfield应在if语句中使用标记创建,并在以后通过
viewWithTag:
访问。