【问题标题】:ios app crashes when I reload tableView with custom uitableviewcell当我使用自定义 uitableviewcell 重新加载 tableView 时,ios 应用程序崩溃
【发布时间】:2015-03-11 23:36:18
【问题描述】:

我创建了一个自定义表格视图单元格

@interface FixtureTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *teamsVSTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *teamsScoreDetailLabel;

@end

我向服务器发出异步数据请求,当它返回时,我重新加载表数据。这是我的

//reload table data when request is over
-(void)finished
{
    self.pastFixturesArray = aPastFixture.pastFixturesArray;
   [self.tableView reloadData];
}


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

// Configure the cell...
PastFixture *thisFixture = [pastFixturesArray objectAtIndex:indexPath.row];


//string to store the match result and score result
NSMutableString *matchString = [[NSMutableString alloc] init];
NSMutableString *matchScoreString = [[NSMutableString alloc] init];

//configure match string
[matchString appendFormat:@"%@ vs %@",thisFixture.homeTeam.teamName, thisFixture.awayTeam.teamName];

//configure the score string
[matchScoreString appendFormat:@"%ld - %ld", thisFixture.homeScore, thisFixture.awayScore];

//configure cell labels
cell.teamsVSTextLabel.text = matchString;
cell.teamsScoreDetailLabel.text = matchScoreString;
cell.teamsScoreDetailLabel.textColor = [UIColor blueColor];

return cell;
}

如果我在第一次加载视图时填充表格并且我不重新加载表格数据,则自定义单元格有效,如果我不使用自定义单元格,它也有效,因此问题特定于重新加载我的表格自定义单元格。有人可以帮忙吗?

编辑我收到 EXC_BAD_ACCESS code=2 错误消息,所以这可能与内存有关

编辑 2 我的线程 1:

编辑 3:我认为这是问题所在。使用自定义单元格,我得到空对象:/

这是我的自定义单元的实现文件:

#import "FixtureTableViewCell.h"

@implementation FixtureTableViewCell

@synthesize teamsVSTextLabel, teamsScoreDetailLabel;

- (void)awakeFromNib {
// Initialization code

}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    teamsVSTextLabel.text = @".";

}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

【问题讨论】:

  • 请发布完整的符号化堆栈跟踪以及您收到的确切错误消息。
  • 你能告诉我怎么做吗?
  • 看起来你有一个无限循环。你能扩展线程 1 吗?
  • 你看到“CA::Layer::ensure_transaction...”左边的数字了吗?这就是它被调用的次数。您以某种方式创建了一个循环返回自身的 UIView 结构。

标签: ios objective-c uitableview


【解决方案1】:

您是否在viewDidLoad 中注册了tableCell? [self.tableView registerClass:[FixtureTableViewCell class] forCellReuseIdentifier:@"pastFixtureCell"];

编辑:

不要忘记检查单元格是否为 nil。

if (cell == nil) {
    cell = [[FixtureTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle @"pastFixtureCell"];
}

【讨论】:

  • 如果你注册一个类,单元格永远不会为nil,所以不需要检查。此外,如果单元格是在 xib 或情节提要中制作的,则不应注册该类。
  • 当我注册表格单元时,应用程序不再崩溃,但表格也没有被填充
  • 哦,故事板...那么,您是否将 Identity Inspector 中的单元格自定义类更改为您的 FixtureTableViewCell?也许这就是它首先崩溃的原因? static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/…
  • 是的,我使用了正确的重用标识符;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多