[英]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
#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
我实现了应用程序所需的所有UITableViewDelegate
和UITableViewDataSource
方法。 为什么它会在视图出现之前崩溃?
谢谢!!
里克斯特是对的。 但是我猜你需要为你的属性使用一个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;
}
想必你正在使用ARC? myTableDelegate
仅在viewDidLoad
中的局部变量中引用 - 一旦该方法结束,它就会被释放。 (在委托/数据源模式中,对象不拥有其委托,因此表视图对您对象的引用很弱。)我不希望单独导致崩溃,但它可能是您的问题的关键。
setDelegate
不会保留委托。
和
numberOfSectionsInTableView
方法必须返回1而不是0;
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
部分数量应至少设置一个
UITableView对象的委托必须采用UITableViewDelegate协议。 协议的可选方法允许代理管理选择,配置节标题和页脚,帮助删除方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.