我是目标c和iOS的新手。问题很简单,UIImagePicker
选取的图像被分配到自定义单元格图像视图,我有3个部分,其中5行具有相同的按钮和UIImageView
。每当我在第3行选择一张图像时,它就显示在第3行的所有3个部分上。我真的厌倦了请有人请帮助我。
这是我的代码。
- (void)viewDidLoad {
[super viewDidLoad];
imgArr=[[NSMutableArray alloc]init];
[imgArr addObject:[[UIImage alloc]init]];
[imgArr addObject:[[UIImage alloc]init]];
[imgArr addObject:[[UIImage alloc]init]];
[imgArr addObject:[[UIImage alloc]init]];
[imgArr addObject:[[UIImage alloc]init]];
[_myTableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return imgArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section==0){
return @"Section 1";
}else if (section==1){
return @"Section 2";
}else{
return @"Section 3";
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (cell == nil)
{
NSArray *myArray = [[NSBundle mainBundle]loadNibNamed:@"MyCustomCell" owner:self options:nil];
cell = [myArray objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
cell.mvc=self;
[cell.btn addTarget:self action:@selector(slctImage:) forControlEvents:UIControlEventTouchUpInside];
index=(indexPath.section * 1000) + indexPath.row;
section=index%1000;
NSLog(@"Button Tag is %ld",(long)index);
cell.imageVw.image=[imgArr objectAtIndex:indexPath.row];
return cell;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *pic=[info valueForKey:UIImagePickerControllerOriginalImage];
NSLog(@"image is %@",pic);
img=pic;
[imgArr replaceObjectAtIndex:section withObject:img];
picker.delegate=self;
[picker dismissViewControllerAnimated:YES completion:nil];
[_myTableView reloadData];
}
答案 0 :(得分:1)
首先,您使用indexpath.row获取特定单元格视图的图像,但问题是每个部分的indexpath.row = 0/1/2。
因此,每个部分的第一,第二或第三个图像都会显示图像,因为img [indexPath.row]会返回相同的图像。
您需要使用indexPath.section检查该部分并相应地分配图像。
您可以为不同的部分维护三个不同的数组。 如果你想让它变得动态。 您需要以适应的方式创建数据结构。