UITableView委托和数据源方法未被调用

13

我有一个包含UITableView的UIView。UITableView的代理设置为我的UIView,但它从未调用代理方法:

-(id)init {
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds];
        self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.scrollEnabled = NO;
        self.tableView.layer.cornerRadius = PanelCornerRadius;
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - UITableViewDelegate methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"height for row");
    int height = [self heightForRowAtIndexPath:indexPath];

    return height;
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"number of rows");
    if (self.manager.fetchOperation.dataFetchProblem) {
        return 1;
    }

    int numberOfRows = [self.delegate numberOfSectionsInPanel:self];

    return numberOfRows;
}

我已经探索了我能想到的每个选项,但似乎找不到问题的根源。

编辑:包括了numberOfRowsInSection。 它有可能返回0,只是从来没有机会,因为"行数"NSLog从未被调用过。


1
你从 numberOfRowsInSection: 返回什么? - Lance
另外,如果这是在像你说的那样的UIView中,你应该将init中的代码放到awakeFromNib中。 - Lance
3
你是否曾经调用过"reloadData"? - Rok Jarc
1
willMoveToSuperview 中调用 reloadData - Lance
2
你有没有把UIView添加到另一个可见的视图中呢?只有在tableview可见时才会被绘制。否则,我从你的代码中看不出任何问题。我以前也做过同样的事情,而且它运行得很好。 - Vincent
显示剩余5条评论
8个回答

22

请问您能否将您在cellForRowAtIndexPath:方法中编写的代码写出来吗?因为我无法找到您的代码有什么问题。

您是否在从其他视图控制器调用UIView?如果是,请问如何调用?

我曾经做过这样的事情。我像这样在UIView中声明了一个方法:

- (void)setUpTableView{
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
}

然后我从添加了这个视图的视图控制器中调用了setUpTableView方法,代码如下:

[self.yourView setUpTableView]; 

这可能会有所帮助。谢谢。


我曾经遇到过同样的问题,使用了自己的视图控制器。忘记了dataSource=self。 - Bill Cheswick

16

我有一个类似的问题,我的解决方案是因为我没有将章节数量设置为1。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

3
您不需要使用UIViewController,那只是一个幌子。您也不需要在.h文件中添加,尽管无论如何都应该这样做,因为否则Xcode会抱怨,并且您将无法获得方法名称自动完成。
我刚刚写了一个测试项目,在普通的UIView中嵌入了一个表格视图,它可以正常工作。只需确保调用了您的初始化代码。我敢打赌您需要将其移动到awakeFromNib中。

3

您应该这样声明您的视图:

@interface MyView : UIView <UITableViewDelegate, UITableViewDataSource>

顺便说一下:我会有一个“控制”您的视图和表格视图的ViewController,并将ViewController作为表格视图的委托和数据源。


1
我认为 *.h 声明不是问题所在。然而,我完全同意拥有一个控制视图和表格视图的视图控制器。这就是它们存在的原因。+1 - LuisCien
啊,是的,这很有道理。我相信tableview会在delegate/dataSource上调用conformsToProtocol:方法。 - Lance
@LuisCien 你说得对。.h文件不是问题所在。我刚刚在我的一个项目中检查了一下。我移除了这些协议,方法确实被调用了。 - gWiz

1
我曾经遇到过相同问题,愚蠢的错误在于我在initWithCoder中调用了 [super init]。TableView在xib中。
-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super init]
}

而不是

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
}

只需检查是否不是这种情况


1

try this:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

我也遇到过这个愚蠢的问题。

0

尝试将您的UIView更改为UIViewController, 并在 viewDidLoad 中调用

[[UITableView alloc] initWithFrame:style:] 以创建您的表视图。

像上面那样将自己设置为代理和数据源,然后将这个UITableView添加为此控制器的子视图。


0

我不小心将tableViewallowsSelection属性设置为false

Storyboard解决方案

选择您的表视图并设置以下内容... enter image description here

Swift解决方案

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.allowsSelection = true
}

注释

  1. 起初,我以为这是一个UITableViewDelegate问题(正如其他答案所建议的)。但实际上不是这个问题,原因在于我没有在storyboard中将其链接到我的视图控制器。
  2. 接下来,我认为这是一个UITableViewDataSource问题,因为我没有实现协议的numberOfSections(in tableView:)方法(正如其他答案所建议的)。但是根据UIKit文档,

// 默认值为1,如果未实现

  1. 最后,我检查了我的表视图的设置 :]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接