设置UITableView Delegate和DataSource [英] Set UITableView Delegate and DataSource
问题描述
这是我的问题:
我的故事板中有这个小的 UITableView
:
这是我的代码:
SmallTableViewController.h
#import <UIKit/UIKit.h>
#import "SmallTable.h"
@interface SmallViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *myTable;
@end
SmallTableViewController.m / p>
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 / p>
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
我实现了所有的<$应用程序需要的c $ c> UITableViewDelegate 和 UITableViewDataSource
方法。为什么在视图出现之前崩溃?
I implemented all the UITableViewDelegate
and UITableViewDataSource
method that the app need. Why it just crash before the view appear??
谢谢!!
推荐答案
rickster是对的但是,由于您的 viewDidLoad
方法结束后,您可能需要为您的资源使用 strong
限定符无论如何,将被释放。
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
只需从表cache 一个已经创建并可以重用的单元格(这可以避免内存消耗),但是你没有创建任何内容。
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
?这样做:
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 Delegate和DataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!