How to set Accessoryview to static UITableViewCell? - ios

I have created a UITableViewController with a UITableView and static UITableViewCells.
How can I change the Accessory View to a custom Image within a UIImageView?
I know how to change the Accessory View generally:
UIImage *accessor = [UIImage imageNamed:#"imageName"];
[somecell setAccessoryView:[[UIImageView alloc] initWithImage: accessor]];
The solution:
Create an IBOutlet for each UITableViewCell which should have a custom accessory view and connect the IBOutlet to the UITableViewCell in the storyboard. After that you can set the accessory view like above.

I found a better solution:
Drag a view (for example a instance of UISwitch) into UITableViewController in storyboard
Selet the cell on which you want to add a custom accessory view. Then open the Connections inspector, drag the accessoryView in section Outlets to the view that you created in step 1.
Now run the app, see a custom accessory view appearing in a static UITableViewCell. Of course you can create another IBOutlet between the UISwitch and controller so that you could get the reference of it, or create an IBAction for receiving action when user change the value of UISwitch.

Add a custom UIButton in the storyboard and set the image you want to it

Related

Refactor UIButton to UILabel in storyboard

I've a view in my storyboard with a lot of UIButton with constraints.
Is there a way to transform these buttons to labels without removing each button and create a label with constraints?
UIButton actually contains a UILabel for displaying its titleLabel. So You can set the button UserInteractionEnabled: property to NO.
UIButton may have border color. So set the border color as clear color.
Make the UIButton as custom type.
As you need to reconfigure the constrains again if you added the labels, you can go for the above solution.
Reason for couldn't change in IB/Storyboard:
You have dragged the UIButton from the library to your view. Even you try to change the class name to UILabel or its subclass it won't change the class name.
because UILabel is just a UIView type, Where as UIButton is UIControl type.
That's why you couldn't change the UIButton class name to UILabel or Subclass of UILabel in IB.
Updates:
No Guarantee/Not Recommended on upcoming XCode release

Reusing a UITableView footer view in multiple views

I have a footer for a UITableView which is a "complex" view (textView and a button).
I made a xib and added it to the footer of the UITableView.
The next stage is having referencing outlets in the ViewController of the UITableView
The thing is that I want to use this footer view in several ViewControllers (and not DRYing)
What can I do?
It sounds like you want to have a xib/.h/.m separate from the .xib used by your UIViewController. This is for your View that will be used as the footer of the UITableView. You can [[MyFooterView alloc] init] in the viewDidLoad of any controllers that are using a UITableView and assign the footer to that instance of MyFooterView. Is this what you're after? You can still have referencing outlets to the UITableView in any UIViewController, and keep a reference to MyFooterView that you instantiated in viewDidLoad
addTarget example:
myFooterView.button addTarget:self action:myCustomHandlerFunction forControlEvents: UIControlEventTouchUpInside

Can't place UIImageView behind UITableView in storyboard

I can't seem to position a UIImageView behind a UITableView on my Table View Controller. I'm trying to do this within the storyboard designer. Moving a UIImageView onto the View Controller just stacks the imageview onto the tableview. In previous versions of XCode I had a window that allowed me to change the stack order of views within a view controller. Is this still possible?? I've tried dragging the views around in the jump bar and this doesn't work. So how do I accomplish this using XCode 5?
Thanks!
Use UIViewController for parent of UITableView!
Or if you want to set Background of tableView, can use this code
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"bg"]];
UITableView has a backgroundView property. You will need to set it in code.
In your example, you're probably using a UITableViewController in your storyboard. This means that the root view is a UITableView and you can't add a UIImageView as a subview to a UITableView and put it behind the superview. Doesn't make sense.
In a previous version that you've made, I suspect that you had a UIViewController with a UIView root view, which you added a UIImageView subview and a UITableView subview on top of that.
Every UIViewController is associated with a UIView, i.e. [viewController view]. In the case of UITableView, [viewController view] is of type UITableView, so when you add subviews to it, you're adding to the UITableView, not a superview of type UIView as you seem to expect.
I would recommend using a UIViewController to control that view, adding subviews of types UIImageView and UITableView.

tableView initwithframe and storyboard

I'm trying to add a tableview as subview to my tableViewController, but I want to setup the cells in storyboard. It will be a static tableview.
This is the code for calling the tableview on button click.
- (IBAction)botaoAdicionar:(id)sender {
AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(0, 170, 320, 320) style:UITableViewStylePlain];
[self.view addSubview:tableview];
}
In the tableview class I have this:
#implementation AtividadesPraticadasTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Now, I have a viewcontrollerin storyboard with a tableview, which the class of the tableviewI changed to this file AtividadesPraticadasTableView. It has three static custom cells in storyboard, therefore it opens a blank default tableview.
What am I missing?
Static table views are entirely contained within the storyboard, and require information from the storyboard to display their content.
You've defined a static table view controller in the storyboard, populated it and set the tableView's custom class to your custom class, but when you want to add the table view you are just instantiating a table view of that class. That isn't going to get any of the information you've added to the storyboard.
In addition, the static cells information is read and understood by the UITableViewController, not the UITableView, so you are at the wrong level there too.
You need to do the following:
Get a reference to the storyboard, either from your original view controller's storyboard property (if it is on the same storyboard as your static table) or using storyboardWithName:bundle:.
instantiate the table view controller from that storyboard, using instantiateViewControllerWithIdentifier:. This will create a table view controller object containing all your static cells
Add this as a child view controller to your original view controller, using addChildViewController:
Add the table view controller's tableView as a subview
It may be simpler to add a container view in the storyboard to hold this view, and reveal it when the button is pressed, as Mike Slutsky suggested - this will do all of the instantiating and adding and child view controller-ing work for you, but the principle is still the same.
Also, adding a table view as a subview to a table view controller sounds very dodgy - a table view controller already has a table view as its view, and you can't really add subviews to that.
The thing your missing is the association between the programatically instantiated tableview and the UITableView that you put in your storyboard. You cannot just draw UITableViews in your storyboard and start instantiating new UITableViews in the controller's code and expect xcode to know which UITableView you wanted to use from the storyboard. Use an IBOutlet to connect a global variable for the controller to the UITableView in the storyboard, then the controller will know what you're trying to refer to. If you want that UITableView to appear on a button click, simply set the UITableView to hidden by default in the storyboard and then unhide it when the button is pressed.
The thing You are missing called manual. Check this protocol for TableView dataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource
P.S. Here good tutorial for storyboards http://maniacdev.com/ios-5-sdk-tutorial-and-guide/xcode-4-storyboard

How to instantiate a Custom UIButton in UITableViewCell

I have a UITableViewCell and my cells contain a UIButton subclass (CoolButton).
I have picked the code of CoolButton from raywenderlich tutorial
available here
In the IB I have created a subclass of the UITableViewCell and I have configured the class of my button to CoolButton and set the property type to Custom and configured the IBOutlet for the CoolButton to my UITableView subclass.
My problem is when cells are created, they get a UIButton instead of a CoolButton. As a consequence when I'm setting the properties of the CoolButton I get a crash with "unrecognized selector".
Why UIButton is instantiated instead of a CoolButton? How can I get my CoolButton in the table?
Thanks,
Séb.
I'm going to guess you are doing something like:
CoolButton *b = [UIButton ...]
You need to create it using the CoolButton class:
CoolButton *b = [CoolButton ....];
Or, if you used IB, that you created a UIButton but used an IBOutlet of CoolButton. You would need to make the actual class of the button you drag into IB CoolButton, which you can do by selecting the button and tapping (I think) the second to the left top tab in the inspector.

Resources