开发者

How to generate a UITextField and UIButton in UIScrollView and remove these items dynamically

开发者 https://www.devze.com 2023-04-04 00:59 出处:网络
HI stackoverflow friends I have situation that when i click on a button it will generate dynamically a uitextfield with a remove button. If I don\'开发者_如何转开发t need that text field then i click

HI stackoverflow friends

I have situation that when i click on a button it will generate dynamically a uitextfield with a remove button. If I don'开发者_如何转开发t need that text field then i click the remove button it will goes off.More over the user can generate infinite no: of these pair. So that I think it should be add in a scrollview. Is it possible this. Can anyone know how to do this?

Any help would be appreciable.


In your TableView cellForRowAtIndexpath method:

- (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;
}

You can just have one add button on the navigation bar across top right that would add to NSMutableArray addObject textField to it and reloadTable. Inside cellForRowAtIndexPath get objectAtIndex for that array and add to cell contentView. To delete the cells you can implement swipe to delete for cells...

- (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
     }
}


Posted some code that should help you:

// 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];
0

精彩评论

暂无评论...
验证码 换一张
取 消