2

A quick question, I am setting a delegate for UITableView and I have a question regarding setting the delegate and dataSource properties. I have noticed that the properties for delegate and dataSource are not available, I was thinking that adopting the protocols would make them available. But I am now thinking that I maybe have the superclass for my delegate class wrong.

Currently I have:

-(void)viewDidLoad {
    TestDelegate *tempDelegate = [[TestDelegate alloc] init];
    [self setMyDelegate:tempDelegate];
    // setDelegate
    // setDataSource
    [tempDelegate release];
    [super viewDidLoad];
}

My interface for TestDelegate looks like:

@interface TestDelegate : NSObject <UITableViewDelegate, UITableViewDataSource> {
    NSArray *listData;
    int myCounter;
}

Can I ask if the above should be:

@interface TestDelegate : UITableView <UITableViewDelegate, UITableViewDataSource> {
    NSArray *listData;
    int myCounter;
}

gary

EDIT: I think it might be on the right track: my delegate superClass should be NSObject, I also have a UITableView in Interface Builder.

I have added @property(nonatomic, retain)IBOutlet UITableView *myTableView; in Xcode and connected this to my UITableView in IB. I can now access the delegate and dataSource properties in Xcode via the IBOutlet.

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • THere doesn't seem to be a question there. What do you mean by "properties for delegate and dataSource are not available"? - a UITableView has those properties. Also what do you mean by "adopting the protocols would make them available"? – Paul Lynch May 11 '10 at 10:20
  • Hi Paul, I was getting confused, basically I was trying to set UITableView>delegate and UITableView>dataSource inside Xcode but was confused about how to do it. What I was missing was that like all items you add in IB if you want to access them via Xcode you need to create a pointer to the respective object using IBOutlet. – fuzzygoat May 11 '10 at 10:29

1 Answers1

3

No, there is a difference between subclassing UITableView and merely conforming to the UITableViewDelegate or UITableViewDatasource protocols.

You would want to subclass UITableView if you needed different behavior in the table view itself. -> Most of the time you will not want to do this.

UITableView has a delegate and dataSource property, you can assign it to an object that conforms to the respective protocol.

If you want to have top-level access to the delegate and dataSource properties, you need to subclass UITableViewController. (do not conform to the delegate protocols if you subclass UITableViewController)

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • Hi Jacob, your right, I was getting confused with how to access my UITableView in IB via Xcode and went off on a tangent looking for the solution in the wrong place. Your time and comments are much appreciated. – fuzzygoat May 11 '10 at 10:38