将 UITableView 和数据源/委托放在单独的类中不起作用(XCode 4.2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7705006/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:05:20  来源:igfitidea点击:

Put UITableView and datasource/delegate in separate class doesn't work (XCode 4.2)

objective-cxcodeuitableview

提问by MoFuRo

I tried to put the delegate and the datasource of my tableview into a separate class. My problem is that it always crashes with no error. This is why I can't figure out what I'm doing wrong. Maybe someone can tell me. Maybe it is also important that I'm using ARC.

我试图将我的 tableview 的委托和数据源放入一个单独的类中。我的问题是它总是崩溃而没有错误。这就是为什么我无法弄清楚我做错了什么。也许有人可以告诉我。也许我使用 ARC 也很重要。

So that's my simple code:

所以这是我的简单代码:

//ViewController.h
@interface ViewController : UIViewController {
    UITableView *myTableView;
}

@property (strong, nonatomic) IBOutlet UITableView *myTableView;

@end


//ViewController.m
#import "ViewController.h"
#import "MyTableViewDatasourceDelegate.h"

@implementation ViewController
@synthesize myTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    MyTableViewDatasourceDelegate *test = [[MyTableViewDatasourceDelegate alloc] init];

    self.myTableView.delegate = test;
    self.myTableView.dataSource = test;
}

@end


//MyTableViewDelegateDatasourceDelegate.h
@interface MyTableViewDatasourceDelegate : NSObject <UITableViewDataSource, UITableViewDelegate>

@end


//MyTableViewDatasourceDelegate.m
@implementation MyTableViewDatasourceDelegate

-  (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return 1;
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    cell.textLabel.text = @"Test";
    return cell;
}

@end

回答by Bartosz Ciechanowski

It seems that you are not referencing testanywhere else so it gets automatically released at the end of viewDidLoadmethod. Make sure you implement testas an instance variable, so at least something has reference to it.

似乎您没有引用test其他任何地方,因此它会在viewDidLoad方法结束时自动释放。确保你test作为一个实例变量来实现,所以至少有一些东西可以引用它。

It is not required for object to be an ivar for it to persist. Take a look at delegateproperty definition:

对象不需要是一个 ivar 就可以持久化。看一下delegate属性定义:

@property(nonatomic,assign)   id <UITableViewDelegate>   delegate;

The assignis crucial here, it means that this is a weak reference and UITableView won't retain this object. Note, that if it said (nonatomic, retain)your code would work, but it was Apple's design decision to implement it this way to avoid retain cycles.

这里assign很关键,这意味着这是一个弱引用,UITableView 不会保留这个对象。请注意,如果它说(nonatomic, retain)您的代码可以工作,但 Apple 的设计决定是以这种方式实现它以避免保留循环。