1

UITableViewCell滚动时我的位置发生了变化UITableView。在这里我有更多UITableViewCell。所以它覆盖了不止一页的屏幕。当我从上到下滚动时,位置UITableViewCell会发生变化,例如第一个单元格排在第 5 个。第二个细胞来到第一个等等。

我的代码:

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

谁能指导我正确存档它..

提前致谢。

4

3 回答 3

1

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

这个文件

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

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

- (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;
}
于 2013-10-26T18:29:11.317 回答
1

这是您if (cell == nil)第一次遇到问题,单元格为零时,我创建了一个单元格,然后我尝试重用 de cell,该单元格不为空,因此我像以前一样添加此单元格,无需进行任何更改,为解决您必须解决的问题删除ifand for don't have leak 最好创建一个包含所有对象的自定义单元格。

于 2013-10-26T18:29:45.020 回答
0

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

于 2013-10-26T18:45:18.627 回答