【问题标题】:UITableView crashing on scrollUITableView 在滚动时崩溃
【发布时间】:2012-03-12 02:31:06
【问题描述】:

这是我的代码:

- (id)init
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        //self.tableView.delegate = self;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIColor *wood = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wood_pattern.png"]];
    self.view.backgroundColor = wood;
    self.tableView.separatorColor = [UIColor clearColor];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundView.backgroundColor = [UIColor redColor];
    }

    // Configure the cell...

    cell.textLabel.text = @"Test";

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

当我向下滚动然后返回到以前在屏幕上的单元格时,应用程序崩溃了。任何想法为什么?

附带说明..背景颜色设置为红色,虽然我可以看到文字,但我看不到背景颜色。

【问题讨论】:

  • 当开发人员看到崩溃时,我们通常会查找崩溃日志的内容。如果没有崩溃日志,它会浪费我们的时间试图通过查看代码来找出答案。你能告诉我们崩溃日志吗?
  • 在 if 语句之外设置单元格颜色,您应该会看到红色,但我需要查看错误以获得进一步的帮助
  • 我一直遇到 xcode 行为怪异的问题(非粗体 nslog,建议我的代码完成的可能性最小,在我输入时上下跳跃,每次我都自行取消目标框尝试添加文件等),现在它没有给我崩溃日志。
  • 关于背景颜色:你想改变内容视图的背景颜色
  • 滚动时会调用几个dataSource/delegate方法。我的赌注:在 tabelView:numberOfRowInSection: 你正在访问一个被过度释放/保留不足的集合——数组或类似的。

标签: iphone objective-c uitableview


【解决方案1】:

在 XIB 中正确绑定 UITableView。和正确的以下代码。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.separatorColor = [UIColor clearColor];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }
    cell.contentView.backgroundColor = [UIColor redColor];
    cell.textLabel.text = @"Test";
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Configure the cell.

    return cell;
}

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多