UITableviewCell Image size changing on Click of cell? - ios

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... :)

Related

UITableView not loading data with TabBar Controller

I have tableview in one of my controller. It is not loading data into custom labels and image view with tags. I checked each and every thing, delegates are attached and I am reloading the data in viewWillAppear. I do not understand, where is the problem. I added label and images and assign them tag. Only default label of tableview is showed.
NSString *cellIdentifier;
NSMutableArray *historyArray;
#implementation CloudHistory
-(void)viewDidLoad
{
[super viewDidLoad];
historyArray = [[NSMutableArray alloc]initWithObjects:#"history1",#"history2",#"history3",#"history4",nil];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
dispatch_async(dispatch_get_main_queue(), ^{
//Reload data in services table.
[_historyTableView reloadData];
});
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Number of section in services table.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [historyArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Change the background color to stoker Cloud color.
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = [ UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:1.0];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
#end
maybe you can export the name and image , I think some of them maybe is invalid or nil .
UILabel *name = (UILabel*)[cell viewWithTag:40];
NSLog(#"name = %#",name); // is it valid ?
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
NSLog(#"image = %#", image); // is it valid ?
if some of them invalid or nil,you can do something like this:
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 80, 44)];
name.text = #"name";
[cell addSubview:name];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 44, 44)];
imageView.image = image;
[cell addSubview:imageView];
hope it helps.
Did you create a custom UITableViewCell using a xib file?
If so, you could register it in viewdidload.
Something like this
[self.tableView registerNib:[UINib nibWithNibName:#"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
Then your tableview can find your custom view outlets.
Hope this helps
Why don't you use a custom UITableViewCell with your UILabel and UIImageView? So you can access the cell property, like:
cell.name.text = [historyArray objectAtIndex:indexPath.row];
cell.image = [UIImage imageNamed:#"rtb_logo"];
Anyway looks like your UILabel and UIImage is getting the values, but it's not setting them to your cell. To test you can log the value of the UILabel just to check. I don't know if it will work but have you tried this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
[cell viewWithTag:40] = name; // I don't know if this works
// Check it out if the name.text is getting any value
NSLog(#"UILabel %#",name.text);
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
[cell viewWithTag:44] = image; // I don't know if this works
return cell;
}

Add second image to standard UITableViewCell

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
}

how to set background image in uitableview cell in ios7?

i am new to ios and now a days i am facing a little issue.When table view load then after scrolling up and down then background image show like this and here is my code
Actually i want to show table as chat design. i not want to use any third party.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_messages count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *comment = [_messages objectAtIndex:indexPath.row];
CGFloat whidt = 250;
UIFont *FONT = [UIFont systemFontOfSize:18];
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:comment attributes:# { NSFontAttributeName: FONT }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height +50;
}
- (UIFont *)fontForCell{
return [UIFont boldSystemFontOfSize:18.0];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"ChatListItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *text = [_messages objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(250 , 20000.0f);
UIFont *FONT = [UIFont systemFontOfSize:11];
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:FONT }context:nil].size;
cell.textLabel.frame = CGRectMake(10, 0, 250, MAX(size.height, 54.0f) + 20.0f);
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.text = text;
cell.textLabel.font = [UIFont systemFontOfSize:18];
UIImage *img = [UIImage imageNamed:#"balloon_selected_right.png"];
CGSize imgSize = cell.textLabel.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,250,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.textLabel.backgroundColor = [UIColor colorWithPatternImage:newImage];
/*[cell.textLabel setText:[_messages objectAtIndex:indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [_messages objectAtIndex:indexPath.row];*/
return cell;
}
Thanks is advance!
use a custom delegate method for this. here is the solution of your proble ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [self getCellContentView:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
[textLabel setText:#""];
textLabel.text = #"some text message to show"; // your text string
return cell;
}
- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell.backgroundColor=[UIColor clearColor];
CGRect labelRect = CGRectMake(20, 5, 100, 30); // set your desired values here
UIImage *img = [UIImage imageNamed:#"image.png"];
CGSize imgSize = labelRect.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,250,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UILabel *textLabel = [[UILabel alloc] initWithFrame:labelRect];
textLabel.tag=1;
textLabel.font=[UIFont fontWithName:#"Superclarendon" size:15];
// some of your other constraints to set ...
textLabel.backgroundColor=[UIColor colorWithPatternImage:newImage];
[cell.contentView addSubview:textLabel];
return cell;
}
Hi you can use resizableImageWithCapInsets,
UIImage *image = [[UIImage imageNamed:#"baloonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0,10.0,5.0,10.0)];
[self.yourButton setBackgroundImage:image forState:UIControlStateNormal];
Modify the values (UIEdgeInsetsMake(top, left, bottom, right)) to suit any image you need.
Above code will make the image corners fixed and then after you can resize the image frame to adjust your view size,
[image setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
Haseeb, based on this line of code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
I can see that you are not using custom table view cells.
I think the best way to solve this would be to use a custom table view cell. You would create your own class where you could design your own cell.
There's a couple of ways of doing this: 1) create a custom UITableViewCell (lots of examples on the net on how to do this), 2) you could also simply add your image as a Layer to the cell (again, lots of examples).
For example:
ImageTableViewCell : UITAbleViewCell
#property (strong, nonatomic) UIImage *backgroundImage;
Then, in your implementation something like this:
-(void)layoutSubviews
{
[super layoutSubviews];
CALayer *imageLayer = [[CALayer alloc] init];
imageLayer.frame = self.layer.bounds;
// code goes here to do whatever you want to the image ...
imageLayer.contents = self.backgroundImage.CGImage;
[self.layer addSublayer:imageLayer];
}
Food for thought, as they say ....
As suggested above use a custom prototype cell. Then in the Document Outline select the prototype cell and the Attributes inspector. Under View put Alpha to 0 Background color to Clear Color.
In swift you can set background image to UITableViewCell like below.
cell.backgroundView = UIImageView(image: UIImage(named: "yourImage.png")!)
Write this code in this function of table view.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
This image will repeat fort each row.

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;
}

Resources