Reputation: 137
I'm new to objective c and iOS. The problem is simple that the image that is picked by UIImagePicker
is assigned to custom cells image view, i have 3 sections with 5 rows with the same button and UIImageView
. whenever i picked a image on row 3 it displays on all 3 sections row 3. I'm really fed up please can someone please help me with this.
here is my code.
- (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];
}
Upvotes: 0
Views: 83
Reputation: 564
First of all you are using indexpath.row to get the image for particular cell view but the issue would be indexpath.row =0/1/2 for each section.
Hence every section's first,second or third image would show the image as img[indexPath.row] would return the same image.
You need to check the section using indexPath.section and assign the image accordingly.
Either you can maintain three different array for different sections. If you want to make it dynamic. You need to create your data structure in a way that it accommodates.
Upvotes: 1