Proper way to build custom UITableViewCell - objective-c

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.

Related

iOS custom UITableView and UITableViewCell with columns (Twitter app)

I'm developing an iPhone/iPod application, and the designer used a lot of these "Twitter App Concepts" (screenshot). As you can see, the TableView has a shadow and the cells can be split in columns (those columns are clickable).
I just need to create those columns, any idea how I can accomplish that?
The prettykit library is probably a good place to start, as the library is a subclass of UITableViewCell and UINavigationController.
http://cocoacontrols.com/platforms/ios/controls/prettykit
Well a UITableViewCell is a UIView so in your tableView:cellForRowAtIndexPath: when you hit that row simply add 3 subviews to the UITableViewCell.
There is one downside to this approach though and that is if there are a lot of these "Column Cells" then it will hinder performance. You also tend to want to avoid more then 5 subviews in a UITableViewCell
In case you are wondering "Why can't i just add multiple cells to a Single Row?"
Good question and the reason is UITableView's dequeueReusableCellWithIdentifier: (Reference) this takes an Index Path which is a combination of the Section Number and Row Number the Cell is in.
As it only returns a single cel, it's impossible to return multiple cells (unless you write a custom implementation), but you can return a cell with multiple subviews that has a unique identifier ;)
UITableViewCell Class Reference
UIView Class Reference
Edit: The library that danielbeard linked looks to be a good implementation to use.
Use your own subclass of UITableViewCell instead of UItableviewcell. And Customise your cell as you want.

Get UITableView headers/footers

Ok, so I have this issue where I need to get access to the headers/footers displayed in a UITableView. Moreover, I need to do this from a subclass of UITableView (so I can't simply assign tags to the view from the UITableView Delegate). UITableView does keep an array of visible headers and footers but it provides no access to those arrays even to the subclass, which I personally think is asinine.
I need this so that I can provide a custom drag-n'-drop insertion/move user interface. I'm trying to get it to work almost exactly like Apple's own rearranging interface, but of course, with my own custom implementation (like the ability to drag from another table). Everything works perfectly except for the headers/footers.
At the moment I'm trying to hack it by iterating through all the subviews of UITableView. I'm pretty sure that the only subviews of UITableView is: backgroundView, UITableViewCells, and Headers/Footers. So I just need to determine which are UITableViewCells (very easy), which is the background view (also easy) and which are headers/footers (not so easy). I'm pretty sure I can do it this way but it's definitely a hack and I'd rather find a better way if possible.
UPDATE
I'm overriding the - (void) setDelegate:(id<UITableViewDelegate>)delegate method and checking to see if the delegate responds to the appropriate selectors to generate headers/footers (and set BOOL's appropriately). This is making it a lot easier to determine what a particular subview is since the progression of header -> cells -> footer is predictable.
You say you can't use UITableView delegate methods, but did you consider letting the UITableView subclass object be its own delegate? Create a separate property on the subclass called realDelegate and pass any other delegate calls through to that.

How to show a .xib file in a TableViewCell

I have a .xib with an image and a label in the potition that i want..
And i want in the cellForRowAtIndexPath function to set image and label.text then to add then view in the cell and show the cell
Check this:
How do you load custom UITableViewCells from Xib files?
But i do not recommend to load UITableViewCells from xib.
You loose performance on a very critical part.
I recommend to create you UITableViewCell subclasses by code. Because inflating a XIB(XML) during scrolling will create buckings.
If you wan't to have a nice, snappy app, then code instead of XIB (at least the UITableViewCell subclasses).

What is the alternative of visible true/false for views in cocoa-touch?

I'm customizing a tableviewcell which is still very hard to me.
After reading apple's guide, I'm customizing my cells by loading them from a nib file, and managing them programmatically.
so instead of creating two different types of nib files (tableviewcells) one with textfield, and another with pickerview.
I'm doing a hybrid cell. i.e. In my nib file, i'm adding a UILabel, UItextField, UIPickerView.
Now on the 'cellForRowAtIndexPath' for the tableview (after i've bound each cell to the nibfile)
i want to be able to programmatically use the pickerView OR the textfield.
Now there's no 'visible' property to use in order to show one and hides the other.
So what's the best approach here.
I'm a bit new to cocoa , and the framework is not giving me many options.
So for example if indexpath is 0.1 i want it to be a pickerViewCell.
and if indexPath is 0.2 , i want it to be a textfieldCell.
So if indexpath is 0.1? what should i do?
should i dispose of the textfield? change its coordinates to somewhere that can never be viewable? disable it? what?
and if my approach is wrong and not recommended, what is the recommended way?
should i go back and remove the pickerView from the nib, and create another nib file with only label and pickerview? and upon loading decide which one to load?
Thanks
View visibility is controlled by hidden property (that is - the opposite of visible) - you can use it in your case

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.