初始化自定义UITableViewCell [英] Initialize custom UITableViewCell

查看:85
本文介绍了初始化自定义UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将单个自定义单元格加载到 UITableView 中,并不断抛出错误

I'm trying to load a single custom cell into a UITableView and it keeps throwing an error


UITableView数据源必须从tableView:cellForRowAtIndexPath返回一个单元格:

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

我不知道为什么。我已经在代码中将表视图单元格链接到UITableViewCell定义,但是它一直给我这个错误。这是我的代码;任何帮助将不胜感激。

I have no idea why. I have linked my table view cell to the UITableViewCell definition in my code, but it keeps giving me this error. Here is my code; any help would be greatly appreciated.

#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


推荐答案

好的。这不是UITableView的工作方式。当表视图需要绘制一个单元格(即一行)时;它会在 dataSource 属性中指定的对象上调用 tableView:cellForRowAtIndexPath:。从该方法返回UITableViewCell是您的工作。这就是Apple的做法(以及应该如何做):

Okay. That's not how UITableView works. When the table view needs to draw a cell (ie, a row); it invokes tableView:cellForRowAtIndexPath: on the object specified in the dataSource property. It's your job to return a UITableViewCell from that method. This is how Apple does it (and how you should do it):

- (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;
}

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

The number of times that method will be invoked depends on the number of sections in the table view and the number of rows in each section. The process works like this:


  1. 表视图调用委托方法 numberOfSectionsInTableView: dataSource (它知道它实现了该方法,因为 dataSource 必须遵守 UITableViewDataSource 协议。)

  2. 如果 numberOfSectionsInTableView:返回大于零的数字,则表视图将调用委托数据源上的方法 tableView:numberOfRowsInSection:。因此,如果 numberOfSectionsInTableView:返回 2 ,则 tableView:numberOfRowsInSection:

  3. 如果每次调用 tableView:numberOfRowsInSection:都返回一个大于零的数字,则表视图将调用在 dataSource 上使用委托方法 tableView:cellForRowAtIndexPath:'因此,如果 tableView:numberOfRowsInSection:返回 5 tableView:numberOfRowsInSection:将被调用五次(每行一次) 。

  4. 自定义该单元格的显示方式的机会是在您收到可用的单元格之后,但在返回之前(其中此文本将显示在单元格显示在上方)。您可以在这里做很多事情;您应该会看到UITableViewCell的类参考,以查看您可以做的所有事情(我所做的全部都将其设置为显示此文本...)。 上方行是iOS出于性能考虑重新使用单元的一种方式。例如,如果要显示字符串数组中的某个字符串,则可以执行此操作(注意使用 indexPath 变量): cell.textLabel.text = [someArrayYouHave objectAtIndex:indexPath.row];

  1. Table View invokes the delegate method numberOfSectionsInTableView: on its dataSource (it knows it implements that method because the dataSource must adhere to the UITableViewDataSource protocol).
  2. If numberOfSectionsInTableView: returns a number greater than zero, the table view will invoke the delegate method tableView:numberOfRowsInSection: on the dataSource. So if numberOfSectionsInTableView: returns 2, tableView:numberOfRowsInSection: will be invoked twice.
  3. If each invocation of tableView:numberOfRowsInSection: returns a number greater than zero, the table view will invoke the delegate method tableView:cellForRowAtIndexPath: on the dataSource' So if tableView:numberOfRowsInSection: returns 5, tableView:numberOfRowsInSection: will be invoked five times (once for each individual row).
  4. Your opportunity to customise how that cell appears is after you've received a useable cell, but before it is returned (where 'This text will appear in the cell' appears above). You can do quite a lot here; you should see the Class Reference for UITableViewCell to see everything you can do (all I've done is set it to show 'This text...'). The lines above that are a way for iOS to reuse cells for performance considerations. If you, for example, wanted to show a certain string from an array of strings, you could do this (notice the use of the indexPath variable): cell.textLabel.text = [someArrayYouHave objectAtIndex:indexPath.row];.

这篇关于初始化自定义UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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