简体   繁体   English

滚动UITableView时UITableViewCell的位置更改

[英]UITableViewCell position change while scroll UITableView

My UITableViewCell position changed while scroll UITableView . 滚动UITableView我的UITableViewCell位置发生了变化。 Here i have more UITableViewCell . 在这里,我有更多的UITableViewCell So it covers more than one page screen. 因此它涵盖了不止一页的屏幕。 When i scroll top to bottom the position of UITableViewCell is changed such like 1st cell went 5th. 当我从上到下滚动时, UITableViewCell的位置会发生变化,例如第一个单元格排在第五位。 And 2nd cell came to 1st and etc. 第二单元到达第一单元,依此类推。

My Code: 我的代码:

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

Could anyone guide me to archive it correctly.. 谁能指导我正确存档。

Thanks in Advance. 提前致谢。

You must set the visual elements of the cell even when cell is not nil. 即使单元格不是nil,也必须设置单元格的视觉元素。 If you read the documentation, you will notice that Apple says that the cell's content must always be updated in this method. 如果阅读文档,您会注意到Apple表示必须始终使用此方法更新单元格的内容。 The reason is because checking for nil just tests if you must create a new cell. 原因是因为检查nil只是测试是否必须创建新的单元格。 If you don't need to create a new cell, that means you are reusing a cell, and therefore it will most likely display contents from a row other than the one you intended. 如果不需要创建新的单元格,则意味着您正在重用一个单元格,因此,该单元格很可能会显示您想要的行以外的内容。 Therefore, you should reset the contents every time in this method. 因此,您应该每次使用此方法重置内容。

From this document : 从此文件

The table view's data source implementation of tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. 重新使用单元格时,tableView:cellForRowAtIndexPath:的表视图的数据源实现应始终重置所有内容。

Here's some code to get you started... but it will need to be improved since you are adding subviews to the cells in this method. 这里有一些代码可以帮助您入门……但是由于要在此方法中向单元格添加子视图,因此需要对其进行改进。 That should only happen when the cell is nil, and you should store a reference to those subviews so that they can be accessed at other times. 只有在单元格为零时才应该发生这种情况,您应该存储对这些子视图的引用,以便可以在其他时间访问它们。

- (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更好的方法是创建一个包含所有对象的自定义单元格。

To optimize the memory iOS initializes and uses only screen displayed cells. 为了优化内存,iOS会初始化并仅使用屏幕显示的单元格。 When you scroll to see next content it dequeues the non-visible cell and uses it for new visible cell. 滚动查看下一个内容时,它将使不可见的单元出队,并将其用于新的可见单元。 So, 1st cell is used at 8th row and so on. 因此,第一个单元格用于第8行,依此类推。 Now, in your code, you are not resetting the content when this dequeue operation is happening. 现在,在您的代码中,当此出队操作发生时,您无需重置内容。 All your cell content code is only when cell is not there. 您所有的单元格内容代码仅在单元格不在时才存在。 Take out the cell initializing code from your IF condition and you should be good to go. 从您的IF条件中取出单元初始化代码,您应该一切顺利。

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

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