【发布时间】:2023-03-07 10:23:01
【问题描述】:
在我的项目中,我使用UICollectionView 来显示数据。数据完美显示在UICollectionViewCells 中,但我也在单元格中使用“编辑”和“删除”按钮来编辑或删除数据。
当我尝试编辑任何单元格并按下“编辑”按钮时会出现问题。它仅适用于前 3 个单元格。当我单击第四个单元格的编辑按钮时,它会在编辑页面上显示第一个单元格的结果,对于第五个单元格,它显示第二行的结果,对于第 6 个单元格,它显示第三行的内容,然后对于第 7 行,它再次显示第一行的结果单元格,这个过程一直持续到单元格结束。
编辑函数
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cellIdentifier";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
editbtn = (UIButton *)[cell.contentView viewWithTag:206];
editbtn.tag = indexPath.row;
[editbtn addTarget:self action:@selector(editprocedure:) forControlEvents:UIControlEventTouchUpInside];
[editbtn setTintColor:[UIColor blackColor]];
deletebtn = (UIButton *)[cell.contentView viewWithTag:207];
[deletebtn addTarget:self action:@selector(deleteprocedure:) forControlEvents:UIControlEventTouchUpInside];
[deletebtn setTintColor:[UIColor blackColor]];
deletebtn.tag = indexPath.row;
return cell;
}
我还分享了模拟器上的截图
这是编辑功能的代码
-(IBAction)editprocedure:(UIButton*)sender
{
UIButton *btnTapped = (UIButton *)sender;
self.strdiagid = btnTapped.accessibilityIdentifier;
NSLog(@"str ID : %@",self.strdiagid);
self.strdiagid = [CompletephyDic objectForKey:@"id"];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
EditProcedureVC *vch = [mainStoryboard instantiateViewControllerWithIdentifier:@"EditProcedureVC"];
self.strdiagid = [CompletephyDic objectForKey:@"id"];
NSString * a = self.strdiagid;
NSLog(@"id...%@",a);
vch.stridd = a; //stridd is the id of the procedure which is transferred to other view
vch.infoDictionary = [dataArray objectAtIndex:sender.tag];
[self presentViewController:vch animated:NO completion:nil];
}
【问题讨论】:
-
对于第四个单元格,按钮操作是否调用?如果不是,请检查 collectionview 中的单元格大小。
-
单元格数据来自api,collectionview中大约有15个单元格。通过上面的代码,编辑按钮对前 3 个单元格运行良好,但对于第四个单元格,它显示第一个单元格的内容
-
这是由于collectionView的重用单元格的行为,如果您可以分享editprocedure,deleteprocedure的代码,我可能会帮助您。
-
请再检查一次我更新了我的问题也请点赞,以便我可以添加截图并使用本网站的更多功能
-
你在这一行犯了错误 vch.infoDictionary = [dataArray objectAtIndex:sender.tag];您的按钮标签是 206 和 207,因此您得到了错误的数据。
标签: ios objective-c uibutton uicollectionview uicollectionviewcell