Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 未调用UITableView委托和数据源方法_Iphone_Ios_Objective C_Uitableview - Fatal编程技术网

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

Iphone 未调用UITableView委托和数据源方法,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,我有一个包含UITableView的UIView。tableview的委托设置为my UIView,但它从不调用委托方法: -(id)init { self = [super init]; if (self) { self.tableView = [[UITableView alloc] initWithFrame:self.bounds]; self.tableView.autoresizingMask = UIViewAutoresizingF

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

您应该这样声明您的视图
@接口MyView:UIView


顺便说一句:我想要一个“控制”您的视图和表视图的ViewController,并且让ViewController成为表视图的委托和数据源。

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

[[UITableView alloc]initWithFrame:style:][/code>创建表视图


将self设置为代理和数据源(如上所述),然后将此UITableView添加为此控制器中的子视图。

您不需要使用UIViewController,这只是一个烟幕。您也不需要添加到.h中,尽管您无论如何都应该这样做,因为Xcode会抱怨其他情况,并且您不会得到方法名自动完成


我刚刚编写了一个测试项目,它将一个表视图嵌入到一个普通的UIView中,效果很好。只需确保正在调用初始化代码。我打赌您需要将其移动到awakeFromNib。

您可以编写您在CellforRowatinexPath:method上编写的代码吗?因为我找不到你的代码有什么问题

是否从其他ViewController调用UIView?如果是,如何进行

我曾经做过这样的事。我在UIView中声明了如下方法:

- (void)setUpTableView{
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
}
[self.yourView setUpTableView]; 
然后我从添加此视图的视图控制器调用setUpTableView,如下所示:

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

这可能会有帮助。谢谢。

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

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

我曾经遇到过同样的问题,我犯的愚蠢错误是在init中使用了我称之为[super init]的编码器。TableView在xib中

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

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
}
如果不是这样,只需检查一下即可

尝试以下方法:

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

我也遇到了这个愚蠢的问题

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

故事板解决方案

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.allowsSelection = true
}
选择您的表视图并设置以下内容。。。

Swift解决方案

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

  • 起初,我认为这是一个
    UITableViewDelegate
    问题(如其他答案所示)。事实并非如此。我将其链接到情节提要中的视图控制器
  • 接下来,我认为这是一个
    UITableViewDataSource
    问题,因为我没有实现协议的
    numberOfSections(在tableView:)
    方法(如其他答案所示)。事实并非如此。根据UIKit的文件
  • //如果未实现,则默认值为1

  • 最后,我检查了表视图上的设置:]

  • 您从
    numberOfRowsInSection:
    返回什么?另外,如果这是您所说的UIView,您应该将
    init
    中的代码放到
    awakeFromNib
    中,您是否调用过“重新加载数据”?在
    willMoveToSuperview
    中调用
    reloadData
    ,是否可能未将UIView添加到另一个可见视图中?tableview仅在可见时才会绘制。否则我看不出你的代码有什么错误。我以前也做过同样的事情,它很有魅力。我不认为*.h声明是个问题。但是,我完全同意使用控制视图和表视图的视图控制器。这就是他们在那里的目的+是的,这是有道理的。我确信tableview会对委托调用
    conformsToProtocol:
    /dataSource@LuisCien你是对的。h不是问题所在。我只是在我的一个项目中检查了一下。我删除了这些协议,方法也被调用了。我也有同样的问题,使用我自己的视图控制器。忘记了dataSource=self。