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:
    将被调用五次(每行一次)
  • 您可以在收到可用单元格后,但在返回该单元格之前自定义该单元格的显示方式(上面显示的“此文本将显示在单元格中”)。你可以在这里做很多事情;您应该看到UITableViewCell的类引用,以查看您可以执行的所有操作(我所做的只是将其设置为显示“thisttext…”)。上面的几行是iOS出于性能考虑重用单元的一种方式。例如,如果您想显示字符串数组中的某个字符串,可以这样做(注意
    indepath
    变量的使用):
    cell.textlab.text=[someArrayYouHave objectAtIndex:indepath.row]

  • 好的。UITableView不是这样工作的。当表格视图需要绘制一个单元格(即一行)时;它在
    dataSource
    属性中指定的对象上调用
    tableView:cellforrowatinexpath:
    。您的工作是从该方法返回UITableViewCell。苹果就是这样做的(你也应该这样做):

    调用该方法的次数取决于表视图中的节数和每个节中的行数。过程如下所示:

  • 表视图调用i上的委托方法
    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
    }