Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

ios - How can I subclass a UITableView?

I want to subclass a UITableView as I want to create a reusable table view component in my application.

The idea is instead of using a delegate for say cellForRowAtIndexPath I want the table view itself to get that call.

I don't think I want a UITableViewController as this UITableView that I want to build has to live in various UIViewControllers (and these UIViewController might have UITableViews of their own).

I subclassed my UITableView as:

@interface ShareUITableView : UITableView

but none of its methods get called.

My ShareUITableView is created via the NIB by setting the custom class to ShareUITableView. I have verified in code that a ShareUITableView is instantiated.

My UITableView does not delegate to its view controller, so that's not the problem.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understood you, you need this class declaration:

@interface ShareUITableView : UITableView <UITableViewDataSource>

And then, in your class constructor, you should assign the instance itself as its own datasource:

- (id)init
{
    //...
    self.dataSource = self;
    //...
}

Of course, the class will have to adopt the protocol.

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...