How to add a view on cell.contentview of uitableview iphone sdk - iphone

In my application I'm creating a .xib of a class and loading that .xib on cells of uitableview of another class. I want to add this .xib to cell.contentView and not cell. How shall I do that?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = #"testTableCell";
testTableCell * cell = (testTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"testTableCell" owner:self options:nil];
cell = tCell;
}
return cell;
}
testTableCell is my another class in which I have two labels and two buttons, of which I have created a .xib and loading it to tableview's cell as above.
tCell is an object of class testTableCell.
This' really urgent.
Can anybody please help?
Thanks in advance.

Well, if you are asking about how to add your custom cells into your UITableView, and not use default UITableViewCell, than its not very difficult. Just follow all the steps described in the following tutorial:
Creating Custom UITableViewCell using Interface Builder

Related

how to show data like grid view in ipad application

Is there any way to show data in the grid view form in ipad application
I want to show the data in grid view in ipad application i have searched a lot some say use custom table view but it is not looking i want full professional i found iOS DataGrid TableView for ipad.
Is it ok to use wether it is free or not. And how to implement this if it possible or not.
Below is the link of iOS Grid
http://www.binpress.com/app/ios-data-grid-table-view/586
How can you not have stumbled upon AQGridView.
However if you are just interested in styling a cell only, you can implement a custom uitableview cell which has few grids and style them according to your requirement.
There are many tutorials out there showing how to customize a UITableViewCell.
Create custom UITableViewCell as per your requirement and load that cell to table view cell.
You can use below sample code to load custom cell with in table's cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *sCellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
if (cell == nil) {
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed: #"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObject) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (CustomCell *)currentObject;
break;
}
}
}
cell.label1.text = #"label1"; // you can access all controllers of your custom cell like this way
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
By Using the CustomUITableViewCell you can get the same.i.e Take the UITableviewcell and add the three or four labels to it.Then Pass the data in cellForRowAtIndexPath method.
I used GMGridview, it has an example and basically if you change the cell size, and columns you can make it fit in iPad or iPhone. You can also add UITextField (instead of the default UILabel), if you need to input data. One nice feature is it can scroll.
http://www.gmoledina.ca/projects/gmgridview/
Feel free to ask if you have any questions.
We use this one in our application : http://www.ioscomponents.com/. We like it so far.

Using UITableViewCell in a UITableView

I'm a little confused with something. I'm trying to create a custom cell and I want to use the interface builder way.
The normal way I create a table is to have the table as:
.h
#interface AssessList : UIViewController {
IBOutlet UITableView *tblAssessList;
}
#property(nonatomic, retain) UITableView *tblAssessList;
#end
.m
- (NSInteger)
numberOfSectionsInTableView:(UITableView *)tableView {
return groupArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return totalArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"I am the text....";
return cell;
}
Now i've created a new class for the cell and I think I understand how to put that in. But can I leave the .h as
#interface AssessList : UIViewController
or does the class/nib with the full table on it have to be a UITableViewController?
Tom
does the class/nib with the full table on it have to be a
UITableViewController?
No. A UITableViewController is just a convenience UIViewController subclass which has a UITableView and is already setup as its delegate/datasource (it is declared as conforming to the UITableViewDelegate and UITableViewDatasource protocols), it also has pre-filled method implementations for these protocols in the template implementation file which Xcode generates for you. You can just as well do all of this yourself, I often do.
You should however make an IBOutlet for your UITableViewCell so that you can load it from the nib file (see the Loading Custom Table-View Cells From Nib Files in the Table View Programming Guide).
If you want to do in the Interface Builder Way, then create an xib (view xib). Drag and drop a UITableViewCell object from the obj palette. Customize it as you wish. In the tableView:cellForRowAtIndexPath: method, do this:
UITableViewCell * aCell = [tableview dequeueReusableCellWithIdentifier:#"SomeIdentifier"];
if (aCell == nil)
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:#"CustomCellNibName" owner:self options:nil];
for (NSObject *anObj in arr) {
if([anObj isKindOfClass:[UITableViewCell class]]) {
aCell = (UITableViewCell *)anObj;
}
}
}
The identifier for the tableviewcell can be set in the IB.
I guess it should be sub class of UItableViewCell
i.e.
#interface AssessList : UITableViewCell
When you want a custom tableview cell you will also need a subclass of UITableViewCell..
A tutorial can be found on this blog
Keep in mind that quite a few things can be done without creating a custom cell, this includes adding the switch to make your tableview look like the one from settings.app, to the way the iPod displays songs.
In the assessList Class you are using the custom cell created in otherviewController (UITableViewCell subclass) so there is no need to change this line
#interface AssessList : UIViewController
Note:- the otherviewController should be a subclass of UITableViewCell

Grouped UITableView and cell design problems

i've designed a UITableView in an app that i'm developing
The view is designed in interface builder and table style is set to grouped there
But i'm facing a little problem in table design.
As You can see in the screenshot below the first separator is bolder than normal borders and as you can note in the second screenshot the separator in the last sections doesn't appear at all.
As because there's no problem if table have only two sections and not three i'm thinking that there may be a problem in dequeuereusableCellWithIdentifier, but i can't manage to resolve the issue.
Table Cell is a subclass of UITableViewCell designed in interface builder and loaded by using this code
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"GraphiDetailTableCell" owner:self options:nil];
cell = (GraphiDetailTableCell *)[topLevelObjects objectAtIndex:0];
I've checked that the cellIdentifier is the same in interface builder and in cellForRowAtIndexPath
Thanks in advance if anybody can help me facing this problem
.
Your code for create the cell in the delegate method its something like that ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"UITableViewCell"]autorelease];
}
/* your code */
return cell;
}
It was my fault. i have forget to implement the heightForRowAtIndexPath method. I've had set it only in the xib file

how to make a UIView added to UITableViewCell content view fit within its bounds?

how to make a UIView added to UITableViewCell content view fit within its bounds?
That is, I have created a single NIB file (with 3 labels on it), and want to use this for the display of each cell in a UITableView. I'm adding in the cellForRowAtIndexPath method the custom NIB based view to the cell's content view, however all I see as an end result is one (1) custom NIB based view (not multiple within a tableview as I expected).
How do I arrange so that each custom view fits neatly within each UITableViewCell? Also note the labels within the custom NIB view have word wrap.
Do I have to create a frame for the custom NIB view, but then in that case I'm not exactly sure how to set the co-ordinates. Would it be relative to the UITableViewCell? and if the custom view height can change due to it's UILabel word wrap, then I assume I separately have to manually calculate this height in the UITableView? (perhaps I should go to dynamic/custom view creation and drop the concept of using InterfaceBuilder/NIB)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIView *detailedView = [[[NSBundle mainBundle] loadNibNamed:#"DetailedAppointView" owner:self options:nil] objectAtIndex:0];
[cell.contentView addSubview:detailedView]; // DOESN'T SEE TO WORK
return cell;
}
This is how I do it if I need to implement a custom UITableViewCell.
I create a subclass of UITableViewCell for my custom cell ex. "MyCustomCell". Then I create a NIB file for it, and within this NIB file I insert UITableViewCell and change it's type to "MyCustomCell" and I give it an identifier with the same name as the class name. Then I insert all my subviews onto my cell. All in IB.
After I got my cell pimped out to my likning :-) I use it in the following way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCustomCell";
static NSString *CellNib = #"MyCustomCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (MyCustomCell *)[nib objectAtIndex:0];
}
//Manipulate your custom cell here
return cell;
}

How to add a custom subview to a UITableViewCell derived class when the UITableViewCell is loaded using loadNIB

I am trying to add a custom view control to a custom UITableViewCell which I had designed in the interface builder.
Now, to load UITableCellView I am using
NSArray * loadedViews = [[NSBundle mainBundle] loadNibNamed:#"CustomSearchResultsCell" owner:self options:nil];
What method of the CustomSearchResultsCell class will be called the Nib loads the view and initializes it. I tried using viewDidLoad, but UITableViewCell does not respond to this method. Also initWithStyle is not being called in this case.
TIA
Nitin
Views loaded from a nib are initialized with initWithCoder:, which you can implement in a similar way like initWithFrame:.
Also, take a look at Apple' Sample Code called AdvancedTableViewCells. In this example they use following code to load table view cell from XIB:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ApplicationCell";
ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
}
return cell;
}