MissCoder87
MissCoder87

Reputation: 2669

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

Upvotes: 0

Views: 433

Answers (5)

Shanti K
Shanti K

Reputation: 2918

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.

Upvotes: 1

jbat100
jbat100

Reputation: 16827

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).

Upvotes: 5

Anil Kothari
Anil Kothari

Reputation: 7733

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

Upvotes: 0

Antwan van Houdt
Antwan van Houdt

Reputation: 6991

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.

Upvotes: 0

Mihir Mehta
Mihir Mehta

Reputation: 13833

I guess it should be sub class of UItableViewCell

i.e.

@interface AssessList : UITableViewCell

Upvotes: 0

Related Questions