Blios
Blios

Reputation: 729

How to remove a UITextField from a cell, when a row is selected?

I've added a UITextField to cell whenever any row of UITableView is first time selected, now i want to remove that text field when row get selected second time.

Any suggestion or sample code will be appreciated. Thanks!!

Code for adding Text field in cell: in cellForAtIndexPath method

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

            }

in didSelectedRowAtIndexPath

selectedRow = indexPath.row;
[mainTable reloadData];

Upvotes: 0

Views: 134

Answers (3)

Tomasz Wojtkowiak
Tomasz Wojtkowiak

Reputation: 4920

I strongly recommend you to create own UITableViewCell subclass. In this class add this UITextView object and, it the method - (void)setSelected:(BOOL)selected animated:(BOOL)animated show/hide your textField.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [textField setHidden:!textField.hidden]; //this alternately show and hide textField
}

Upvotes: 0

Jörg Kirchhof
Jörg Kirchhof

Reputation: 1640

You can easily get this by giving the model-objects you used to fill the cells a integer variable. This variable gets incremented by 1 each time the user selects that cell.

Then in the – tableView:didDeselectRowAtIndexPath: method (or however it is called in your app) you can make something like that:

if (selectedCellModel.selectCnt == 1) {
    //create the text field
} else if (selectedCellModel.selectCnt == 2) {
    //delete the text field
}

Upvotes: 1

ElasticThoughts
ElasticThoughts

Reputation: 3477

Why not just hide it?

[yourTextField setHidden:YES];

Or if the textField is a subview of the tableview cell just remove it.

[yourTextField removeFromSuperview];

Upvotes: 0

Related Questions