Cocoa touch 初始化自定义UITableViewCell
我试图将单个自定义单元格加载到Cocoa touch 初始化自定义UITableViewCell,cocoa-touch,ios,uitableview,datasource,custom-cell,Cocoa Touch,Ios,Uitableview,Datasource,Custom Cell,我试图将单个自定义单元格加载到UITableView中,但它不断抛出错误 UITableView数据源必须从tableView:cellForRowAtIndexPath: 我不知道为什么。在代码中,我已将表视图单元格链接到UITableViewCell定义,但它一直给我这个错误。这是我的密码;任何帮助都将不胜感激 #import "RegisterDeviceViewController.h" @implementation RegisterDeviceViewController @s
UITableView
中,但它不断抛出错误
UITableView数据源必须从tableView:cellForRowAtIndexPath:
我不知道为什么。在代码中,我已将表视图单元格链接到UITableViewCell定义,但它一直给我这个错误。这是我的密码;任何帮助都将不胜感激
#import "RegisterDeviceViewController.h"
@implementation RegisterDeviceViewController
@synthesize checkString;
@synthesize cellRegistration;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
//Change UITableView Style to Grouped
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
style = UITableViewStyleGrouped;
if (self = [super initWithStyle:style]) {
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.title = @"Registration";
[super viewDidLoad];
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
if (indexPath.row == 1) {
return cellRegistration;
}
}
return nil;
}
//Pass search type over to rootViewController section2
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
#导入“RegisterDeviceViewController.h”
@实现注册器设备视图控制器
@合成校验串;
@综合细胞登记;
//指定的初始值设定项。如果以编程方式创建控制器并希望执行不适用于viewDidLoad的自定义,请重写。
/*
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化。
}
回归自我;
}
*/
//将UITableView样式更改为“分组”
-(id)initWithStyle:(UITableViewStyle)样式{
//Override initWithStyle:如果以编程方式创建控制器并希望执行不适合viewDidLoad的自定义。
样式=UITableViewStyleGroup;
if(self=[super initWithStyle:style]){
}
回归自我;
}
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
self.title=@“注册”;
[超级视图下载];
}
//自定义表视图中的节数。
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
//自定义表视图中的行数。
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.section==1){
if(indexath.row==1){
申报注册;
}
}
返回零;
}
//将搜索类型传递给rootViewController节2
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
/*
*detailViewController=[[alloc]initWithNibName:@“bundle:nil];
// ...
//将选定对象传递给新的视图控制器。
[self.navigationController pushViewController:detailViewController动画:是];
[详细视图控制器发布];
*/
}
/*
//替代以允许默认纵向方向以外的方向。
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
//对于支持的方向,返回YES。
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
*/
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
-(无效)视图卸载{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(无效)解除锁定{
[super dealoc];
}
@结束
好的。UITableView不是这样工作的。当表格视图需要绘制一个单元格(即一行)时;它在dataSource
属性中指定的对象上调用tableView:cellforrowatinexpath:
。您的工作是从该方法返回UITableViewCell。苹果就是这样做的(你也应该这样做):
调用该方法的次数取决于表视图中的节数和每个节中的行数。过程如下所示:
数据源
上调用委托方法numberOfSectionsInTableView:
(它知道它实现了该方法,因为数据源
必须遵守UITableViewDataSource
协议)numberOfSectionsInTableView:
返回一个大于零的数字,则表视图将调用数据源上的委托方法tableView:numberofrowsinssection:
。因此,如果numberOfSectionsInTableView:
返回2
,tableView:numberofrowsinssection:
将被调用两次
tableView:numberOfRowsInSection:
返回一个大于零的数字,则表视图将调用dataSource
上的委托方法tableView:CellForRowsInDexpath:
,因此如果tableView:numberOfRowsInSection:
返回5
,tableView:numberOfRowsInSection:
将被调用五次(每行一次)indepath
变量的使用):cell.textlab.text=[someArrayYouHave objectAtIndex:indepath.row]代码>
好的。UITableView不是这样工作的。当表格视图需要绘制一个单元格(即一行)时;它在
dataSource
属性中指定的对象上调用tableView:cellforrowatinexpath:
。您的工作是从该方法返回UITableViewCell。苹果就是这样做的(你也应该这样做):
调用该方法的次数取决于表视图中的节数和每个节中的行数。过程如下所示:
numberOfSectionsInTableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AnIdentifierString"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];
}
cell.textLabel.text = @"This text will appear in the cell";
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (indexPath.row == 0) {
//this checking is no necessary, anyway if you want use like this
//ensure that cellRegistration is UITableViewCell
return cellRegistration;
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[self configureCell:cell forRowAtIndexPath:indexPath];
}
- (void)configureCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure your cell
}