【问题标题】:UITableView delegate and dataSource methods not getting calledUITableView 委托和数据源方法没有被调用
【发布时间】:2013-09-23 22:34:45
【问题描述】:

我有一个包含 UITableView 的 UIView。 tableview 的委托设置为我的 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 永远不会被调用。

【问题讨论】:

  • 你从numberOfRowsInSection:返回什么?
  • 另外,如果像你说的那样在 UIView 中,你应该把init 中的代码放到awakeFromNib
  • 你曾经调用过“reloadData”吗?
  • willMoveToSuperview中致电reloadData
  • 您是否有可能没有将 UIView 添加到另一个可见视图中?只有在可见时才会绘制 tableview。否则我看不出你的代码有什么问题。我以前也做过同样的事情,它就像魅力一样。

标签: iphone ios objective-c uitableview


【解决方案1】:

你能写下你在 cellForRowAtIndexPath: 方法上写的代码吗?因为我找不到您的代码有任何问题。

你是从其他 ViewController 调用你的 UIView 吗?如果是,怎么做?

我曾经做过这样的事情。我在 UIView 中声明了一个方法,如下所示:

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

然后我从视图控制器中调用 setUpTableView 我添加了这个视图,像这样:

[self.yourView setUpTableView]; 

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

【讨论】:

  • 我有同样的问题,使用我自己的视图控制器。忘记了 dataSource=self。
【解决方案2】:

我遇到了类似的问题,我的解决方案是因为我没有将节数设置为 1。

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

【讨论】:

    【解决方案3】:

    你应该这样声明你的视图 @interface MyView : UIView <UITableViewDelegate, UITableViewDataSource>

    顺便说一句:我会有一个 ViewController 来“控制”您的视图和 tableview,并让 ViewController 成为 table view 的委托和数据源。

    【讨论】:

    • 我不认为 *.h 声明是问题所在。但是,我完全同意拥有一个控制视图和表格视图的视图控制器。这就是他们的目的。 +1
    • 啊,是的,这是有道理的。我确定 tableview 在委托/数据源上调用 conformsToProtocol:
    • @LuisCien 你是对的。 .h 不是问题。我刚刚在我的一个项目中检查了这一点。我删除了这些协议,并且确实调用了这些方法。
    【解决方案4】:

    你不需要使用 UIViewController,那只是一个烟幕。您也不需要添加到 .h,尽管无论如何您都应该这样做,否则 Xcode 会报错,而且您不会让方法名称自动完成。

    我刚刚编写了一个测试项目,它在普通 UIView 中嵌入了一个表格视图,它工作正常。只需确保正在调用您的初始化代码。我打赌你需要把它移到 awakeFromNib。

    【讨论】:

      【解决方案5】:

      我曾经遇到过同样的问题,我犯的愚蠢错误是在我称为 [super init] 的 initWithCoder 中。 TableView 在 xib 中

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

      代替

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

      看看是不是这样

      【讨论】:

        【解决方案6】:

        试试这个:

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

        我也遇到过这个问题

        【讨论】:

          【解决方案7】:

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

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

          像上面一样将self设置为委托和数据源,然后将这个UITableView作为子视图添加到这个Controller中。

          【讨论】:

            【解决方案8】:

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

            故事板解决方案

            选择您的表格视图并设置以下...

            Swift 解决方案

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

            备注

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

            //如果没有实现,默认为1

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-11-08
              • 1970-01-01
              • 2016-01-17
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多