简体   繁体   中英

UITableView with custom class

I have created a derived class from UITableView which will contain some custom logic. But I am not sure how to get it to populate cells.

h. file:

@interface MetricsView : UITableView {
    @private
    NSMutableArray *_items;
}
@end

m. file:

    @implementation MetricsView

    //this gets called
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self) {
            _items = [[NSMutableArray alloc]initWithObjects:@"Name:", nil];
        }
        return self;
    }

//this never gets called??
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    //this never gets called??
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_items count];
    }

//this never gets called??
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = @"MetricsViewCell";

        MetricsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        [cell setupWithName:[_items objectAtIndex:0] withData:[_items objectAtIndex:0]];
        return cell;
    }

    @end

I created MetricsView on top of my ViewController. Here's what storyboard looks like:

这是故事板的样子:

Do I need to add some custom initialization code inside ViewController.m?

Thanks.

//this never gets called??
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

That and the other methods are data source methods. They will not be called unless you set this class as the table view's data source.

Oddly, you have put this code inside the UITableView subclass itself. Okay, that's weird and it violates MVC, but you can do it if you really want to. But then you must set the UITableView subclass as its own data source (and, presumably, its own delegate). That isn't going to happen all by itself; you must do it.

LATER EDIT: However, in general I would cast doubt on the need and desirability of subclassing UITableView in the first place. It's hard for me to think of a situation where this would be useful to do. Just put your table view in the interface, hook its delegate and datasource outlets to the View Controller, and you're done. Now the view controller gets the numberOfSectionsInTableView: and other calls, as rightly it should.

Well let's put things straight. Your UITableView is the view. You can indeed subclass it but it does not seems to be what you want. What I feel you're trying to do is create your custom UITableView delegate and dataSource . Meaning a custom class that does obey to those two protocol. You don't need to subclass your UITableView but you need to set your custom class to be the delegate of your UITableView . And in this class you will be able to implement the method you're trying to implement (-cell for row at index path, -number of row in section, -number of section). What you need is to subclass the well where your tableview will get the water (water being info on what to put in the cell and how many cell to create). I hope this will help!

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