如何在Tableview中使用UiTextFiled

本文介绍了如何在TableView中添加并管理UITextField,包括添加事件、通过tag值获取内容,以及编辑结束后的键盘隐藏策略。同时,还讲解了如何在数据不足时去除多余的表格线。
摘要由CSDN通过智能技术生成
  1. 在Tableview 中添加UItextField控件,并且给每一UItextfield添加事件,并且将方法设置在编辑结束的时候触发,这样就可以捕捉到填写在UItextfield上的内容
#pragma mark - 初始化tableview的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    static NSString  *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = [self.xinxi objectAtIndex:row];  //给每一行添加标题
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    CGRect textFieldRect = CGRectMake(0.0, 0.0f, 215.0f, 31.0f);
    UITextField *theTextField = [[UITextField alloc] initWithFrame:textFieldRect];
    theTextField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    theTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    theTextField.returnKeyType = UIReturnKeyDone;
    theTextField.clearButtonMode = YES;
    theTextField.tag = row;
    theTextField.delegate = self;   //将代理指向自己

    //给每一个UITextField添加事件,使得编辑结束时调用textFieldWithText方法
    [theTextField addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];

        switch (row) {
            case 0:
                theTextField.placeholder = @"请填写第一行";
                theTextField.returnKeyType = UIReturnKeyDefault;
                break;
            case 1:
                theTextField.placeholder = @"请填写第二行";
                theTextField.returnKeyType = UIReturnKeyDefault;
                break;
            case 2:
                theTextField.placeholder = @"请填写第三行";
                theTextField.returnKeyType = UIReturnKeyDefault;
                break;
            default:
                break;
        }
    cell.accessoryView = theTextField;
        return cell;
}

#pragma mark - 获取每个UITextField在编辑结束的值
- (void)textFieldWithText:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
           _theOneRow = textField.text;
            break;
        case 1:
           _theTwoRow = textField.text;
            break;
        case 2:
           _theThreeRow = textField.text;
            break;
        default:
            break;
    }
}

当然除了给每一个UItextfield设置事件之外,还可以给每一个UItextfield设置tag值,然后通过tag值找到那个UItextfield控件,并把它text的值读取出来的,如果是一进入页面就要显示内容,也可以通过tag值进行设置。

2.编辑结束后如何隐藏键盘。隐藏键盘有很多方法,比如点击不可填写内容的区域来隐藏键盘,也可以使用键盘上的return按钮进行键盘的隐藏。

#pragma mark - 将键盘收起方法一 (编辑结束点击其他地方收起)
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - 将键盘收起方法二(点击键盘的return按钮收起键盘)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

3.如何把多余的表格线去除。在初始化了一个表格之后,如果填充的数据较少,只有一两条的话,那么页面会有很多的分割线,要怎么样把这些线给去除呢?其实只要一步就可以了

 self.table.tableFooterView = [[UITableView alloc] init];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值