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