简体   繁体   中英

how to give textfield on cell in iphone

I want to give textfield on tableviewcell I writen some code for that I can see textfield on cell but when i go next cell then my value is disapper and how to give on cell keyboard disapper please check this code which I writen on cell, please help me on how to disapper keyboard from cell.

I want to give textfield only at one row not at all row.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([indexPath row] == 0 && [indexPath section] == 0)
    {
    cell.textLabel.text=@"Tags";
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
        cell.accessoryView = textField;

    }

    if ([indexPath row] == 1 && [indexPath section] == 0)
    {
         cell.textLabel.text=@"Notes";
        cell.detailTextLabel.text=app.Notes;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

To dismiss keyboard your class should conform UITextFieldDelegate protocol and you should implement this method:

- (BOOL)textFieldShouldReturn:(UITextField *)txtField {
    [txtField resignFirstResponder];
    return YES;
}

Your value disappears when your UITableView decides to requery your cell.

Look what you do then: You create a new cell with a new UITextField which - because being new - does not include any value. Your old cell (and the value with it) is lost then.

For hiding the keyboard 'beryllium' & 'Stas' are correct. To hold the text of the textField, store the text in textFieldDidEndEditing method, like this:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    nameString = textField.text;
}

In your cellForRowAtIndexPath method set the text of textField as textField.text = nameString after initializing the textField.

you can do one thing also set tag for this. and add it on cell direct without accessoryView. Use [cell addSubview:textField]; to add on cell. later access it with the help of tag .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM