tableviews单元格在向下滚动后会发生变化 [英] tableviews cells are changing after scrolling down

查看:89
本文介绍了tableviews单元格在向下滚动后会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在分组的tableview中制作表单。在这种形式中,我有UIswitches和textfields。但是在向下滚动之后,单元格样式正在发生变化。

I am making a form within a grouped tableview. In this form I have UIswitches and textfields. But after scrolling down, the cells styles are changing.

这是我的cellForRowAtIndex

Here is my 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, 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;
}

经过一番搜索,我发现有更多人遇到这个问题。并且问题在于这段代码。

After some searching I found that more people are having this problem. And that the problem lays in this piece of code.

- (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;

但我找不到解决方案。
希望有人可以提供帮助!

But I don't find a solution for it. Hope anybody can help!

亲切的问候!

澄清

Oké所以在这里你会看到我的表格截图。下面我有一个红色单元格(保存按钮),当我向下滚动其他单元格正在获得红色背景。而一些单元格,文本属性正在发生变化。

Oké so here you see a screenshot of my form. below I have a red cell (save button) when I scroll down other cells are getting a red background. And some cells, text property's are changing.

推荐答案

这不是工作的锣。显然你还没有完全理解重用机制是如何工作的。

That is not gong to work. Aparently you did not yet fully understand how the re-use mechanism works.

你做什么?
首先,您获取要重复使用的单元格。如果你到目前为止得到一个 - 但问题来得更晚。如果你没有得到一个,那么你创建一个新的。

What do you do? First you fetch a cell to be re-used. If you get one -fine so far but the problem comes later. If you don't get one then you create a new one.

当你创建一个新的,在用户开始滚动之前就是这种情况,然后根据section和row添加一些UIItems。我将解释为什么这实际上并不是一件好事。

When you have created a new one, which is the case at start before the user begins scrolling, then you add some UIItems depending on section and row. I will explain why this is not actually a smart thing to do.

然后用户滚动。细胞将从屏幕上消失,然后可供重复使用。然后,您将获取单元格以供重复使用。但很可能这些单元格已经有了额外的UI项目,因为你之前已经以这种方式使用过它们。在以下过程中,您将添加新的UI项目,无论该单元格上是否已有其他UI项目。

Then the user scrolls. Cells will dissappear from screen and then made available for re-use. Then you will fetch the cells for re-use. But it may well happen that those cells already have additional UI-Items on them because you have used them before in that way. In the following process you will add new UI Items regardless whether there are already additional UI-Items on that very cell.

你能做什么:


  1. 创建你自己的自定义表细胞亚类。您可能需要的每组附加UI项目的一个子类。这可能是最好的方式。对于每个子类,使用不同的重用标识符(!!!)
    这是我推荐的!
    然而,有其他选择:

  1. Create your own custom table cell subclasses. One subclass for each set of additional ui items that you may need. That is probably the neatest way of doing it. For each subclass use a different re-use identifier (!!!) This is what I would recommend! However, there are alternatives:

您仍然可以接受您的概念,但为每种类型的细胞发明一种单独类型的重用标识符它上面有一些额外的ui项目。如果是这样,那么请确保仅在代码的 if(cell == nil)分支中创建并添加这些UI项目作为子视图。只创建一次,然后重复使用它们。单元重用-ID可以是电子邮件显示,电子邮件输入,密码显示,密码输入,切换,...

You could still live with your concept but invent an individual type of re-use identfier for each type of cell that has some type of additional ui item on it. If so, then make sure that these UI items are only created and added as sub-views in the if (cell == nil) branch of your code. Only create them once and then re-use them. Cell reuse-IDs could be "email-display", "email-input" , "password-display", "password-input", "switch", ...

上面解决方案的方差是,将行和段
计算到重用标识符中。例如第0部分的cell-id-0.2和第2行的
- 左右。但是你仍然需要确保你真的
重新使用额外的UI视图,并且当单元格充满数据时,每次
时都不会重新创建它们。此外,第一部分的布局取决于您是要输入密码和电子邮件还是只显示它们。您仍然需要处理这些变化。

A variance of the solution above would be, to calculate row and section into the reuse-identifier. Such as "cell-id-0.2" for section 0 and row 2 - or so. But still you will have to make sure that you really re-use the additional UI views and do not re-create them every time when the cell is filled with data. Plus, the layout in your first section varies depending on whether you want to input password and e-mail or just display them. You will still have to deal with those variations.

如果cell == nil - 意味着是否重新使用了一个单元格 - 那么首先从之前添加的每个UI项目中清除它。你可以通过标记你的UIViews来实现这一点 - 比如99 - (任何不同于0应该做的事情)在创建时和重用枚举所有子视图并删除那些具有标记99的那些。尽管你可以坚持使用代码你已经做了。

If cell == nil - meaning if a cell is re-used - then first clean it from every UI item that you may have added before. You can do that by tagging your UIViews with - let's say 99 - (anything different from 0 should do) upon creation and when reusing enumerate over all subviews and remove those, which have the tag 99. Despite that you can stick with the code that you have already made.

这篇关于tableviews单元格在向下滚动后会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆