Add second image to standard UITableViewCell - ios

Is there a way to add a second image to the right of a table row while still using a standard subview cell? I would like to take advantage of the standardization of the subview but add a second picture to the far right.
Basically, I want a circular profile picture to the left as in Instagram and a separate photo to the right of the text.
Here is my code for the table cell that works:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListPrototypeCell" forIndexPath:indexPath];
// Configure the cell...
tableItem *tableItem = [self.tableItems objectAtIndex:indexPath.row];
cell.textLabel.text = tableItem.itemName;
cell.detailTextLabel.text = tableItem.itemSub;
cell.imageView.image = [UIImage imageNamed:#"pic.jpg"];
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.layer.masksToBounds=YES;
cell.imageView.layer.cornerRadius=20.0;
cell.imageView.frame = CGRectMake(20, 20, 500, 50);
return cell;
Can I add a separate image in here without having to create a whole custom cell? Here is look I am going for:
two pictures

You'll have to basically make a custom UITableViewCell in this case. The standard UITableViewCell just has the one image view on the left.
When you create the custom UITableViewCell (it can be done in the Storyboard), be sure to give it an identifier. Then place your two UIImageViews within the custom cell and give them each a unique tag. Then you can call up the UIImageViews based on their tags:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListPrototypeCell" forIndexPath:indexPath];
UIImageView *img1 = (UIImageView *)[cell viewWithTag:1];
UIImageView *img2 = (UIImageView *)[cell viewWithTag:2];
// Set the images of img1 and img2
}

Related

UITableviewCell Image size changing on Click of cell?

I am trying to display name and images in UITableViewCell like the normal way
cell.textLabel.text=#"some name";
cell.imageView.image=[array objectAtIndex:indexPath.row];
and when i load the ViewController , I can see all images are coming correctly but when I tried to click on any of UITableviewCell image size is changing to big(please check the example screenshot).
Please help me to solve this problem.
Loading time it looks like
When I click on the first cell ,the cell image becomes big like below image
Please check my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.backgroundColor=[UIColor clearColor];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.backgroundColor=[UIColor clearColor];
if([userName count]>0){
cell.textLabel.text=[userName objectAtIndex:indexPath.row];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[userList valueForKey:#"photo"] objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:#"avatar.png"]];
}
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.layer.masksToBounds=YES;
cell.imageView.layer.cornerRadius=9.0;
return cell;
}
My Didselect method is like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *nib;
if ([[MyDevice getMyDevice] isiPad])
{
nib = #"nibname_iPad";
}
else
{
nib = #"nibname";
}
ViewController *chat = [[ViewController alloc] initWithNibName:xibName bundle:[NSBundle mainBundle]];
chat.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:chat animated:YES];
}
Try to use Custom Cells (Change the Style of the cell in the attributes inspector of the cell), its better and you can design it as the way you want. Create in the Story board the UILabels and the UIImage (with a specific width and height), finally in the attributes inspector of the UILabel and UIIamge set them with a specific Tag number, this number has to be unic for both of them. And access them like this:
In the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Insert this:
UITableViewCell *customCell = [self.tableView dequeueReusableCellWithIdentifier:#"theNameOfTheCell"];
UILabel *label;
label = (UILabel *)[customCell viewWithTag:1];
label.text = #"Some text to label with tag number 1";
label = (UILabel *)[customCell viewWithTag:2];
label.text = #"Some text to label with tag number 2";
UIImageView *imageCell;
imageCell = (UIImageView *)[customCell viewWithTag:3];
CALayer * l = [imageCell layer];
UIImage *cellImage = [UIImage imageNamed:#"theImageYouWant.png"];
imageCell.image = cellImage;
Try with this and tell what append, this is the full Documentation: Official Documentation Table View Cells
Add content mode:
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
Hope this helps... :)

UIImageView changing size unexpectedly

I have UITableView with cells that have images on it. Im using storyboard, and connect UIImageView as outlet and name it imageView. Images what I get come with different size (i download it through URL). But when table shows up sometimes image shown in incorrect size (it suppose to be square with 66x66 width and height, but in many cases square is bigger, or it actually have more width then height).
Obviously i want that borders of my UIImageView be stable. There is my code for cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"name"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.imageView setImageWithURL:[NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:#"imageFirst"]] placeholderImage:[UIImage imageNamed:#"dashie.png" ]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.width /2.0f;
cell.imageView.clipsToBounds = YES;
cell.separatorInset = UIEdgeInsetsMake(0, 82, 0, 0);
cell.imageView.image = resizedImage ;
}];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
Please explain me whats going on and how to fix that, any advice would be appreciated, thanks!
Add new imageView and set the image. I would recommend you make subclass of uitablviewCell and add the imageView as outlet view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *labelText = (UILabel *)[cell viewWithTag:1000];
labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"name"];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:yourFrame];
imgView.contentMode = UIViewContentModeScaleAspectFit;
[cell addSubview:imgView];
imgView.tag = 7;
}
UIImageView *img = (UIImageView*)[cell viewWithTag:7];
UIImage *resizedImage = [image resizedImageWithBounds:CGSizeMake(66, 66)];
img.imqge = resizedImage
You are using standard UITableViewCell class and try to change its imageView property. Not your cell, that you created on Storyboard.
You should look through this tutorial Storyboards Tutorial in iOS 7: Part 1.
Your are interested in part:
Designing Your Own Prototype Cells
Using a Subclass for the Cell
I'll recommend use subclass of UITableViewCell issue.

Click on a cell, the image size changed

I am setting the imageview size to be fixed. I have set the constraint in the storybard, and the constraint is correct. But after I loading some images from the web service, when I click on the cell, some of the images changed it's size, some of them don't. I have set the image view contentMode to aspect fit. How to make the image auto resize to fit in the imageview, and I don't have to click on the cell, to make it resize to fit?
The cellForRow:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
LocalCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[LocalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *photoReference = [self.restaurants[indexPath.row][#"photos"] objectAtIndex:0][#"photo_reference"];
NSString *photo = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/photo?maxwidth=80&photoreference=%#&sensor=true&key=%#", photoReference, AppKey];
cell.name.text = self.restaurants[indexPath.row][#"name"];
cell.desc.text = self.restaurants[indexPath.row][#"vicinity"];
[cell.imageView setImageWithURL:[NSURL URLWithString:photo] placeholderImage:[UIImage imageNamed:#"placeholder"]];
return cell;
}
Set your UIImageView clipsToBounds propery to YES as below
[yourImageView clipsToBounds:YES];
I just figured out:
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(15, 15, 80, 50);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

SDWebImage UIImageView+WebCache in custom UIImageView

I'm having a problem with loading images from my webserver into my UIImageView in a tableview
This code works perfect, but the images are placed weird and ugly in my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell. Note that this name is the same as in the storyboard
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Set the correct name in the cell.
//Do so by looking up the row in indexpath and choosing the same element in the array
NSInteger currentRow = indexPath.row;
Image* currentImage = [self.images objectAtIndex:currentRow];
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:#"testimage.jpg"]];
return cell;
}
So I created an UIImageView on my storyboard, and changed the code to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell. Note that this name is the same as in the storyboard
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Set the correct name in the cell.
//Do so by looking up the row in indexpath and choosing the same element in the array
NSInteger currentRow = indexPath.row;
Image* currentImage = [self.images objectAtIndex:currentRow];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];
// Here we use the new provided setImageWithURL: method to load the web image
[imgView setImageWithURL:[NSURL URLWithString:currentImage.src] placeholderImage:[UIImage imageNamed:#"testimage.jpg"]];
return cell;
}
But if I run this code on my device, I'm getting a signal SIGABRT error.
How can I get this working on my custom Imageview?
In the first code snippet, add these lines:
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
You are getting SIGABRT because your imagevIview is NOT added to cell, but cell's contentView..! So the right way to get the imageView should be,
UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];

How to draw image on the right side of the UITableView cell on iOS 7 on iPad?

How to draw image on the right side of the UITableView cell on iOS 7 on iPad?
Tried and don't help the following code:
tableView.contentInset = UIEdgeInsetsZero;
[cell contentView].frame = CGRectMake(0, [cell contentView].frame.origin.y, cell.frame.size.width, [cell contentView].frame.size.height);
You most likely want to set the accessoryView property on UITableViewCell. This will put a an image view on the right side of a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Configure cell
}
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"<some image name>.png"]];
cell.accessoryView = image;
}

Resources