简体   繁体   English

UITableView委托和dataSource方法没有被调用

[英]UITableView delegate and dataSource methods not getting called

I have a UIView which contains a UITableView. 我有一个包含UITableView的UIView。 The tableview's delegate is set to my UIView, but it never calls the delegate methods: 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;
}

I've explored every option I can think of, but can't seem to find the root of the problem. 我已经探索了我能想到的每一个选项,但似乎无法找到问题的根源。

EDIT: Included the numberOfRowsInSection. 编辑:包括numberOfRowsInSection。 It could potentially return 0, only it never gets that chance because the "number of rows" NSLog never gets called. 它可能会返回0,只有它永远不会有机会,因为“行数”NSLog永远不会被调用。

Can you please write the code that you have written on your cellForRowAtIndexPath: method? 你能写一下你在cellForRowAtIndexPath:方法上写的代码吗? Because i can't find anything wrong with your code. 因为我发现你的代码没有任何问题。

Are you calling your UIView from some other ViewController? 你是从其他ViewController调用你的UIView吗? If yes, how? 如果有,怎么样?

I have done something like this once. 我做过类似的事情一次。 I declared a method in the UIView like this : 我在UIView中声明了一个方法,如下所示:

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

And then i called the setUpTableView from the view controller i added this view to, like this : 然后我从视图控制器调用setUpTableView我添加了这个视图,如下所示:

[self.yourView setUpTableView]; 

This might help. 这可能有所帮助。 Thanks. 谢谢。

I had a similar problem, my solution was because I did not set the number of sections to 1. 我有类似的问题,我的解决方案是因为我没有将部分数量设置为1。

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

You should declare your view like this @interface MyView : UIView <UITableViewDelegate, UITableViewDataSource> 你应该声明你的视图@interface MyView : UIView <UITableViewDelegate, UITableViewDataSource>

BTW: I would have a ViewController that "controls" your view and your tableview, and have the ViewController be the delegate and datasource of the table view. 顺便说一句:我会有一个ViewController“控制”你的视图和你的tableview,并让ViewController成为表视图的委托和数据源。

You do not need to use a UIViewController, that's just a smokescreen. 你不需要使用UIViewController,这只是一个烟幕。 You also do not need to add to the .h, although you should do that anyway because Xcode will complain otherwise, and you won't get method name auto complete. 你也不需要添加到.h,尽管你应该这样做,因为Xcode会抱怨,否则你不会自动完成方法名称。

I just wrote a test project that embeds a table view in a normal UIView and it works fine. 我刚刚写了一个测试项目,在普通的UIView中嵌入了一个表视图,它运行正常。 Just make sure your initialization code is being called. 只需确保调用您的初始化代码。 I bet you need to move it to awakeFromNib. 我打赌你需要将它移动到awakeFromNib。

I ran into the same issue once, what silly mistake I did was inside initWithCoder I called [super init]. 我曾经遇到过同样的问题,我在initWithCoder中犯了什么愚蠢的错误我叫[super init]。 TableView was in xib TableView在xib中

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

Instead of 代替

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

Just check if that's not the case 只是检查是否不是这种情况

try this: 试试这个:

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

i had this stupid problem too 我也有这个愚蠢的问题

Try changing your UIView to UIViewController and in viewDidLoad , call 尝试将UIView更改为UIViewController并在viewDidLoad中调用

[[UITableView alloc] initWithFrame:style:] to create your Table View. [[UITableView alloc] initWithFrame:style:]创建表视图。

Set self as the delegate and data source like you did above, and then add this UITableView as Subview in this Controller. 像上面一样将self设置为委托和数据源,然后在此Controller中将此UITableView添加为Subview。

I accidentally set my tableView 's allowsSelection property to false . 我不小心将tableViewallowsSelection属性设置为false

Storyboard solution 故事板解决方案

Select your table view and set the following... 选择表格视图并设置以下内容...... 在此输入图像描述

Swift solution 迅捷解决方案

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

Notes 笔记

  1. At first, I thought this was a UITableViewDelegate issue (as other answers suggested). 起初,我认为这是一个UITableViewDelegate问题(正如其他答案所示)。 It wasn't. 事实并非如此。 I linked it to my view controller in my storyboard. 我在故事板中将它链接到我的视图控制器。
  2. Next, I thought it was a UITableViewDataSource issue, in that I didn't implement the protocol's numberOfSections(in tableView:) method (as other answers suggested). 接下来,我认为这是一个UITableViewDataSource问题,因为我没有实现协议的numberOfSections(in tableView:)方法(如其他答案所示)。 It wasn't. 事实并非如此。 According UIKit's documentation, 根据UIKit的文档,

// Default is 1 if not implemented //如果未实现,则默认值为1

  1. Finally, I checked the settings on my table view :] 最后,我检查了表视图上的设置:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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