iPhone4 iOS5 custom history UITableViewCell [image included] is it possible? - objective-c

I'm trying to add a way for my app to display data in an easily comprehensible form. I tried doing this with graphs, but found it to be a bit awkward, so I'm off to find a better solution.
Here's a problem:
I want to show up to 30 days of data, with user reported events plotted against a timeline.
The events should align vertically, to visually provide a reference of when certain events cluster together.
To solve the problem, I thought of using a UITableView with a background which has a scale, and superimpose upon that background a set of "pins", indicating user events. As the table is scrolled, the events would align one under another, providing a good indication historic event development:
I know that this is possible to be done with a UIScrollView, where I can just position a series of rulers and add events as pins. In order to do this efficiently, I'll have to use Tiling, a technique that I don't have much experience with.
I'm interested in knowing if this is possible with a UITableView, where caching and tiling is already done for me.
I tried adding UIImageViews to a tableViewCell.backgroundview , but this does not produce the desired results. I'd like to be able to dynamically add any number of subviews to the table cell. I can do this by adding a certain pre-determined number of images to an .xib and then trying to reposition them, but this does not seem like an ideal solution.
Is what I'm trying to do possible with UITableView, or should I abandon these efforts and look at UIScrollView with tiling?
Thank you!

yes this should be possible. Add the ruler image to the background of the UITableViewCell.
Then create a subclass of UITableViewCell that takes an array of pins objects that it will place within its view. The pin objects would contain the pin image and a horizontal location on the scale. Then in the tableView dataSource method cellForRowAtIndexPath method you set the tableViewCell's pin array to the correct array (or NSSet) of pins.
To make it even more lightweight, you could draw the pins yourself in the UITableViewCell subclasses drawRect method. Then you could just pass the locations of the pins.
Good luck

Related

Gesture recognizer for tap event at specific positions on the screen

I am displaying hundreds of thumbnails in my view . I know default way to handle tap on thumbnail is using UICollectionView delegate method "didSelectItemAtIndexPath" but since its many thumbnails i wanted to look into adding gestures to the screen position so when i tap on a particular spot on the screen, it will handle the event accordingly for that particular thumbnail underneath. I would like to know if it is a good/possible approach?
It would be a hell of a lot easier to use a UICollectionView.
If you need a custom layout then you can subclass UICollectionViewLayout and get some really cool dynamic layouts.
You also get the added bonus of dequeued cells meaning that you get better memory management using it.
You may find UIGestureRecognizer useful. A good tutorial to get you started is here.

Too many UIViews causes lag

I am creating an app for practice that is a simple drawing app. The user drags his/her finger along the screen and it colors in a 100px x 100px square.
I currently achieve this by creating a new colored UIView where the user taps, and that is working. But, after a little time coloring in, there is substantial lag, which I believe is down to there being too many UIViews as a subview of the main view.
How can I, and others who similarly create UIViews on dragging a finger reduce the lag to none at all, no matter how many UIViews there are. I also think that perhaps this is an impossible task, so how else can someone like me color a cube of the size stated above in the main view on a finger dragged along the screen?
I know that this may seem like a specific question, but I believe that it could help others understand how to reduce lag if there are a very large amount of UIViews where a less performance reducing option is available.
One approach is to draw each square into an image and display that image, rather than keeping around an UIView for each square.
If your drawing is simple enough, though, you can use OpenGL to do this, which is much faster. You should look at Apple's GL Paint Sample Code which shows how to do this in OpenGL.
If your drawing is too complex for OpenGL, you could create, for example, a CGBitmapContext, and draw each square into that context when the user drags their finger. Whenever you draw a new square into that bitmap, you can turn the bitmap into an image (via CGBitmapConxtextCreateImage) and display that image an a UIImageView.
There are two things that come to my mind:
1- Use Instruments tool to check if you are leaking any memory
2- If you are just coloring the views than instead of creating images for each of them, either set the background color property of UIView or override the drawRect method to do custom drawing
I think what you are looking for is the drawRect: method of UIView. You could create your custom UIView (you propably have that already) and override the drawRect method and do your drawing there! You will have to save your drawings in an array or another container and call the setNeedsDisplay method whenever the array content is changed.

How to draw in code an imageview

I have a problem. I want for each record in my core data database to draw an UIImage view on screen. But the problem is that I want to make a sort of grid. On the link below you see what I want to achieve.
picture
So my question is, how do I draw an image on screen in code. And place those images in a sort of a grid. using a collection view is no option, because the app should be running on all IOS devices.
While you could implement a custom UIView and implement the drawRect: method and draw UIImages there, I suggest just using multiple UIImageViews as subviews on your "main" view. Your view might be embedded in a UIScrollView, or you could use a UITableView with custom UITableViewCells. Whichever is easier is probably related to how you can interact with the view.
Building that one huge image view is something that I'd definitely try to avoid - it costs many many (probably unnecessary) memory, and it might be slow as well. Definitely not very flexible to handle, and a pain to update dynamically.
A quick cheat for something like this is to use a Table View and then in each cell to place another TableView but rotated at 90 degrees.
You can then use this second TableView to display the pictures etc...
This will give you a table that scrolls up and down and then each cell can scroll left to right.
I'd suggest subclassing UITableViewCell and setting it up as a UITableViewDelegate and UITableViewDatasource.
You will also have to remember to rotate the content of these "sub"tables by 90 degrees also so that they are the right way up.
This sounds like a lot of work but if you push the management of the sub Tables into the cells then it actually becomes quite easy.

How can I place a small image in my UIView

I want to place a series of images in a UIView... I want to be able to change each image's color using three different .png files (red,yellow,green) as a status indicator. Other than actually drawing the rectangle and filling it, is there an easier way?
Research effort: I have Googled and looked through SO and found nothing dealing with this.
There are lots of ways you can do this. I think the easiest way would be to pragmatically add UIViews as subviews to your main view, or to your parent UIView.
You can set the frame of your views as well as the background color. See this link
adding uiview as a subview to the main view
You can also use images, but since it is a plain solid color, its a lot of extra storage space, etc. to use images when you can programatically render a color.
If it is a series of rectangles, you can store UIViews in some kind of data structure so it is easier to dynamically change the color at runtime. If you aren't moving them around, then init there frames/geometry so they are in the correct location, then access them as members of an array or something similar.
If you want to add gradients to images, this is the best thing I have found:
Gradients on UIView and UILabels On iPhone
I figured it out... I took a UIButton, changed the Type to "Custom", sized it (32 x 32) and programmatically changed the the image depending on circumstances.
[button setImage:<#(UIImage *)#> forState:<#(UIControlState)#>]
Thanks to both of you for getting the thought process running... :D

Lazy-loading Custom UIScrollView with dynamic grid size?

So I wrote a custom subclass of UIScrollView that basically displays rectangles in a 2-column grid, but while the width of each is fixed to half the width of the iPhone/iPad screen, the height varies, depending on the dimensions of the picture within it. All's well and good now, but I can imagine that after adding lots of these subviews to my UIScrollView subclass, things are going to lag. So I'm trying to implement lazy loading analogous to UITableView's dequeueing reusable cell method.
I've looked at a handful of other questions on SO, but they all involve paging based on full-screen photos, including Apple's WWDDC PhotoScroller example.
Anyone have any insight? Thanks.
You basically want to keep track of the views that are scrolled off screen and reuse them.
You might want to check out ATArrayView. It implements a recycling mechanism like what the UITableView has except with a UIScrollView with an arbitrary number of rows and columns?
I've used it as the basis for my own image scrolling code and is pretty good. It's delegate methods follow the spirit of UITableView data source and delegate protocols.
Good luck
Tim