0

我从 UITableView 创建了一个派生类,其中包含一些自定义逻辑。但我不知道如何让它填充细胞。

H。文件:

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

米。文件:

    @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

我在 ViewController 之上创建了 MetricsView。这是故事板的样子:

这是故事板的样子:

我需要在 ViewController.m 中添加一些自定义初始化代码吗?

谢谢。

4

2 回答 2

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

那和其他方法是数据源方法。除非您将此类设置为表视图的数据源,否则它们不会被调用。

奇怪的是,您已将此代码放入 UITableView 子类本身。好吧,这很奇怪,它违反了 MVC,但如果你真的想这样做,你可以这样做。但是你必须将 UITableView 子类设置为它自己的数据源(并且可能是它自己的委托)。这不会自行发生。你必须做。

后期编辑:但是,总的来说,我首先会对子类化 UITableView 的必要性和可取性表示怀疑。我很难想到这样做会有用的情况。只需将您的表格视图放入界面中,将其delegatedatasource插座连接到视图控制器,您就完成了。现在视图控制器得到了numberOfSectionsInTableView:和其他调用,这是理所当然的。

于 2013-05-07T20:24:49.380 回答
1

好吧,让我们把事情说清楚。你UITableView是观点。您确实可以对它进行子类化,但它似乎不是您想要的。我觉得你正在尝试做的是创建你的自定义UITableView delegatedataSource. 意味着一个确实遵守这两个协议的自定义类。你不需要子类化你的 UITableView 但你需要将你的自定义类设置为你的UITableView. 在这个类中,您将能够实现您尝试实现的方法(-cell for row at index path,-number of row in section,-number of section)。您需要的是对您的 tableview 将获取水的井进行子类化(水是有关在单元格中放入什么以及要创建多少个单元格的信息)。我希望这个能帮上忙!

于 2013-05-07T20:44:40.197 回答