objective-c – tableviews单元格在向下滚动后会发生变化
我正在分组的tableview中制作一个表单.在这种形式中,我有UIswitches和textfields.但在向下滚动后,单元格样式正在发生变化.
这是我的cellForRowAtIndex - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; static NSString *MyIdentifier = @"GenericCell"; cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ; } NSString *text = nil; if(indexPath.section == CREDENTIALS_SECTION){ if (indexPath.row == 0) { NSLog(@"tot hier login"); UITextField *login = [[UITextField alloc] initWithFrame:CGRectMake(110,10,185,30)]; login.adjustsFontSizeToFitWidth = YES; login.placeholder = @"example@gmail.com"; login.keyboardType = UIKeyboardTypeEmailAddress; login.returnKeyType = UIReturnKeyNext; login.backgroundColor = [UIColor clearColor]; login.tag = 0; login.delegate = self; [login setEnabled: YES]; [cell addSubview:login]; }else if (indexPath.row == 1){ NSLog(@"tot hier pass"); UITextField *pass = [[UITextField alloc] initWithFrame:CGRectMake(110,30)]; pass.adjustsFontSizeToFitWidth = YES; pass.placeholder = @"Required"; pass.keyboardType = UIKeyboardTypeDefault; pass.returnKeyType = UIReturnKeyDone; pass.secureTextEntry = YES; pass.backgroundColor = [UIColor clearColor]; pass.tag = 0; pass.delegate = self; [cell addSubview:pass]; } if (indexPath.row == 0) { // Email text = @"Email"; } else if(indexPath.row == 1) { text = @"Password"; } }else if(indexPath.section == METHODS_SECTION){ UISwitch *toggleSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(220,100,30)]; toggleSwitch.tag = indexPath.row; [toggleSwitch addTarget:self action:@selector(toggleSwitched:) forControlEvents:UIControlEventValueChanged]; [cell addSubview:toggleSwitch]; if (indexPath.row == 0) { // Web text = @"Web applicatie"; } else if(indexPath.row == 1) { //Mobile text = @"Mobiele applicatie"; } else if(indexPath.row == 2) { //Mail text = @"E-mail"; } }else if(indexPath.section == PHONE_SECTION){ UITextField *phoneText = [[UITextField alloc] initWithFrame:CGRectMake(20,30)]; phoneText.adjustsFontSizeToFitWidth = YES; phoneText.font = [UIFont fontWithName:@"Arial-BoldMT" size:18]; phoneText.keyboardType = UIKeyboardTypeNumberPad; phoneText.delegate = self; phoneText.textColor = [UIColor blackColor]; phoneText.text = _person.phone; [cell addSubview:phoneText]; }else if(indexPath.section == REMARK_SECTION){ UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20,280,260)]; textView.text = _person.remark; textView.delegate = self; textView.font = [UIFont fontWithName:@"Arial" size:15.0]; textView.backgroundColor = [UIColor clearColor]; [cell addSubview:textView]; text = @""; }else if(indexPath.section == BUTTON_SECTION){ cell.backgroundColor = [UIColor redColor]; text = @"test"; } cell.textLabel.text = text; return cell; } 经过一番搜索,我发现有更多人遇到这个问题.问题在于这段代码. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; static NSString *MyIdentifier = @"GenericCell"; cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ; } NSString *text = nil; 但我没有找到解决方案. 亲切的问候! 澄清 好的,所以你在这里看到我的表格截图.下面我有一个红色单元格(保存按钮),当我向下滚动其他单元格获得红色背景.而一些单元格,文本属性正在发生变化. 解决方法
这不是工作.显然你还没有完全理解重用机制是如何工作的.
你是做什么? 当您创建一个新的,在用户开始滚动之前就是开始的情况,然后根据节和行添加一些UIItems.我将解释为什么这实际上并不是一件好事. 然后用户滚动.细胞将从屏幕上消失,然后可以重复使用.然后,您将获取单元格以供重复使用.但很可能这些单元格已经有了额外的UI项目,因为你之前已经以这种方式使用过它们.在以下过程中,您将添加新的UI项目,无论该单元格上是否已有其他UI项目. 你能做什么: >创建自己的自定义表格单元子类.您可能需要的每组附加UI项目的一个子类.这可能是最好的方式.对于每个子类,使用不同的重用标识符(!!!)这是我推荐的!但是,还有其他选择:>您仍然可以使用您的概念,但为每种类型的单元格创建单独类型的重用标识符,其中包含某些类型的附加UI项目.如果是这样,那么请确保仅在代码的if(cell == nil)分支中创建这些UI项并将其添加为子视图.只创建一次然后重复使用它们.单元重用-IDS可以是“电子邮件显示”,“电子邮件输入”,“密码显示”,“密码输入”,“切换”,……>上面的解决方案的方差是,计算行和部分进入重用标识符.例如0和0的“cell-id-0.2”第2行 – 左右.但是你仍然必须确保你真的重复使用其他UI视图,不要每次都重新创建它们当细胞充满数据时.此外,第一部分的布局取决于您是要输入密码和电子邮件还是只显示它们.您仍然需要处理这些变化.>如果cell == nil – 意味着是否重新使用了一个单元格 – 那么首先从您之前添加的每个UI项目中清除它.你可以通过标记你的UIViews来实现这一点 – 比如99 – (任何不同于0应该做的事情)在创建时以及重用枚举所有子视图并删除那些具有标记99的那些.尽管你可以坚持使用代码你已经做了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |