Wire components to the custom view inside a UITableViewCell - ios

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.

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

UITableViewCell subclass layout messed up in autoLayout-disabled Storyboard

I have created a table view with prototype cells in a Storyboard with the "Use Autolayout" unchecked in Xcode 5.
The cells are UITableViewCell subclasses, mainly to add IBOutlets and no code inside. The result is a messed layout. I tried changing the Autoresizing masks with no luck. Also tried this.
If I however implement an empty layoutSubviews it shows ok. Any idea of what's going on?
Is auto layout still enabled despite unchecking it?
Edit:
More details...
#interface SettingDefaultTableViewCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *label;
#property (nonatomic, weak) IBOutlet UIImageView *imageView;
#property (nonatomic, weak) IBOutlet UIView *backgroundView;
#end
#implementation SettingDefaultTableViewCell
- (void)layoutSubviews
{
// Emtpy implementation to fix weird layout bug
}
#end
Storyboard with Autolayout disabled:
Result when layoutSubviews fix above is not used:
Edit 2:
The cell is set to not autoresize subviews.
While the above should be enough to prevent subviews from getting auto-adjusted, also all autoresizing masks are set to flexible right and bottom margin only.
Only the > mark is set to flexible left margin. No flexible width or height.
The cell is getting modified by the undocumented UITableViewCell's layoutSubviews:
Despite set not no autoresize subviews, the default implementation changes the backgroundView frame.
It also moves imageView (the ">") to the left despite being configured as a Custom Style cell.
Thus avoiding executing super's layoutSubviews fixes it.

How to retrieve pointer to one of multiple UIViews inside a custom TableViewCell?

I have created a custom UITableViewCell in Interface Builder and also created a class for it, and loaded it into my ViewController by registering its NIB.
Inside the custom cell I have an UIImageView, a UILabel and two IBOutlets for each of them in my customViewCell.h:
#property (strong, nonatomic) IBOutlet UIImageView *assignmentImageView;
#property (strong, nonatomic) IBOutlet UILabel *testLabel;
Now in the the cellForRowAtIndexPath method, I can write to the label outlet with:
cell.testLabel = #"text"
But I am unable to set the image property of the cell.assignmentImageView to some image I want to load. So the following doesn't do anything:
cell.assignmentImageView.image = [UIImage imageNamed:#"my.png"];
I also noticed that the completion feature of xcode doesn't show assignmentImageView as a property of cell, but it does show testLabel. So that leads me to believe that even though assignmentImageView has been set as an IBOutlet, it is for some reason not considered as such.
I tried also using the tag property of the UIImageView and retrieve a pointer to the UIImage in the cell using the viewWithTag method, but that didn't work either.
I know that if I have only a single UIImage in the cell, I can reference it with cell.imageView, which is strange to me, but I want to be able to have multiple UIImageView in the cell and reference them.
Can anyone explain me how to do this?
Thanks
-Malena
Check of your assignmentImageView outlet is wired with a correct view in XIB.
Check if the image you are trying to set is added to your project.
Set breakpoints or use NSAssert or NSParameterAssert statements to verify that UIImage and the assignmentImageView are not nil.

Add subview to specific view

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

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