• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c - UITableView滚动后UITextFields重叠

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:39:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个带有 UITextFields 的 UITableView 来制作表单。它看起来很棒——除非你滚动新的 UITextFields 被放在旧的 UITextFields 之上......我不知道为什么!我知道当单元格被重用时(即 dequeueReusableCellWithIdentifier 返回非零),单元格与现有的 UITextField 返回。我找到了一个解决方案来保持标签的唯一性,最好删除任何以前的 UITextField。但我不想那样做。在提交此表单的最后,我想从文本字段中获取所有值(如果我删除它们就不能这样做)..

这里是代码。任何帮助表示赞赏!

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

cell.accessoryType = UITableViewCellAccessoryNone;

UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(155, 15, 130, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];

if (indexPath.row == 0) {
    playerTextField.placeholder = @"Unique ID Sample";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyNext;
}

else if (indexPath.row == 1) {
    playerTextField.placeholder = @"Common Name";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if (indexPath.row == 2) {
    playerTextField.placeholder = @"Scientific Name";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 3) {
    playerTextField.placeholder = @"03/25/1992";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 4) {
    playerTextField.placeholder = @"Male";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 5) {
    playerTextField.placeholder = @"03/01/2012";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 6) {
    playerTextField.placeholder = @"Huntington, WV";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 7) {
    playerTextField.placeholder = @"Huntington, WV";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 8) {
    playerTextField.placeholder = @"Marshall University";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 
else if ([indexPath row] == 9) {
    playerTextField.placeholder = @"Sub Straight";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 10) {
    playerTextField.placeholder = @"Any Light";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 11) {
    playerTextField.placeholder = @"Temperature";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 12) {
    playerTextField.placeholder = @"A Lot";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 13) {
    playerTextField.placeholder = @"All Types";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 14) {
    playerTextField.placeholder = @"Schedule";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 15) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 16) {
    playerTextField.placeholder = @"Ate Someone";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 17) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 18) {
    playerTextField.placeholder = @"The Morgue";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 19) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ([indexPath row] == 20) {
    playerTextField.placeholder = @"MM/DD/YYYY";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
} 

else if ([indexPath row] == 21) {
    playerTextField.placeholder = @"To/ From";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}    

playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;

playerTextField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[playerTextField setEnabled: YES];

    [cell addSubview:playerTextField];

[playerTextField release];

cell.textLabel.text = [self.options objectAtIndex:indexPath.row];


return cell;    
}



Best Answer-推荐答案


您将不得不创建一个自定义 UITableViewCell。这将允许您使用可以直接访问的 UITextField 创建一个 ivar。这样,当单元格出列并重用时,您只需在需要时进行设置。请务必在您的单元子类中覆盖 prepareForReuse: 并重置您添加的组件的所有属性。

上面的代码可以帮助你的另一件事是使用 typedef enum 而不是使用整数来确定它是什么类型的单元格:

typedef enum{
    PlayerFieldId = 0,
    PlayerFieldName,
    PlayerFieldGender
} PlayerFields

然后在您的自定义单元格中,您可以通过上面的枚举和 switch 语句设置单元格的类型:

switch(type){
case PlayerFieldId:
    playerTextField.placeholder = @"Unique ID Sample";
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyNext;
    break;
    /* other fields here */
}

关于objective-c - UITableView滚动后UITextFields重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112320/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap