简体   繁体   中英

Overriding UITableViewDatasource in a subclass of UITableView

I have a UITableView subclass that sometimes needs to provide its own internal implementation for reckoning the number of sections. As such, sometimes I need it to grab the number of sections from the datasource, and sometimes I need to ignore the datasource and calculate it for myself.

I tried overriding the UITableView's numberOfRowsInSection: (NOTE this is the table view's version of this, not the datasource's tableView:numberOfRowsInSection:) with something like this:

- (NSInteger)numberOfRowsInSection:(NSInteger)section
    {
    NSInteger numberOfRows = 0 ;
    if( ![self sectionIsCollapsed:section] ) // is the section expanded?
        {
        numberOfRows = [super numberOfRowsInSection:section] ;
        }
    return numberOfRows ;
    }

However, UITableView's -endUpdate does not actually call this method but goes directly to the datasource. Is there a way to override the call to the datasource, or am I just SOL?

Thanks!

Your question is somewhat related to something I am working on.
If I understand correctly, you want to override -some- of the methods of UITableviewDataSource, but not others, in your custom UITableView implementation.

In your case, you are asking for the UITableView to be its own DataSource. Perhaps you can switch back and forth between your embedded datasource and the 'real' one at appropriate times?

The tableview, the datasource, and the delegate have no inheritance relationship with each other. The tableview just has pointers to the other two. The only way I can see this working is if you implement the full UITableviewDataSource protocol in your tableview, (which frankly smells bad) and then shuffle the delegate pointers around, which seems risky.

I think a cleaner and less risky way would be to create a class that conforms to the UITableviewDataSource protocol, which takes an instance of the framework-users existing UITableviewDataSource as an init parameter, and wraps all the way around it, passing through and/or modifying calls to it when you need its behavior, and keeping it in the dark when you don't. Then you use your wrapper class(es) as the delegat(es) on the tableview.

What do you think?

I don't remember the name for this design pattern, so I will call it the Matrix Pattern. The client's datasource/delegate is in the dark, in a box, and doesn't know it's not in the 'real' world.

I suspect that you would need to implement classes for the UITableviewDataSource and the UITableViewDelegate to make this work smoothly.

I have implemented the above scheme in my own project, and it works well. I have entirely wrapped the delegate and the datasource in a wrapper object, which is initialized with the existing delegate and datasource. Then I manipulate the data flowing in and out through my wrapper object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM