·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> UITableView延伸:点击cell关闭键盘,加载不同cell,监听里面的textfeild内容改变

UITableView延伸:点击cell关闭键盘,加载不同cell,监听里面的textfeild内容改变

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

其实点击cell关闭键盘只要一句话

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView endEditing:YES];
}

 监听里面的textfeild内容改变其实就是当textfeild内容改变的时候将赋值给他的数组的数据改变下就行了,用tag标示不同的textfeild

        newcell.textfeild.delegate = self;
        newcell.textfeild.tag = indexPath.row;
        newcell.textfeild.text = self.arrayWithCell3[indexPath.row];

 在UITextFieldDelegate代理方法中通过tag改变相应的数据

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    self.arrayWithCell3[textField.tag] = [textField.text mutableCopy];
    return YES;
}

 加载不同的cell就是给他注册不同的cell,取的时候也那样取就行了

注册:

    [_tableveiw registerClass:[CustomCell class] forCellReuseIdentifier:@"cellID"];
    [_tableveiw registerClass:[CustomCell1 class] forCellReuseIdentifier:@"cellID1"];
    [_tableveiw registerClass:[CustomCell2 class] forCellReuseIdentifier:@"cellID2"];

 加载:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
        CustomCell *newcell = (CustomCell *)cell;
        newcell.label.text = @"00000";
        
    }else if (indexPath.section == 1){
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellID1"];
    }else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellID2"];
        CustomCell2 *newcell = (CustomCell2 *)cell;
        newcell.textfeild.delegate = self;
        newcell.textfeild.tag = indexPath.row;
        newcell.textfeild.text = self.arrayWithCell3[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

 demo链接:https://github.com/WuJiForFantasy/UITableViewCell.git