Strange UITableViewCellStyleSubtitle + [cell imageView] behaviors - iphone

This question's been bugging me for a month and i've been resisting posting about it, but I can hold out no longer. Here's what's going on:
I have an app with UITableViewCells, all initialized as UITableViewCellStyleDefault. For each one, I am setting the image view's image using the excellent SDWebImage package, which extends UIImageView to have a method for [UIImageView setImageWithURL:(NSURL*)url placeholderImage:(UIImage*)image].
The problem is that the images are showing up more narrow than they should be, on the tableView, but for some reason selecting a row makes it the correct size. So does scrolling the row off screen, then onscreen, then offscreen.
What method should I be using to force cells to redraw? I have found a few conflicting ideas and none of them have worked so far. I feel like there's a simple call to UITableView to UITableViewCell I'm missing out on. Also, where do I put this call? I have tried calls in cellForRowAtIndexPath and willDisplayCell .
Thanks for your help!
SDWebImage: https://github.com/rs/SDWebImage

I solved the issue - I had to just make my SDWebImage placeholder images the same aspect ratio as the images I'm trying to load. As my server forces all my images to be 95x95, this was a relatively easy fix; I simply updated my placeholder.png to also be 95x95 (instead of some odd dimension it was, like 92x132)

[[cell imageView] setContentMode:UIViewContentModeScaleAspectFit];
this line makes the imageview show the image in its original resolution but fit to the image view size.
the main drawback is if u have the images in different sizes then each image will show i different sizes.... it should not looks good...

Related

Too many UILabels with unicode text

I have 180 UILabels (subviews of UITableViewCells) in an iPad app with 155 width X 155 height for each UILabel, and each one contains a big amount of Unicode text (Arabic language), when I scroll down the TableView it hangs for 1 second and then keeps scrolling normally, this happens with every scroll attempt by the user and this is tested on iPAD2 device.
however, when I changed the text to English language (also big amount of English text), the TableView does not hang and scrolls normally.
anyone got an idea on how to solve this issue with Unicode text ?
thank you so much in advance.
EDIT:
the code is large to fit here, so in brief, I create each UILabel with a loop like this: [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; in cellForRowAtIndexPath method, then play with the frame later in the same method according to interface orientation, after that I add each UILabel to the cell like this: [cell.contentView addSubView:myLabel]; . each cell contains 4 of these 'UILabels', so I have a total of 45 cells, nothing more, straight forward and simple code.
The use of unicode shouldn't be the problem here, as it will render at similar speeds to any other text.
There are possibly a few issues that are slowing down your code. First of all, you should attempt to use UITableView's native cell reuse, add the labels to the UITableViewCell and then dequeueWithResusableIdentifier them. You should only generate your labels when that method returns nil and you have to create a new UITableViewCell (it's unclear from the original question if you do this already).
One other thing you can do after this to make sure as many of your views are opaque as possible to speed up compositing. Instruments includes an option to tint non-opaque views to make this easier.
There are many ways you can optimize your code:
One check if your app is not leaking. Proper release of labels.
Use reusability of cells. I dont know if you are using that or not.
Since every time you scroll your cellForRowAtIndexPath delegate method is called.
In case you dont know about reusability try this link.

UITableViewCell not showing until scrolling

I'm having an issue where my UITableViewCell's image (I customized it into a cell with just the image) is not showing up after being downloaded unless you have scrolled down to make it invisible and scroll up to see again.
I am using HJCache for asynchronous image downloading as well as caching. What I am not sure is if this was the issue with HJCache or my UITableViewCell and how to fix it.
When the image has finished downloading (assuming the cell itself is the delegate for the asynchronous call), call [self setNeedsDisplay] which will redraw the cell. This is what happens when the cell is reloaded because of scrolling.
Well, I'm just going to answer my own question since I had found the most ridiculous solution.
After trying out various methods and headaches, I deleted the app ( + cache) from the simulator and everything seems to be working properly again.
My best guess is that the locally stored unfinished images by HJCache were messing up.
As soon as your data is finished loading, call [tableView reloadData]; This will make the tableView reload all of it's cells that are currently visible.
When you set up the cell set the image to nil. If that doesn't work set the image to a default locally stored image of the same size web if it's just completely white.

How to remove separation between UITableView cells

I want to remove the built in separation between cells in UITableView.
I tried using :
[self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
But that only removes the separator line.
I need the view to appear as if it's not a table view at all. (like the tableview is one big view who doesn't contain many separated cells)
Is that possible ?
Edit:
See the separation between the cells? I wan't it to disappear and the table view to be as if it's one big cell.
Edit 2:
The problem doesn't appear when I don't use an image view as the cell background, but just use a simple background color.
I tried using a different image, and as you can see the problem is much less obvious.
I would still appreciate a solution for the red image though, since I do have a lot of images that still can't be put as background currently. (Not sure why one image would cause the problem and other won't ,I guess something with the pic setting)
You can do something like
self.tableView.separatorColor = [UIColor whiteColor];
or
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
I suppose in your case it would be
self.myTableView.separatorColor = [UIColor whiteColor];
In the future you might want to search Stack Overflow as there are many similar questions.
Your separation is due to the background gradient in the red example and it is not coming from the tableView. in your second image there is no gradient so you don't see separation.
remove the gradient from the background image and it will be fine.
try to do this:
self.tableView.backgroundColor=[UIColor clearColor];
self.tableview.separatorColor=[UIColor clearColor];
self.view.backgroundColor=[UIColor yourcolor];
i don't remember if it's then color with image or backgroundwithimage or background with view.. don't remember, however logic way is this.
hope it's usefull
If you just want to get rid of the separator between UITableView cells. Two things you need.
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Make sure your imageView ( in your tableview cell) has same height as tableview cell. This means your image view is fully covering the table view cell. Goto inspector view and verify the height , if you see the height is 43 just set it to 44 ( 44 is default tableview cell height for retina display).
Go StroyBaord -> Select Table -> Attribute inspector -> Separator None
No Separator Will Appear
You seem to have answered your own question. If you want a solid colored background, make an image with just a solid color. I suspect it's not best practice, but it will get the job done.
I've done this many times in my own applications. I understand that you wish to make your tableview look as if its not a tableview but as if its on a blank piece of paper.
Just a note for you, the reason you can see the separation with some images and not so much with others is due to its image texture. When you place copies of the image side by side, they dont have a continuous pattern thus forming obvious visual separations.
Solution:
Assuming you have the tableview setup in a xib file, display the background of the table itself to to clear -> then set the background of the view itself to whatever image you like. You will then have a tableview with the background view being transparent with only the main view behind it showing its image or color. Displaying your image on this canvas will mean a clear one image background.

UIImageView in UITableCell gets a weird size

I'm trying to place a UIImageView in a UITableViewCell.
I create a special view called ProductView. In that view, I have several components, one of them being a UIImageView. I set this ProductView as content view of the UITableViewCell. Everything is drawn properly except the UIImageView. UIImageView gets scaled meaninglessly. It's height gets twice the height of the table cell size.
I've spent hours trying to solve this problem. I've set the frame of UIImageView manually, I've called drawRect method of UIImage in UIImageView, neither of them worked.
I'm thinking of replacing UITableView with a UIScrollView if I can't find a solution. Does anyone know how to solve this problem?
Use the Custom cells, and place all the UI component inside that cell, (including the UIImageView), and in cellForRowAtIndexPath method, set the image like following:
[cell.imgView setContentMode:UIViewContentModeScaleAspectFit];
[cell.imgView setFrame:CGRectMake(cell.imgView.frame.origin.x, cell.imgView.frame.origin.y, 120, 79)];
[cell.imgView setImage:[UIImage imageNamed:#"image.png"]];
And if you need to know how to use custom cells, follow this tutorial :
iPhone Programming Tutorial: Part 6: Creating custom UITableViewCell Using Interface Builder [UITableView]
Hope this helps you...
I've had the same problem and realized in the end that the issue wasn't anything to do with automatic resizing, it was simply that I was not getting the UIImageView, but its parent view (the table cell), due to clashing tag values (I was getting the view through callUIImageView *pictureView = (UIImageView *)[cell viewWithTag:0]; and tag zero was also set on the cell). Fixed by just changing the tag value.

Adding a dynamic-height UITableView into a scrolling view?

Hello all – I'm getting into iPhone development and have hit my first confusing UI point. Here's the situation:
My app is tab-based, and the view that I'm confused about has a static featured content image at the top, then a dynamic list below into which X headlines are loaded. My goal is to have the height of the headline table grow as elements are added to it, and then to have the whole view scroll (both featured image on top and headline list below). So, I guess my question comes in two parts:
1) First, how do you set up a dynamic-height table view that will grow as cells are added to it. So far I've only been able to have my tables handle their own scrolling.
2) Then, what is the root NIB view that the featured image and the table should live in to enabled scrolling? I've dropped oversized content into a UIScrollView now, although did seem to have any success with having it automatically scroll.
Thanks in advance for any help on this subject!
To the first:
As i understand your situation:
You want to add a image to the top of the UITableView and the image should scroll with the UITableView, shouldn't?
The UITabeView has a property called tableHeaderView. It's just a view, so you can set a UIImageView to it.
(I have no xCode at the current time, you need to edit the code)
UIImage *image = [UIImage imageNamed:#"myCoolPic.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame =CGRectMake(0,0,width,height);
tableView.tableHeaderView = imageView;
[imageView release];
What you're asking is probably doable with Interface Builder (or not, I don't know) but I know the code way to do it.
To change the height of the table all you do is set the frame of the UITableView object. The default height of a UITableViewCell is 44 I believe, so set it to multiples of that depending on how many cells you have. Of course your cells can be any height so you will need to keep track of what you report in heightForRowAtIndexPath and set the table frame accordingly.
UITableView will certainly live in a UIScrollView and both components can scroll. The table view needs to become a subview of the scroll view, so does the image. Then you will scroll the table if you drag on it directly or scroll the scroll view if you drag the image or the scroll view.
For the first question, I'm a little confused by the way you ask it: "how do you set up a dynamic-height table view that will grow as cells are added to it." Table views have a function that it calls before the table is fully loaded with data called "numberOfRowsInSection." So the number of cells is based on that function, and should you update the variable used to determine the return value of that function (usually [myArray count]) it should automatically find the right size for the whole table.
However, variable height cells are something that I found kinda tricky and I've solved it using the following:
There are some UIKit NSString additions that you might find useful.
http://developer.apple.com/iphone/library/documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html
Particularly the sizeWithFont: functions.
Table views also have a 'heightForRowAtIndexPath:' function that is called 'numberOfRowsInSection' amount of times. Each call determines the height of the cell at the indexpath.
So, for example: (assuming myArray is an array of NSStrings)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [[myArray objectAtIndex:indexPath.row] sizeWithFont:myFont];}
This will return a height based off of your actual data, piece by piece. There are other functions to specify how the text wraps and truncates, etc. as well.
It doesn't feel like a great solution because you end up fetching your data twice, once to determine the height, and then again when you configure the cell in 'cellForRowAtIndexPath:' However, it does work!
I've learned a lot in the past few weeks and have gone through a few iterations of addressing this problem. My first solution was to manually measure the table height, then set the table rect to display at that height, and finally to set the scrollView's content rect to encompass the the table and top feature. What that solution did basically work, I started encountering some display issues when branching out into new views with different toolbar configurations. It seemed that my manual frame size was interfering with iPhone's native content scaling.
So, I scrapped the manual sizing and went to just making that top feature block be a custom table cell that displayed within its own section at the top of the table. I made a hard logic definition that section 0 only had one table cell, and that cell was my custom layout that I linked in through Interface Builder. I was then able to get rid of ALL my messy custom scaling logic, and the whole system is cleaner, smoother, and works reliably.