Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UICollectionViewCell中的文本字段变得疯狂_Iphone_Ios_Objective C - Fatal编程技术网

Iphone UICollectionViewCell中的文本字段变得疯狂

Iphone UICollectionViewCell中的文本字段变得疯狂,iphone,ios,objective-c,Iphone,Ios,Objective C,我在每个单元格中有一个collectionview和一个文本字段 我发现一些意想不到的行为 有时textfield中的文本会消失或随机复制到其他单元格中的textfield 如何修复此问题因为您使用的是可重用单元格(来自-dequeueReusableCellWithReuseIdentifier:),所以返回的单元格中可能已经包含以前单元格的内容。在返回之前,需要明确清除返回的单元格中文本字段的内容。例如: -(UICollectionViewCell *)collectionView:(UI

我在每个单元格中有一个collectionview和一个文本字段

我发现一些意想不到的行为 有时textfield中的文本会消失或随机复制到其他单元格中的textfield


如何修复此问题

因为您使用的是可重用单元格(来自
-dequeueReusableCellWithReuseIdentifier:
),所以返回的单元格中可能已经包含以前单元格的内容。在返回之前,需要明确清除返回的单元格中文本字段的内容。例如:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Cell *aCell = [collectiveView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

return aCell; 
}

这是可能的。但是,不建议这样做,如中所示:“您通常不自己创建此类的实例……当您想要单元类的新实例时,请调用集合视图对象的
dequeueReusableCellWithReuseIdentifier:forIndexPath:
方法来检索一个实例。”
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" 
                                                           forIndexPath:indexPath];

    cell.textField.text = @"Default contents"; // or other data from your model
    return cell;
}