单击TableView单元格时,表视图崩溃

时间:2012-09-03 19:33:22

标签: ios xcode uitableview

当我点击一个单元格时,我正在崩溃(应用程序在iOS模拟器中退出)。错误代码是

  

“EXC_BAD_ACCESS(code = 1,address = 0x310cc493)

以下是代码:

// .h

#import <UIKit/UIKit.h>

@interface ChecklistsViewController : UITableViewController

@end

// .m

#import "ChecklistsViewController.h"

@interface ChecklistsViewController ()

@end

@implementation ChecklistsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];

    UILabel *label = (UILabel *)[cell viewWithTag:1000];

    if (indexPath.row % 5 == 0) {
        label.text = @"Walk the dog";
    } else if (indexPath.row % 5 == 1) {
        label.text = @"Brush my teeth";
    } else if (indexPath.row % 5 == 2) {
        label.text = @"Learn iOS development";
    } else if (indexPath.row % 5 == 3) {
        label.text = @"Soccer practice";
    } else if (indexPath.row % 5 == 4)
        label.text = @"Eat ice cream";       

    return cell;
}

- tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}  // Control reaches end of non-function

@end

3 个答案:

答案 0 :(得分:3)

在您的示例中,如果没有出列,则不会分配UITableViewCell。你需要做这样的事情:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
if( cell == nil ) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ChecklistItem"] autorelease];

    // whatever additional initialization
    ...
}

答案 1 :(得分:1)

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


   static NSString *CellIdentifier = @"Cell";
   UItableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {
       cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   } 
//write your code here

return cell;

}

答案 2 :(得分:0)

您需要在UITableViewCell方法中分配tableView: cellForRowAtIndexPath:

你的方法应该是这样的:

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


   static NSString *CellIdentifier = @"Cell";
   UItableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {
       cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   } 

//INSERT YOUR CODE

}