trying to load an image inside a cell.
i am able to load the image inside the cell but when scrolling up and down the tableView the image appears randomly inside other different cells as well…
here is some code:
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
}
if (indexPath.section == ONE_SECTION) {
switch(indexPath.row) {
case 0:{
//some code
}
break;
case 2:{
//some code
}
break;
case 3:{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(256,0,43,43)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:imageView];
imageView.layer.cornerRadius = 10.0;
//imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.layer.masksToBounds = YES;
imageView.image = [UIImage imageNamed:@"imageName.png"];}
break;
default:
break;
}
}
if (indexPath.section == TWO_SECTION) {
//some code
}
if (indexPath.section == THREE_SECTION) {
//some code
}
return cell;
}
thank you
Try with the below code. If you are reusing cells, you dont have to add
imageViewrepeatedly to all cells. You can create it in cell allocation part and just setimageView.imageproperty out side it as shown below.A better approach is to create custom
UITableViewCellclass and addUIImageViewinside theinitmethod of that cell. Then set the property ascell.imageView.image = ...incellForRowAtIndexPathmethod.