设置UITableView Delegate和DataSource

Mar*_*oni 15 iphone delegates objective-c uitableview

这是我的问题:UITableView我的故事板中有这么小:在此输入图像描述

这是我的代码:

SmallTableViewController.h

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

@interface SmallViewController : UIViewController

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

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

这是SmallTable类的源代码.

SmallTable.h

#import <Foundation/Foundation.h>

@interface SmallTable : NSObject <UITableViewDelegate , UITableViewDataSource>

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

谢谢!!

Lor*_*o B 15

里克斯特是对的.但是我想你需要strong为你的属性使用一个限定符,因为在你的viewDidLoad方法结束时,对象将被解除分配.

@property (strong,nonatomic) SmallTable *delegate;

// inside viewDidload

[super viewDidLoad];
self.delegate = [[SmallTable alloc] init];    
[self.myTable setDelegate:myTableDelegate];
[self.myTable setDataSource:myTableDelegate];
Run Code Online (Sandbox Code Playgroud)

但有没有理由为您的表使用分离的对象(数据源和委托)?为什么不SmallViewController将表设置为表的源和委托?

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

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

// Configure the cell...
cell.textLabel.text = @"Hello there!";
Run Code Online (Sandbox Code Playgroud)

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!";
Run Code Online (Sandbox Code Playgroud)

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

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