1

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

UITableView dataSource 必须从 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
4

4 回答 4

11

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

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

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

  1. numberOfSectionsInTableView:Table View在其上调用委托方法dataSource(它知道它实现了该方法,因为它dataSource必须遵守UITableViewDataSource协议)。
  2. 如果返回一个大于零的数字,numberOfSectionsInTableView:表视图将调用tableView:numberOfRowsInSection:. dataSource所以如果numberOfSectionsInTableView:返回2tableView:numberOfRowsInSection:将被调用两次。
  3. 如果每次调用都tableView:numberOfRowsInSection:返回一个大于零的数字,则表视图将调用'tableView:cellForRowAtIndexPath:上的委托方法,dataSource因此如果tableView:numberOfRowsInSection:返回5tableView:numberOfRowsInSection:将被调用五次(每行一次)。
  4. 您有机会自定义该单元格的显示方式,是在您收到一个可用的单元格之后,但在它返回之前(上面显示“此文本将出现在单元格中”)。你可以在这里做很多事情;您应该看到 UITableViewCell 的类参考以查看您可以做的所有事情(我所做的只是将其设置为显示“此文本...”)。以上几行是 iOS 出于性能考虑重用单元格的一种方式。例如,如果您想显示字符串数组中的某个字符串,您可以这样做(注意indexPath变量的使用)cell.textLabel.text = [someArrayYouHave objectAtIndex:indexPath.row];
于 2011-05-06T03:57:58.300 回答
1

你写了:

它不断抛出错误'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:'但我不知道为什么..

但是您的 -tableView:cellForRowAtIndexPath: 部分表示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
    return nil;
}

看完报错信息,看代码,是不是没看出问题?

于 2011-05-06T04:05:50.013 回答
0

您只返回一个部分,只返回一行

节数和行数从 0 开始。

那就是你遇到这种错误

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

另请参阅此帖子以加载自定义单元格。

于 2011-05-06T04:10:30.063 回答
0

为平滑滚动优化的新 iOS7+ 解决方案

您已经可以看到旧的解决方案,但就大量的应用程序将继续只有 iOS7+ 支持这里是一种更优化和正确的解决方案。

单元初始化

要初始化单元格,只需调用dequeueReusableCellWithIdentifier,iOS7+ 系统就足够智能来处理是否cell == nil。如果在出队期间单元格为零,系统将自动为您创建一个单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    return cell;
}

单元配置

然后在方法中进行整个单元配置willDisplayCell。只需在您的类中创建一个配置单元格的方法,您就可以获得更好的性能!

- (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
}
于 2015-10-23T12:58:50.797 回答