Custom UITableViewCell not appearing when row height is set - iphone

I have a custom UITableViewCell which I have created in IB. My labels display when I don't over-ride:
- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
however my content is squished. I want the height to be 120px so I have the following:
- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ CGFloat rowHeight = 120;
return rowHeight;
}
I'm not sure why the content inside of my UITableViewCell all of a sudden disappears?
UPDATE:
Here is how I create the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"ARCell";
ARObject *myARObject;
myARObject = [items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"ARCell" owner:self options:nil];
cell = arCell;
self.arCell = nil;
}
UILabel *label;
label = (UILabel *)[cell viewWithTag:0];
label.text = [NSString stringWithFormat:#"%#", myARObject.username];
label = (UILabel *)[cell viewWithTag:1];
label.text = [NSString stringWithFormat:#"%#", myARObject.created_at];
label = (UILabel *)[cell viewWithTag:2];
label.text = [NSString stringWithFormat:#"%#", myARObject.articleTitle];
label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:#"%#", myARObject.intro_text];
return cell;
}

within IB did you set the height of the view size there too?
if so you could just try to return 120 opposed to declaring it as a CGFloat
edit: one other thought, make sure you save in IB... thats the leading cause of display bugs!

I think there is something fishy going on with how you're loading the nib for the tableview cell. Instead of connecting the cellview to an outlet on the file's owner, why don't you try something like:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ARCell" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
At the very least I recommend you debug and set a breakpoint on that line to verify that
the cell really exists
the cell has the subviews you expect
the cell has the right frame and
bounds for the tableview cell

Related

UITableView Scrolling stuck when content/text of TableCell is big

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

How to avoid UIImageView contained in the UITableViewCell being expanded?

How to avoid UIImageView contained in the UITableViewCell being expanded when each table view cell has different height? I am having variable height for each row depending on the length of text coming for UILabel. When height of a UITableViewCell increases,the image views image height also increases. How to keep image views frame constant?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.myArray objectAtIndex:indexPath.row];
if(text.length!=0)
{
CGSize constraint = CGSizeMake(212, LONG_MAX);
CGSize size = [text sizeWithFont:[UIFont fontWithName:#"Arial" size:14.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height;
return (71.0 + height); //this gives the row height
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MyXYZ *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyXYZ" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[MyXYZ class]]) {
cell = (MyXYZ *)currentObject;
] break;
}
}
}
((HJManagedImageV *)cell.myImageView).url = [NSURL URLWithString:myImageUrl];
}
Set contentMode property of your image view to appropriate value to disallow scaling of the image inside image view and do not care about image view itself being resized.
Create a custom cell in which you can keep the frame of the UIImageView constant. This will help it to remain the same throughout the tableView cells even if you vary the height of various cells
try this
[cell.contentView addSubview:imageview];

The difference between two ways to access UIImageView under UITableViewCell of UITableView

I have a custom tableview to load news through webservice. I change image in each cell in method cellForRowAtIndexPath. After I got the right cell, I can access the image by two ways:
UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = news.image;
or directly:
cell.imageView.image = news.image;
when I use the second way, some images get into wrong cell while the first way give me a perfect result. It also happend in a callback method iconDownLoadFinsh.
I feel confused about it. Can anyone try to explain it? Please do not hesitate to ask questions. Thanks in advance.
- (void)iconDownLoadFinsh:(NSData *)imageData row:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = [UIImage imageWithData:imageData];
NewsModel *newsModel = [newsArray objectAtIndex:indexPath.row];
newsModel.image = [UIImage imageWithData:imageData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NewsCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = newsTableViewCell;
newsTableViewCell = nil;
}
// read from newsModel
NewsModel *news = [newsArray objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:10];
label.text = [NSString stringWithFormat:news.title];
label = (UILabel *)[cell viewWithTag:11];
label.text = [NSString stringWithFormat:news.description];
UIImageView *imageView;
imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = news.image;
if (news.image == nil)
{
imageView.image = [UIImage imageNamed:IconPlaceHolder];
// if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {
IconDownLoader *iconDownLoader = [[IconDownLoader alloc] init];
iconDownLoader.url = news.imageUrl;
iconDownLoader.delegate = self;
iconDownLoader.indexPath = indexPath;
[downloadArray addObject:iconDownLoader];
[iconDownLoader start];
// }
}
return cell;
}
I generally always reset the cell properties in the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
So I will do the following after getting the cell
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = newsTableViewCell;
newsTableViewCell = nil;
}
cell.imageView.image = nil; // Here resetting the imageview's image to nil.
Because you are reusing cells and if you do not clear old data, the cell data may get corrupt.

Limit the size of an UIImage which is set programmatically

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.

iPhone - UITableViewCell having UIView which has 2 UIButton

I've a table view. In each cell (row), i want to show two buttons. initially both are red in color. when one button is clicked, it turns green and other will be in red.
I created a view which has two buttons. I am using IB to create the view.
i'm using following code to show the my table view to show the custom view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:#"CellView"
owner:self options:nil];
CellView *myView;
for (id object in bundle) {
if ([object isKindOfClass:[CellView class]])
myView = (CellView *)object;
}
NSString* str = [testArray objectAtIndex:indexPath.row];
NSArray* arr = [str componentsSeparatedByString:#" "];
myView.left.titleLabel.text = [arr objectAtIndex:0];
myView.right.titleLabel.text = [arr objectAtIndex:1];
[cell.contentView addSubview:myView];
return cell;
}
Above code works fine but when the button is clicked, its showing the button with text that I created in IB. I am not understanding why is it happening?
Can some one guide me how to show a button in cell and handle its action?
I think you have to specify the titleLabel for different states of button. Then this code will work fine.