UITextField in a UITableViewCell

本文介绍了如何在表格视图单元格中实现文本字段,并详细解释了如何通过按钮点击获取文本字段的值。通过实例演示了在不同场景下设置文本字段属性,包括设置占位符、键盘类型、返回键类型和加密方式。

http://stackoverflow.com/questions/409259/having-a-uitextfield-in-a-uitableviewcell

http://stackoverflow.com/questions/7034433/how-to-get-uitextfield-values-when-button-is-clicked

 

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableLogin dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if ([indexPath section] == 0) {
            UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 165, 30)];
            textfield.adjustsFontSizeToFitWidth = YES;
            textfield.textColor = [UIColor blackColor];
            textfield.backgroundColor = [UIColor whiteColor];
            textfield.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
            textfield.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
            textfield.textAlignment = UITextAlignmentLeft;
            textfield.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
            textfield.delegate = self;

            if ([indexPath row] == 0) {
                textfield.tag = 0;
                textfield.placeholder = @"your username";
                textfield.keyboardType = UIKeyboardTypeDefault;
                textfield.returnKeyType = UIReturnKeyNext;
            }
            else {
                textfield.tag = 1;
                textfield.placeholder = @"required";
                textfield.keyboardType = UIKeyboardTypeDefault;
                textfield.returnKeyType = UIReturnKeyDone;
                textfield.secureTextEntry = YES;
            }

            [textfield setEnabled:YES];
            [cell addSubview:textfield];
            [textfield release];
        }
    }
    if ([indexPath section] == 0) { // Email & Password Section
        if ([indexPath row] == 0) { // Email
            cell.textLabel.text = @"Email";
        }
        else {
            cell.textLabel.text = @"Password";
        }
    }
    else { // Login button section
        cell.textLabel.text = @"Log in";
    }

    return cell;    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            self.username = textField.text;
            break;
        case 1:
            self.password = textField.text;
            break;
    }
    return true;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值