初始化自定义UITableViewCell

tin*_*ead 0 cocoa-touch datasource uitableview custom-cell ios

我正在尝试将一个自定义单元格加载到一个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
Run Code Online (Sandbox Code Playgroud)

Ala*_*ino 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;
}
Run Code Online (Sandbox Code Playgroud)

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

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