ios 未调用 UITableView 委托和数据源方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18970013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView delegate and dataSource methods not getting called
提问by Andrew
I have a UIView which contains a UITableView. The tableview's delegate is set to my UIView, but it never calls the delegate methods:
我有一个包含 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;
}
I've explored every option I can think of, but can't seem to find the root of the problem.
我已经探索了我能想到的每一个选项,但似乎无法找到问题的根源。
EDIT: Included the numberOfRowsInSection. It could potentially return 0, only it never gets that chance because the "number of rows" NSLog never gets called.
编辑:包括 numberOfRowsInSection。它可能会返回 0,只是它永远不会有这样的机会,因为“行数” NSLog 永远不会被调用。
回答by try catch finally
Can you please write the code that you have written on your cellForRowAtIndexPath: method? Because i can't find anything wrong with your code.
你能写一下你在 cellForRowAtIndexPath: 方法上写的代码吗?因为我找不到你的代码有什么问题。
Are you calling your UIView from some other ViewController? If yes, how?
你是从其他一些 ViewController 调用你的 UIView 吗?如果是,如何?
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.
这可能会有所帮助。谢谢。
回答by Darkin
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;
}
回答by gWiz
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 成为 table view 的委托和数据源。
回答by jsd
You do not need to use a UIViewController, that's just a smokescreen. 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.
您不需要使用 UIViewController,那只是一个烟幕。您也不需要添加到 .h,尽管您应该这样做,否则 Xcode 会抱怨,并且您不会自动完成方法名称。
I just wrote a test project that embeds a table view in a normal UIView and it works fine. Just make sure your initialization code is being called. I bet you need to move it to awakeFromNib.
我刚刚写了一个测试项目,它在一个普通的 UIView 中嵌入了一个表格视图,它工作正常。只要确保您的初始化代码正在被调用。我打赌你需要把它移动到awakeFromNib。
回答by user3894518
I ran into the same issue once, what silly mistake I did was inside initWithCoder I called [super init]. TableView was in xib
我曾经遇到过同样的问题,我在 initWithCoder 中犯了一个愚蠢的错误,我称之为 [super init]。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
只是检查是否不是这种情况
回答by ironRoei
try this:
尝试这个:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
i had this stupid problem too
我也有这个愚蠢的问题
回答by Andrew Kirna
I accidentally set my tableView
's allowsSelection
property to false
.
我不小心将我tableView
的allowsSelection
属性设置为false
.
Storyboard solution
故事板解决方案
Select your table view and set the following...
Swift solution
迅捷解决方案
override func viewDidLoad() {
super.viewDidLoad()
tableView.allowsSelection = true
}
Notes
笔记
- At first, I thought this was a
UITableViewDelegate
issue (as other answers suggested). It wasn't. I linked it to my view controller in my storyboard. - Next, I thought it was a
UITableViewDataSource
issue, in that I didn't implement the protocol'snumberOfSections(in tableView:)
method (as other answers suggested). It wasn't. According UIKit's documentation,
- 起初,我认为这是一个
UITableViewDelegate
问题(正如其他答案所建议的那样)。不是。我将它链接到我的故事板中的视图控制器。 - 接下来,我认为这是一个
UITableViewDataSource
问题,因为我没有实现协议的numberOfSections(in tableView:)
方法(正如其他答案所建议的那样)。不是。根据 UIKit 的文档,
// Default is 1 if not implemented
// 如果未实现,则默认为 1
- Finally, I checked the settings on my table view :]
- 最后,我检查了表视图上的设置:]
回答by Unheilig
Try changing your UIView
to UIViewController
and in viewDidLoad
, call
尝试更改您的UIView
toUIViewController
和 in 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 设置为委托和数据源,然后将此 UITableView 添加为此控制器中的子视图。