我是Objective-cI我正在处理集合视图。在我的集合视图中,我的数组中包含数据列表。当我将数据从我的数组加载到集合单元格时,它没有按照数组顺序显示。当我滚动时,单元格值会不断变化。
这是我的代码。
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return myObject.count;
}
- (UIView *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
if([statusfield isEqualToString:@"13"])
{
[cell.mySegmentedControl setTitle:@"SEND TO ADMIN" forSegmentAtIndex:0];
[cell.mySegmentedControl setTitle:@"EVALUATE" forSegmentAtIndex:1];
}
else if([statusfield isEqualToString:@"5"])
{
[cell.mySegmentedControl setEnabled:NO forSegmentAtIndex:0];
[cell.mySegmentedControl setEnabled:NO forSegmentAtIndex:1];
}
else if([statusfield isEqualToString: @"1"])
{
[cell.mySegmentedControl setTitle:@"ACCEPT" forSegmentAtIndex:0];
[cell.mySegmentedControl setTitle:@"REJECT" forSegmentAtIndex:1];
}
else if([statusfield isEqualToString:@"3"])
{
[cell.mySegmentedControl setTitle:@"VIEW" forSegmentAtIndex:0];
[cell.mySegmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
}
return cell;
}
分段控件值正在根据数组进行更新。当我滚动集合视图时,它的顺序正在发生变化。任何帮助都将不胜感激。
答案 0 :(得分:1)
检查以下代码。它可能会帮助你
for (UIImageView *imgView in cell.subviews) {
if ([imgView isKindOfClass:[UIImageView class]]) {
[imgView removeFromSuperview];
}
}
之后添加你的观点
UIImageView *segmentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, cell.frame.size.width - 10, cell.frame.size.height - 10)];
segmentImageView.image = [segmentControlArray objectAtIndex:indexPath.row];
segmentImageView.contentMode = UIViewContentModeScaleAspectFit;
[cell addSubview:segmentImageView];