繁体   English   中英

滚动UITableView时UITableViewCell的位置更改

[英]UITableViewCell position change while scroll UITableView

滚动UITableView我的UITableViewCell位置发生了变化。 在这里,我有更多的UITableViewCell 因此它涵盖了不止一页的屏幕。 当我从上到下滚动时, UITableViewCell的位置会发生变化,例如第一个单元格排在第五位。 第二单元到达第一单元,依此类推。

我的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"SignUpWithEmailCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    switch (indexPath.row) {

        case 0: {

            UILabel *lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblFirstName.text = @"First Name";
            [cell.contentView addSubview:lblFirstName];

            self.txtFirstName = [[UITextField alloc]initWithFrame:CGRectMake(lblFirstName.frame.origin.x + lblFirstName.frame.size.width + 5, 0, 190, 40)];
            self.txtFirstName.delegate = self;
            self.txtFirstName.text = strFirstName;
            self.txtFirstName.tag = 0;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtFirstName.font = [UIFont systemFontOfSize:15.0];
            self.txtFirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtFirstName];
            break;
        } case 1: {

            UILabel *lblLastName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblLastName.text = @"Last Name";
            [cell.contentView addSubview:lblLastName];

            self.txtLastName = [[UITextField alloc]initWithFrame:CGRectMake(lblLastName.frame.origin.x + lblLastName.frame.size.width + 5, 0, 190, 40)];
            self.txtLastName.backgroundColor = [UIColor clearColor];
            self.txtLastName.delegate = self;
            self.txtLastName.tag = 1;
            self.txtLastName.text = strLastName;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtLastName.returnKeyType = UIReturnKeyDone;
            self.txtLastName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtLastName.font = [UIFont systemFontOfSize:15.0];
            self.txtLastName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtLastName];
            break;
        }
        default:
            break;
    }
}

 return cell;
}

谁能指导我正确存档。

提前致谢。

即使单元格不是nil,也必须设置单元格的视觉元素。 如果阅读文档,您会注意到Apple表示必须始终使用此方法更新单元格的内容。 原因是因为检查nil只是测试是否必须创建新的单元格。 如果不需要创建新的单元格,则意味着您正在重用一个单元格,因此,该单元格很可能会显示您想要的行以外的内容。 因此,您应该每次使用此方法重置内容。

从此文件

重新使用单元格时,tableView:cellForRowAtIndexPath:的表视图的数据源实现应始终重置所有内容。

这里有一些代码可以帮助您入门……但是由于要在此方法中向单元格添加子视图,因此需要对其进行改进。 只有在单元格为零时才应该发生这种情况,您应该存储对这些子视图的引用,以便可以在其他时间访问它们。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"SignUpWithEmailCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    switch (indexPath.row) {

        case 0: {

            UILabel *lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblFirstName.text = @"First Name";
            [cell.contentView addSubview:lblFirstName]; // TODO: MUST BE MOVED

            self.txtFirstName = [[UITextField alloc]initWithFrame:CGRectMake(lblFirstName.frame.origin.x + lblFirstName.frame.size.width + 5, 0, 190, 40)];
            self.txtFirstName.delegate = self;
            self.txtFirstName.text = strFirstName;
            self.txtFirstName.tag = 0;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtFirstName.font = [UIFont systemFontOfSize:15.0];
            self.txtFirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtFirstName]; // TODO: MUST BE MOVED
            break;
        } case 1: {

            UILabel *lblLastName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblLastName.text = @"Last Name";
            [cell.contentView addSubview:lblLastName]; // TODO: MUST BE MOVED

            self.txtLastName = [[UITextField alloc]initWithFrame:CGRectMake(lblLastName.frame.origin.x + lblLastName.frame.size.width + 5, 0, 190, 40)];
            self.txtLastName.backgroundColor = [UIColor clearColor];
            self.txtLastName.delegate = self;
            self.txtLastName.tag = 1;
            self.txtLastName.text = strLastName;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtLastName.returnKeyType = UIReturnKeyDone;
            self.txtLastName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtLastName.font = [UIFont systemFontOfSize:15.0];
            self.txtLastName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtLastName]; // TODO: MUST BE MOVED
            break;
        }
        default:
            break;
    }


 return cell;
}

这是您的问题, if (cell == nil)第一次使该单元格为零,那么我将创建一个单元格,那么我会尝试重用de cell单元格为非null,因此我像以前一样添加此单元格,而没有进行任何更改,为了解决您的问题,您必须删除if和for no donage更好的方法是创建一个包含所有对象的自定义单元格。

为了优化内存,iOS会初始化并仅使用屏幕显示的单元格。 滚动查看下一个内容时,它将使不可见的单元出队,并将其用于新的可见单元。 因此,第一个单元格用于第8行,依此类推。 现在,在您的代码中,当此出队操作发生时,您无需重置内容。 您所有的单元格内容代码仅在单元格不在时才存在。 从您的IF条件中取出单元初始化代码,您应该一切顺利。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM