How to always stick button and label together - objective-c

I want to have a button and label who always stick together. I want it to be static and I found a solution which uses the UICollectionView with cells. I got the "Connection "" cannot have a prototype object as its destination" and found a solution here: Strange error when adding items to prototype cells in storyboard-IB but the answer given there is talking about a dynamic one. What is the right way to do this? I don't need to have a #property from the cell, I only need to have a #property from the button and label and I just want the label and button to stick together. So actually something like this:

You should make a UIView with a UIButton and a UILabel as its subviews.
If you want to use UITableView or UICollectionView, this parent UIView will actually be the UITableViewCell or UICollectionViewCell.

Related

Replacing NSTextField with NSScrollView to get a scrollbar

My OS X app written in Objective-C needs to display a varying NSString* variable theString which can get quite large.
I use Xcode IB to build a nib file which displays theString in a NSTextField* object panel declared inside AppDelegate.h like this:
#property (weak) IBOutlet NSTextField *panel;
Now I can set the contents of panel inside AppDelegate.m like this:
self.panel.stringValue = theString;
This all works fine. But I now want to give my text field a scrollbar. So in place of a "Text Field" I choose a "Text View" from the Object Library, and get its blue line to generate me a new declaration of panel which now looks like this:
#property (weak) IBOutlet NSScrollView *panel;
This now no longer works:
self.panel.stringValue = theString;
raising the error: (!) Property 'stringValue' not found on object of type 'NSScrollView*'
How do I need to fixup this statement?
(Might I just say I find the extensive Apple documentation on the topic byzantine and opaque. Why am I being naive to expect a simple answer to this simple question, as it all seems to imply? I must be missing something obvious -- what is it?)
What you got was a NSTextView wrapped inside a NSScrollView. The scrollView is the thing that makes the scrollbars you want. It basically holds a potentially much larger view inside its viewport and shows only small part of it, that you can shift around with the scroll bars. You need to get (another) reference from your code to the NSTextView inside the scrollView. You can find the NSTextView in the hierarchy in IB and attach to that.
This is in the direction of what you want but I think not quite what you need. The textView is a far more advanced control than a simple textField and probably more than you need. You could instead use a custom view by taking a NSScrollView that comes with a default NSView wrapped inside. Then instead of the NSTextView place your NSTextField on the NSView. The issue with this is that then you need to add some code to auto-resize the NSTextField and NSView based on the content of the textField. Once you got that sorted the scrollView will automatically arrange for the scrollbars that you need.

UITableViewCell as subview?

I want to reuse views and code
Is it ok to add a UITableViewCell as a subview to a common view and not in UITableView?
Is it a good idea?
I don't think it's a good idea. UITableViewCell is made to serve as a cell and nothing else. Just like UIButton is made to be a clickable button. But you might think "well the button looks good, shouldn't i reuse it just as a subview?". No, you shouldn't do that. Button is a button, cell is a cell and so on. If you want to reuse your code then you should provide a kind of custom UIView and let it be subview for your cell or anything you want

focus on UITextField inside a UICollectionViewCell

I have created a custom UICollectionViewCell that contains a UITextField inside of it which is only accessible when the cell is selected. The CollectionView has multi select enabled and the problem is that when trying to select the UITextField to type in it, the cell is deselected instead of giving the TextField focus.
How do I allow focus on the UITextField inside of the CollectionViewCell without causing the cell to be deselected?
Note: I've also tried adding buttons to the cell template and the button actions are not getting called either. It seems as though the cell itself is capturing all of the touch events and not passing them along to child views.
I'm not sure what the original problem was, but I solved it by re-creating the UICollectionViewCell interface inside the UICollectionView in my storyboard instead of loading it from a different .xib file.

Highlight UITableViewCell on touch event in a composite UIView

I have a UITableViewCell which is made up of a child container UIView and some other child components, such as labels, background, icons, etc...
Each UIImageView has also an highlight image set in Xcode IB.
The cell is assembled in a UIStoryBoard and has a segue connected to it, therefore I am not using the typical didSelect method of delegate.
This is the hierarchy:
UIViewController (serving as UITableView delegate)
--> UIView (main view)
--> UITableView
--> UItableViewCell
--> UIView
--> (several UIView, image, label, etc..)
I wonder how can I have my cell selected in this scenario. It is fine for me to highlight the whole cell, but I am also interested in knowing how to make a specific select.
For select/highlighting I mean, forget the standard blue or grey selection, but rather, recall all those specific images I mark as 'highlighted' in IB.
Platform target is iOS 5.
thanks
Your UIViewController file should implement UITableViewDelegate and UITableViewDataSource
#interface NameOfViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Then you can override the table's methods. It sounds like you want to override didSelectRowAtIndexPath. I'm still a little unclear on exactly what you want to do, but it sounds like you will just use this method and either tell the cell it's highlighted or pass a message to the cell telling it to do something (like change its images). You will also need to subclass your cell and load it from a .xib file if you want to do this. Sorry if this sounds like a lot of work, but table views require a lot of code for customization beyond very basic stuff.

UITableView in a UIScrollView - How to make the view scroll, but not the TableView in itself?

Imagine, there is a UIViewController with a UIScrollView in it. At the top of the view there is an UIImageView, some UILabels and other things. Furthermore, there is a UITableView which content is Dynamic Prototypes. I attach a picture to make it clear:
I haven't got a static amount of cells in the UITableView so it could be scrollable. My problem is the following: the UITableView scrolls in itself but I want to scroll the whole View. What is the best possibility to do that?
Possible solutions I've founded today
1) The first thing is: I create a UITableViewController and declare a header section in which I include all my labels, images etc. programmatically (I would love to use the interface builder for that...)
2) Another solution is to calculate the height of the view. I tried the best to do it like this way - but: without success. If this is the best way to do that: Can anybody give an example?
I would ditch the UIScrollView and just use a UITableView. You can add a UIView object as the tableHeaderView of the UITableView just by dragging it in in Interface Builder. Now since everything is part of the UITableView hierarchy, everything will scroll together as expected.
You could also try setting delaysContentTouches to NO on your scrollView. Depending on your setup, this may make the scroll view respond to the touch first instead of the table view.
From Apples UIScrollView Docs:
delaysContentTouches
A Boolean value that determines whether the scroll view delays the
handling of touch-down gestures.
#property(nonatomic) BOOL delaysContentTouches
Discussion
If the value of this property is YES, the scroll view delays handling
the touch-down gesture until it can determine if scrolling is the
intent. If the value is NO , the scroll view immediately calls
touchesShouldBegin:withEvent:inContentView:. The default
value is YES.
You'll have to (as you've mentioned) add the UIView containing the image and buttons to the actual UITableView. Embedding it in the scroll view will produce the undesired behavior that you're seeing.
I would recommend returning the UIView as the header view for the first section of your table view. You can do this by implementing the UITableViewDelegate method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
If you maintain an IBOutlet to the view containing your image/labels, you can return it here.
this is same demo i hope its helps you from iphone sorce code library
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html
thank you