Add subview to specific view - ios

I have a static table, one cell has multiple views that I would like to add subviews. Each view has a class (MHRotaryKnob) assigned.
What I can't figure out is how to ID each view in the cell so I can addSubView.
I have set up the table within StoryBoard.

Give your Viewcontroller an IBOutlet-Property for a UIView like so:
#property (nonatomic, strong) IBOutlet UIView *myView;
then connect those to your desired view in storyboards.
now you can address this view in code like this:
[self.myView addSubview:subView];
Thats probably easier than messing with ids or tags.
If you have problems, this will help you:
http://klanguedoc.hubpages.com/hub/IOS-5-A-Beginners-Guide-to-Storyboard-Connection
EDIT:
If you want ta add a subview to a MHRotaryKnob, you can just go the other way around:
#property (nonatomic, strong) IBOutlet MHRotaryKnob *myKnob;
and then, assuming that MHRotaryKnob is a direct or indirect subclass of UIView, you can add the subview:
[self.myKnob addSubview:subView];

Related

UITableViewCell label text not shown

I have one question regarding UITableViewCell.
I have base class inheriting from UITableViewCell.
In that class I have this:
#property (strong, nonatomic) IBOutlet UILabel *name;
Then I have several cells inheriting from that base class created using xib files.
Now I am adding new cell but in this case I am doing it programatically. My problem is that when I try to set the text for that new cell like this:
[self.name setText:#"My Label"];
Text is not displayed.
How can I fix this?
Thanks and regards

Drag/Drop between 3 ScrollViews [ iOS ]

Is it possible to have 3 scroll views lets say the middle is the main where is user find objects inside, then user can drag any item and drop it to the right scroll view or left scroll view so that's object will be removed from the middle and dropped either right or left depends on the uses and suppose the user dropped it to the left thus system should add new object to the left scroll view but when the user remove this object and return back to the main (middle) preferred to dropped and added in the same old index if the object was number 2 then the object will added and dropped at index 2 ..etc.
Could you help on this to make this functionality ?
Let's call the items model1 ... model3;
#property (nonatomic, strong) NSMutableArray *mainArray;
#property (nonatomic, strong) NSMutableArray *leftArray;
#property (nonatomic, strong) NSMutableArray *rightArray;
#property (nonatomic, strong) UIScrollView *mainView;
#property (nonatomic, strong) UIScrollView *leftView;
#property (nonatomic, strong) UIScrollView *rightView;
You need make a model class such as ItemModel inherit UIView or NSObject, in ItemModel.h make some property(what property you need and a property about which UIScrollView contains this model)
First, put all models in the mainView's datasourcemainArray, display models in your self.view and call self.view bringSubviewToFront: and give all models a UIPanGestureRecognizer and a method to respond to UIPanGestureRecognizer so you can drag them.
This is the most important part:
Because you want all models to be contained by three UIViews, you must calculate the models frame between self.view.frame and the three UIView frames relationship. In fact, all modelViews is contained by self.view, but when visually displayed, all modelViews is contained within UIScrollViews.
Second, you already have the frame of three UIView, when you drag model, you can get CGPoint about the location of the drag, then call the method CGRectContainsPoint(CGRect rect, CGPoint point), so you can get any view that contains the point you drag.
For exexample, you drag model1 from mainView to leftView, you can get a YES from CGRectContainsPoint(leftview.frame, point), then remove the model1 from mainArray and put model1 in leftArray, meanwhile you need to re-layout the three UIViews.
here is my share URL for the gif:https://pan.baidu.com/s/1slFBva9

Accessing custom tableView cell property outside of tableview

I have a custom tableViewCell class that has a UIImageView as an IBOutlet.
#property (nonatomic, weak) IBOutlet UIImageView * waveformImageView;
I also have two view controllers: one with a tableView and one without a tableView.
I need to know the height and width of this UIImageView to send to another class to do a calculation in my NON tableView view controller
I tried doing the following in my header file of my view controller (view controller without a tableView)
#property (nonatomic, strong) OSAudioTableCell * tableCell;
and then accessing the UIImageView property by doing the following in one of my methods:
self.tableCell = [[OSAudioTableCell alloc] init];
[self.waveView setSize:self.tableCell.waveformImageView.bounds.size];
This is all done in my view controller that doesn't contain a tableView. I'm pretty sure I'm doing this wrong since the width and height are coming out as zero. I can get these values fine within my view controller which contains my tableView.

Wire components to the custom view inside a UITableViewCell

Came up this question and did some quick experiments without no luck.
Basically, I made a simple single view project where the top view controller is a UITableViewController. For simplicity, I set the table view content to be "Static Cells". The table cell was a custom subclass of UITableViewCell, like this
#interface TopTableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *label;
#property (strong, nonatomic) IBOutlet TableCellBottomView *bottomView;
#end
Both the properties were wired through control dragging. The TableCellBottomView is just a custom subclass of UIView like this
#interface TableCellBottomView : UIView
#end
Now I add a label inside this TableCellBottomView like the following picture showing
Can I wire this bottom label inside to my TableCellBottomView? Control dragging did not work for me here. I certainly could have added it programmatically inside TableCellBottomView.m. But if i could wire it here, it would be quite convenient, since I could also add a lot of other components and arrange them visually. Thanks!
You may set a tag for the label in Xcode and fetch the UILabel based on the tag wherever you need it:
If you use dynamic cells, you can do this in tableView:cellForRowAtIndexPath:. Alternatively wire the cell to a property and then use that property to fetch it:
((UILabel*)[cell viewWithTag:1]).text = #"Some text";
I don't know why Xcode won't allow you to drag from your label to the bottom view .h file, but you can do it another way. Add the IBOutlet property to the .h file, then drag from the littler open circle to the left of the #property to your label in the storyboard, and that should work.

UITableViewController -UISearchBar should not scroll with tableview when in tableViews.tableHeaderView. as Note.app

I have got similar issue with UITableView with a UISearchBar using storyborad as the
tableViews.tableHeaderView. [self.tableView setContentOffset:CGPointMake(0,40)];
worked for me too. But the problem is,I don't want UISearchBar to scroll along with tableview as in Note.app. Can anyone suggest me how to proceed with this?
Don't use a header view. Instead, add the search bar as a subview as the table view superview so it's a sibling.
If you're using UITableViewController, change to use UIViewController instead. The only things you need to do are:
Change the superclass
Add the delegate and datasource protocol definitions to the #interface
Change the XIB so the table view is a subview of a normal view
Add a property #property (weak, nonatomic) IBOutlet UITableView *tableView and connect it in the XIB

Resources