Objective c 向下滚动后,TableView单元格正在更改
我正在分组的tableview中创建表单。在这个表单中,我有UI开关和文本字段。但向下滚动后,单元格样式正在更改 这是我的手机号码Objective c 向下滚动后,TableView单元格正在更改,objective-c,ios,xcode,uitableview,Objective C,Ios,Xcode,Uitableview,我正在分组的tableview中创建表单。在这个表单中,我有UI开关和文本字段。但向下滚动后,单元格样式正在更改 这是我的手机号码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; static NSString *MyIdentifier = @"GenericC
- (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, 10, 185, 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, 10, 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, 10, 185, 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, 10, 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;
但我没有找到解决办法。
希望任何人都能帮忙
亲切的问候
澄清
好的,这里是我表格的截图。下面我有一个红色的单元格(保存按钮),当我向下滚动时,其他单元格会显示红色背景。一些单元格、文本属性正在更改。
在重新使用单元之前,请尝试从单元中删除所有子视图。请尝试以下代码:
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
else
{
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
在单元格上添加子视图之前,请删除所有子视图
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]autorelease];
}
else
{
//To remove the subview of cell.
for (UIView *vwSubviews in [cell.contentView subviews])
{
[vwSubviews removeFromSuperview];
}
}
它可能会解决您的问题。实际上,您这里有一些不好的代码 在方法上
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
除非它不在if(cell==nil)中,否则不应初始化并使用任何
-(void)addSubview:(UIView*)view
为什么?
这些单元格是从tableview重用的视图。因此,如果您添加了一些子视图,下次在重用单元时,它将添加更多的子视图。只是它们重叠,可能导致内存泄漏
不要忘记细胞是可重复使用的。所以,
如果我有以下代码,除非我没有在其他地方设置文本。希望所有单元格的文本标签“this is a text”(这是文本)中都有文本。因为它们是可重用的
if (someChangingBool) {
cell.textLabel.text = @"this is a text";
}
所以我需要一个else,如果它把文本设置成别的
对于。如果您还需要保持UIControl状态,则使用
static NSString *MyIdentifier = [NSString stringWithFormat:@"GenericCell%d",indexPath.row];
它将始终返回您唯一的表行,您可以根据需要使用它。这是行不通的。很显然,您还没有完全理解重用机制是如何工作的 你是做什么的? 首先,获取一个要重复使用的单元格。如果你得到了一个,目前还可以,但问题会在以后出现。如果你没有得到一个,那么你就创建一个新的 当您创建了一个新的项目(在用户开始滚动之前的开始阶段就是这种情况)时,您可以根据节和行添加一些UIItems。我将解释为什么这实际上不是一件明智的事情 然后用户滚动。单元格将从屏幕上显示,然后可供重复使用。然后,您将获取单元格以供重复使用。但很可能这些单元格上已经有了额外的UI项,因为您以前曾以这种方式使用过它们。在下面的过程中,您将添加新的UI项,而不管该单元格上是否已有其他UI项 你能做什么:
If(cell==nil)
分支中。只创建一次,然后重新使用。单元重用ID可以是“电子邮件显示”、“电子邮件输入”、“密码显示”、“密码输入”、“切换”- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GenericCell"] ;
//some more code
return cell;
}
这将从tableview中删除可重用性,但由于它是一个有限的设置视图,所以可以。我仍然建议从Hermann Klecker的解决方案中选择1或2。你能解释一下你的意思吗?但向下滚动后,单元格样式正在改变。?谢谢。请说得具体一点。到底是什么以一种意想不到的方式发生了变化。你期待什么?会发生什么?我用截图编辑了我的问题,你能试试下面我的答案吗?你是对的Stef Geelen。问题在于dequeueReusableCellWithIdentifier重用单元的部分。如果您在重用之前从该单元中删除所有子视图,则此问题将得到解决。它将适用于您。但它将真正消除所有子视图,包括标准文本标签。但至少在某些情况下,您会使用标准文本标签。因此,如果你想应用这个解决方案,那么你就必须在需要的情况下添加一个常规文本标签。@HermannKlecker Oké,我应该把上面的代码放在哪里?我应该用cell.textlab.text=text来结束这件事吗?首先感谢你给出的详细答案。但是你推荐什么解决方案呢?最好的实践方法是什么?解决方案4对您来说是最简单的,因为这样您就不必更改代码的多个部分。建议使用1号解决方案。一开始它的工作量确实最大,但这将通过为您提供最多可维护和可重用的代码来获得回报。这是目前最多的工作,但对未来最少的工作。因此,如果我对每个部分的理解正确,创建一个新的UICustomTableViewCell?@HermannKlec