I've an UIImageView in customized UITableViewCell and I want to resize it, I set it view mode to LEFT. To resize I'm applying below code.
[cell.IBImage setFrame:CGRectMake(201,12,50,20)];
My image width is 100 and I want to reduce is to 50 but it still shows me in 100 width. If I set view mode to Scale To Fill it works fine but I want to have it's view mode to LEFT.
Full method cellForRowAtIndexPath is as given below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"DetailCell";
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DetailCell" owner:self options:nil];
cell = (DetailCell *)[topLevelObjects objectAtIndex:0];
clsDetailResponse *Response = [self.Histories objectAtIndex:[indexPath row]];
[cell.IBUsernameLabel setText:[NSString stringWithFormat:#"%# %#", Response.Firstname, Response.Lastname]];
[cell.IBImage setFrame:CGRectMake(201, 12, 50, 20)];
return cell;
}
You are doing to resize the imageView frame not the image so You can crop your image as you want,
UIImage *ima = cell.image.image;
CGRect cropRect = CGRectMake(0, 0, 50,20);
CGImageRef imageRef = CGImageCreateWithImageInRect([ima CGImage], cropRect);
[cell.image setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
cell.image.frame=CGRectMake(10, 10, 50, 20);
set IBImage frame in DetailCell class in
-(void)layoutSubviews:
method
Add the following lines to the File's owner to make sure it knows how to respond to framechanges.
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
[self setClipsToBounds:YES];
[self setAutoresizesSubviews:YES];
Related
I am new to iOS and i have some serious problem i don't know why this happened, i also tried to find out the reason behind this but i failed.
I am creating a app in iPhone which take tweet of particular user using twitter api, i have no problem in getting the json data, also able to display it in UITableView in iOS 6 using storyboard, i had changed the row height to 200 px, so that i can display user name text tweet and image.
But when I scroll up or down the UITableViewCell the scrollbar goes out of the screen.
What may be the reason?how can I fix it?
ScreenShots
First it shows like this when the app loads the scrollbar takes the full screen
then when i scroll it get out of the screen
at last it completely gets out of screen
UITableView delegate methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *name,*time,*tweet,*image;
name=[[NSMutableArray alloc]init];
time=[[NSMutableArray alloc]init];
tweet=[[NSMutableArray alloc]init];
image=[[NSMutableArray alloc]init];
static NSString *CellIdentifier = #"SimpleTableItem";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
for (User *userModel in self.user) {
[name addObject:userModel.nameStr];
[image addObject:userModel.imageStr];
}
for (Json *jsonModel in self.json) {
[tweet addObject:jsonModel.tweetStr];
[time addObject:jsonModel.timeStr];
}
// Fetch using GCD
dispatch_queue_t downloadThumbnailQueue = dispatch_queue_create("Get Photo Thumbnail", NULL);
dispatch_async(downloadThumbnailQueue, ^{
NSString *imageURL=[image objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:5];
image.image=img;
[cell setNeedsLayout];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[spinner stopAnimating];
});
});
self.labelName = (UILabel *)[cell.contentView viewWithTag:10];
[self.labelName setText:[NSString stringWithFormat:#"%#", [name objectAtIndex:indexPath.row]]];
self.labelTime = (UILabel *)[cell.contentView viewWithTag:11];
[self.labelTime setText:[NSString stringWithFormat:#"%#", [time objectAtIndex:indexPath.row]]];
self.labelTweet = (UILabel *)[cell.contentView viewWithTag:12];
[self.labelTweet setText:[NSString stringWithFormat:#"%#", [tweet objectAtIndex:indexPath.row]]];
cell.imageView.frame=CGRectMake(0, 0, 40, 40);
cell.textLabel.lineBreakMode=NSLineBreakByWordWrapping;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat rowHeight=self.labelName.frame.size.height+self.labelTime.frame.size.height+self.labelTweet.frame.size.height+50;
return rowHeight;
}
Well scroll goes out of the screen means the tableView frame is larger than the screen size
Set the frame of tableview properly using setFrame: method
I think you have to check the height of the UITableView. Show the screenshot.
Here the ScrollBar goes down as the Height of TableView is larger then the UIView in which you are adding TableView. Adjust the frame according to your view then the ScrollBar will not disappear.
Am creating a Rss feed app. Where i need to show Description (more than 5000 characters each). Am able to show all feeds in my app but my problem when i try to scroll the UITableView the scrolling not smooth i believe i need to make lazy loading for my content but am new to iphone development. Can any one show me correct way. And also set correct Cell Size according to the content size.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {
if(cell.gestureRecognizers.count==0){
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longSelection:)];
longPress.minimumPressDuration=1.0;
[cell addGestureRecognizer:longPress];
}
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
cell.textLabel.textAlignment=NSTextAlignmentRight;
cell.textLabel.text=item.content;
cell.textLabel.numberOfLines=0;
cell.textLabel.font=[UIFont systemFontOfSize:15.0];
[cell.textLabel sizeToFit];
}
return cell;
}
Thanks
You have to use the -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
like so:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
//make sure to add the font you're using
CGSize stringSize = [item.content sizeWithFont:[UIFont fontWithName:#"HelveticaNeue" size:14]
constrainedToSize:CGSizeMake(320, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
int rowHeight = stringSize.height;
return rowHeight;
}
Put this code:
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
Inside of here like so and remove it from where it is now (keeping it there causes you to have new imageviews created every time you scroll:
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
}
I am setting a UIImage, via a UIImageView as my background view of a subclassed UITableViewCell like so:
-(void)awakeFromNib {
UIImage *backImg = [[UIImage imageNamed:#"CellBackground"]
resizableImageWithCapInsets:UIEdgeInsetsMake(16, 132, 16, 16)];
UIImageView *imv = [[UIImageView alloc] initWithImage:backImg];
self.backgroundView = imv;
}
This works excellently, each cell is different in height (exposed via heightForRowAtIndexPath which calculates the height of a UILabel with text in it), and the background image resizes as I want the cell to.
However, when I rotate the device, the view hangs mid rotate, and takes 5-10 seconds to redraw in landscape, or crashes with no error. If I remove this imageview from the backgroundView, the rotate works excellently. Both simulator and device.
[Edit] Alternatively, I added the imageview as a subview of the cell.contentView - performance was better, but still laggy.
UIImage *backImg = [[UIImage imageNamed:#"CellBackground"]
resizableImageWithCapInsets:UIEdgeInsetsMake(16, 132, 16, 16)];
UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imv.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imv.image = backImg;
[self.contentView addSubview:imv];
Additionally, as mentioned above, my UITableViewCell is a subclass. The above code is in awakeFromNib, and I'm loading in my UITableViewCell like so:
// within initWithNibName: of UIViewcontroller:
cellLoader = [UINib nibWithNibName:#"MasterTableViewCell" bundle:[NSBundle mainBundle]];
// and UITableView method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MasterTableViewCell *cell = (MasterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MasterTableViewCell"];
if (cell == nil) {
NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
return cell;
}
Am I doing something wrong? Any hints?
Are you calling this every time you draw a cell? This can really hurt your performance. I would suggest only doing this when drawing a new cell. So it would look like
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
UIImage *backImg = [[UIImage imageNamed:#"CellBackground"]
resizableImageWithCapInsets:UIEdgeInsetsMake(16, 132, 16, 16)];
UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
imv.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imv.image = backImg;
[self.contentView addSubview:imv];
}
I'm dynamically loading images from a web service. The images will be loaded (with [UIImage imageWithData:data];) and be placed in a in an UIImageView in my custom UITableViewCell using the setImage message
. I gave the imageView a width and height of 27 by 19 in the interface builder and told him to "Aspect fit" (tried all the others too btw), but the image doesn't do that. It just scales (in aspect, I think) to fill the view cell.
I tried a lot of things, like resizing the image (does the job, but I can count the pixels on my retina display), resizing the UIImageView... But I just don't get is... Someone got an idea what I'm doing wrong?
The code that changes the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = self.tableCell;
self.tableCell = nil;
}
viArticle *article = [self.viData.articles objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = article.title;
label = (UILabel *)[cell viewWithTag:2];
label.text = [NSString stringWithFormat:#"%# %#", article.time, article.type];
if (article.image != nil) {
UIImageView *imageView;
imageView = (UIImageView *)[cell viewWithTag:0];
[imageView setImage:article.image];
}
return cell;
}
This is the view:
And the settings:
What happens if you untick "Autoresize Subviews"?
#GuidoH hey sorry for the late reply..
Here is the code that will add the image to the table view cell.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *SimpleTableIdentifier = #" SimpleTableIdentifier ";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
UIImage *image = [UIImage imageNamed:#"star.png"];
cell.imageView.image = image;
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
//use this delegate it will increase the row size and thus your imagesize will get increased too.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80; //value can be changed
}
I would suggest scaling the images will surely lead to slow performance by your app.
I just posted a "solution" earlier today, but that seem to work, but was a wrong assumption. After asking on IRC I found the real solution. I was using the tag 0, that one shouldn't be used. Changing it to an higher value really solved my problem. Sometimes it can be thát easy. :)
Thanks y'all.
If you want to resize the UIImageView you can use the frame property.
suppose you have a UIImageView declare in .h file(lets take UIImageView *myImage;) and it is linked to the interface builder too.
you have synthesis it in .m class etc.
then when you want to resize the UIImageView, use
myImage.frame = CGRect(x,y,width,height);
Note: by declaring the width and height in the code, the width and height declared in the IB will be override.
then dont forget to add it to subview,
i.e [self.view addSubview:myImage];
and its done, you can see the changes.
I have a table-cell which has all default settings. And I put an image on the ImageView on the left, and I put some random text on TextLabel.
and I adjust cell's height using below method.
(CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
The problem is the left ImageView's frame size is different depend on the cell's height.
See the below image, the 4th cell's height is bigger than others and it's image is slightly right compare to other images.
What causes this and how can I solve this probelm??
-- added
This is my cellForRowAtIndexPath method.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultt reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [self GetSelectionForRowAtIndexPath:indexPath];
NSString *path = [[NSBundle mainBundle] pathForResource:#"icon_notSelected" ofType:#"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
This shows the imageView does not get bigger even if I put more height on the cell.
UIImage *image = [UIImage imageNamed:#"your_image.png"];
cell.imageView.image = image;
put this code in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
If will show the image in each cell of the table in same line.
You can do it only by Code. No need of IB for this.
Give the imageview a fixed frame and then center it vertically in the cell.
Something along these lines:
imageview.frame = CGRectMake(5, floor(cellHeight/2)-20, 40, 40);
Or just use the imageview property of UITableViewCell.