UITableViewController and custom UITableViewCell - objective-c

Have couple of questions
When I place a UITableViewController on the storyboard, and wish to work with static cells must I create a class deriving from UITableViewController or can I only create a class deriving from UITableViewCell for the custom cell?
I created a custom UITableViewCell placed number of controls on it and tried dragging with control pressed to create an outlet but couldn't, any ideas why?
If I wish each cell to have different controls and behavior can I set each cell a different custom UITableViewCell and if so how does the UITableViewController initialize it? do I need to load it programmatically?

1) The cell must be a subclass of UITableViewCell.
2) No, I don't know why that didn't work, it should. Where were you trying to make the connection to? The cell or the table view controller?
3) Yes, I think this is the best way to do it. Have a different custom cell class for each different kind of cell, and make IBOutlets from the custom cell to its controls. Then just make one IBOutlet from the table view controller to the cell itself (you can then refer to the controls with something like self.cellType1.label1 ...). You don't need to do anything to initialize the cells if they're made in the storyboard.

Related

Use single UITableViewCell subclass across multiple UIViewControllers

So I have this UITableViewCell subclass that has some really complicated logics in it - it triggers some actions in UIViewController it's actually attached to. Of course the cell is not aware of its UIViewController but I still navigate to it like this:
UITabBarController *tabVC = (UITabBarController *)appDelegate.rootVC.centerPanel;
SGFirstTabViewController *firstTab1 = [tabVC.viewControllers firstObject];
[firstTab1 reloadCell:self];
The thing is now that I want to use the same subclass of UITableViewCell around about 5 different UIViewControllers.
What's the best way to do this? I will almost never know what is the UITableViewCell's VC is and I simply can't create 5 different cell subclasses with the same code over and over. What's the best way around it?
I think there is perhaps some misunderstanding in the role of the cell in the model-view-controller programming pattern.
The cell should not reload itself but it should be told to reload by the view controller that controls it. The reload code in the cell can stay the same.
So, rather than the cell having to find out which one its view controller is, have the view controller listen to the cell action (e.g. via delegate methods) and fill it with the appropriate reload data as directed.

Change selection / highlight color of static uitableview

How can I change the selection / highlight color of a static UITableView? I understand for non-static UITableViews I might just subclass a UITableViewCell, but since I can't use cellForRowAtIndexPath with a static UITableView what should I do?
When using static cells from storyboard, you can customize each cell dynamically in tableView:willDisplayCell:forRowAtIndexPath:.
You can customise each cell in Interface Builder. You can set each cell's attributes by selecting it and then editing the appropriate fields in the attributes editors. For even more customised appearance/behaviour, you can even subclass a cell and let Interface Builder of its class in the identity inspector.

Proper way to build custom UITableViewCell

I've been wondering about UITableView's and their cells for a while. UITableView is a very handy UIView subclass, but there is a lot of predetermined/forced content on a UITableViewCell. I was wondering what is the best way to create a custom UIView (which will end up being a UITableViewCell) for UITableView?
The cell has a certain style that has to be set and there are predetermined UILabels and accessory views that are completely immutable, other than their contents. I find this to be a big waste, but just giving the cell a custom content view (and background view, if one pleases) doesn't prevent or remove these processes or restore the memory.
Is there any way to create a lighter version of a UITableViewCell subclass? Or is there a way to use a UIView with a selection method instead (other than essentially creating a custom UITableView using UIScrollView)?
Any suggestions would be really appreciated.
There are a few methods:
From a XIB file:
ios steps to create custom UITableViewCell with xib file
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
Using Drawing:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007318 (4th and 5th examples)
There doesn't seem to be an answer for this. UITableViewCells will always have UILabels and other views automatically added to them. I'm not sure if they are allocated before you set them, though. They may have custom setting / allocation methods.

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.

Is there any way to place two UISearchBar in UITableView?

I have a UITableView with customized UITableViewCells, now i have already placed one UISearchBar to find one of the data in UITableViewCell and it is working fine.
Now i want to do search operation for the other data which is placed in UITableViewCell, so is there a way to place another UISearchBar programmatically?
Yes. It's totally flexible. You just need to create the cell and you can control the placement using the UITableViewDataSource/Delegate methods.