ios - 当我单击TableView单元格时, TableView 崩溃

标签 ios xcode uitableview

单击单元格时出现崩溃(iOS模拟器中的应用程序退出)。错误代码是

“EXC_BAD_ACCESS(代码= 1,地址= 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

最佳答案

在您的示例中,如果没有出列,则不分配UITableViewCell。您需要执行以下操作:

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

    // whatever additional initialization
    ...
}

关于ios - 当我单击TableView单元格时, TableView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253162/

相关文章:

ios - 为什么 UITableViewCell 在滚动可用单元格后显示错误数据?

ios - 在处理即将发布的项目时升级到 Xcode 9 是否有任何风险?

ios - 为 iOS 8 UITableViewCell 调整 XIB 中的标签位置

ios - Objective-C 字符串错误后获取意外 token

iOS:如何获取刚刚在界面生成器中设置的 UIView 的框架

iphone - 为 UITableView 过滤内容

ios - 在 iOS 日期选择器中设置最小和最大月份,但应该更新选择器 View

ios - AlamofireObjectMapper 无法推断通用参数 'T'

iphone - 识别圆圈手势以删除注释,如何检测圆圈?

ios - QuickBlox 视频聊天 : QBRequest. logInWithUserEmail 与 QBChat.instance().connectWithUser