开发者

Iphone UITableViewCell customazitaion

I have a UInavigation controller, UITabelView with customized UITableViewCells, which I will create 4 different xib files for that custom cells, which I will get user input from them.

I am a bit confused here, it is said every view should have its own controller in Iphone, so I will p开发者_开发技巧ush a new subclass of UItableviewcontroller on stack for each page(UITableView), and in theory it should be responsible for the delegations of the uicomponents on that table. But each UItableVIew consists of customized cells(xibs),

So -Do I also need a different controller for each customized cell?

-If so, where can I put the delegation methods of those ui components on the custom cells?

-is this hiearchy correct?

UINavigationController--> UITableViewControllersSubclasses -->customUITableViewCellcontrollers


No. You do not need a different controller for each customized cell.

it is said every view should have its own controller in Iphone

This is not true. Actually, a view controller typically manages a view hierarchy rather than a view. Although the view controller is associated with a view, this view is just the root of the whole view hierarchy of a screen’s worth of content (in iPhone applications).

From View Controller Programming Guide for iOS:

Each view controller is responsible for managing a discrete part of your application’s user interface. View controllers are directly associated with a single view object but that object is often just the root view of a much larger view hierarchy that is also managed by the view controller. The view controller acts as the central coordinating agent for the view hierarchy, handling exchanges between its views and any relevant controller or data objects. A single view controller typically manages the views associated with a single screen’s worth of content, although in iPad applications this may not always be the case.


As I remember you are trying to build a question form. Your design could look like this.

Your Page will be UITableViewController that contains lets say two different type of cells. Cell A and cell B. B/c you want to have different behavior and design of those two cells you want to subclass UITableCellView for both cell A and cell B. In those subclases you will handle how the cell behaves. Your UITableViewController on the other hand will manage all the cells together.

So this is how to do it.

Step One

First let's create a custom cell A (subclass of UITableViewCell)

//CustomCellA.h

#import <UIKit/UIKit.h>

@interface CustomCellA : UITableViewCell {
    UILabel *name;
    NSArray *dataSource
    int value;
}
@property(nonatomic,retain)IBOutlet UILabel *name;
@property(nonatomic,retain)NSArray *dataSource;
@property(nonatomic,readwrite)int value;

@end

//CustomCellA.m

#import "CustomCellA.h"

@implementation CustomCellA

@synthesize name;
@synthesize dataSource;
@synthesize type;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        name       = [[UILabel alloc]init];
        datasource = [NSArray alloc] init];
        value      = -1;
    }
    return self;
}

- (void)dealloc
{
    [name release];
    [dataSource release];
    [super dealloc];
}

@end

Edit the content of CustomCellA.xib file.

  • Change cells class to CustomCellA.
  • Give a cell Identifier "CustomCellA"

Step Two

Desing cell B just like cell A above

Step Three

Now in your UITableViewController you can use your cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    //Use your dataSource example an NSArray field with questions as NSDictionary:
    NSDictionary*question = [questions objectAtIndex:indexPath.row];
    int questionType = [question integerForKey:@"type"];

    if(questionType == 0){//cell A

       static NSString *CellIdentifier = @"CustomCellA";
       static NSString *CellNib = @"CustomCellA";

       UserCustomTableCell *cell = (CustomCellA *)[table dequeueReusableCellWithIdentifier:CellIdentifier];
       if (cell == nil) {
           NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
           cell = (CustomCellA *)[nib objectAtIndex:0];
       }

       // setup your cell

    }else{
        //Do the same for cell B
    }

    return cell;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜