iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – tableviews单元格在向下滚动后会发生变化大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在分组的tableview中制作一个表单.在这种形式中,我有UIswitches和textfields.但在向下滚动后,单元格样式正在发生变化. 这是我的cellForRowATindex - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath
我正在分组的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的那些.尽管你可以坚持使用代码你已经做了.

大佬总结

以上是大佬教程为你收集整理的objective-c – tableviews单元格在向下滚动后会发生变化全部内容,希望文章能够帮你解决objective-c – tableviews单元格在向下滚动后会发生变化所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。