设置 UITableView 委托和数据源 [英] Set UITableView Delegate and DataSource

查看:32
本文介绍了设置 UITableView 委托和数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:我的故事板中有这个小 UITableView:

This is my problem: I have this small UITableView in my storyboard:

这是我的代码:

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.

Now as you can see, I want to set an instance called myTableDelegate as Delegate and DataSource of myTable.

这是 SmallTable 类的来源.

This is the Source of SmallTable class.

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 方法.为什么它会在视图出现之前崩溃?

I implemented all the UITableViewDelegate and UITableViewDataSource method that the app need. Why it just crash before the view appear??

谢谢!!

推荐答案

rickster 是对的.但是我想您需要为您的属性使用 strong 限定符,因为在您的 viewDidLoad 方法结束时,无论如何都会释放对象.

rickster is right. But I guess you need to use a strong qualifier for your property since at the end of your viewDidLoad method the object will be deallocated anyway.

@property (strong,nonatomic) SmallTable *delegate;

// inside viewDidload

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

但是是否有任何理由为您的表使用单独的对象(数据源和委托)?为什么不将 SmallViewController 设置为表的源和委托?

But is there any reason to use a separated object (data source and delegate) for your table? Why don't you set SmallViewController as both the source and the delegate for your table?

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

In addition you are not creating the cell in the correct way. These lines do nothing:

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

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

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

dequeueReusableCellWithIdentifier simply retrieves from the table "cache" a cell that has already created and that can be reused (this to avoid memory consumption) but you haven't created any.

你在哪里做alloc-init?改为这样做:

Where are you doing alloc-init? Do this instead:

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:

Furthermore say to numberOfSectionsInTableView to return 1 instead of 0:

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

这篇关于设置 UITableView 委托和数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆