设置UITableView Delegate和DataSource

时间:2012-06-29 15:53:36

标签: iphone objective-c uitableview delegates

这是我的问题: 我的故事板中有这个小UITableViewenter image description here

这是我的代码:

SmallTableViewController.h

#import <UIKit/UIKit.h>
#import "SmallTable.h"

@interface SmallViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *myTable;

@end

SmallTableViewController.m

#import "SmallViewController.h"

@interface SmallViewController ()

@end

@implementation SmallViewController
@synthesize myTable = _myTable;

- (void)viewDidLoad
{
    SmallTable *myTableDelegate = [[SmallTable alloc] init];
    [super viewDidLoad];
    [self.myTable setDelegate:myTableDelegate];
    [self.myTable setDataSource:myTableDelegate];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

现在你可以看到,我想将一个名为myTableDelegate的实例设置为myTable的Delegate和DataSource。

这是SmallTable类的来源。

SmallTable.h

#import <Foundation/Foundation.h>

@interface SmallTable : NSObject <UITableViewDelegate , UITableViewDataSource>

@end

SmallTable.m

@implementation SmallTable

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 5;
}

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

    // Configure the cell...
    cell.textLabel.text = @"Hello there!";

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row pressed!!");
}

@end

我实施了应用所需的所有UITableViewDelegateUITableViewDataSource方法。为什么它会在视图出现之前崩溃?

谢谢!

5 个答案:

答案 0 :(得分:15)

rickster是对的。但我想您需要为您的属性使用strong限定符,因为在viewDidLoad方法结束时,对象将被解除分配。

@property (strong,nonatomic) SmallTable *delegate;

// inside viewDidload

[super viewDidLoad];
self.delegate = [[SmallTable alloc] init];    
[self.myTable setDelegate:myTableDelegate];
[self.myTable setDataSource:myTableDelegate];

但是有没有理由为你的表使用一个单独的对象(数据源和委托)?为什么不将SmallViewController设置为表的来源和委托?

此外,您没有以正确的方式创建单元格。这些行什么都不做:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
cell.textLabel.text = @"Hello there!";

dequeueReusableCellWithIdentifier只是从表“缓存”中检索已经创建并可以重复使用的单元格(这是为了避免内存消耗),但是您还没有创建任何单元格。

你在做什么alloc-init?这样做:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) {
    cell = // alloc-init here
}
// Configure the cell...
cell.textLabel.text = @"Hello there!";

此外说numberOfSectionsInTableView返回1而不是0:

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

答案 1 :(得分:4)

大概你在使用ARC?您的myTableDelegate仅在viewDidLoad中的局部变量中引用 - 一旦该方法结束,它就会被释放。 (在委托/数据源模式中,对象不拥有其委托,因此表视图对您对象的引用很弱。)我不希望单独引起崩溃,但它可能是您的问题的关键。

答案 2 :(得分:1)

setDelegate不会保留代表。

numberOfSectionsInTableView方法必须返回1而不是0;

答案 3 :(得分:1)

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

部分数量应至少设置一个

答案 4 :(得分:0)

UITableView对象的委托必须采用UITableViewDelegate协议。协议的可选方法允许代理管理选择,配置节标题和页脚,帮助删除方法。

enter image description here

相关问题