[英]ios app crashes when I reload tableView with custom uitableviewcell
I'm created a custom table view cell 我创建了一个自定义表格视图单元格
@interface FixtureTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *teamsVSTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *teamsScoreDetailLabel;
@end
I make an asynchronous request to a server for data, and when it returns I reload the tables data. 我向服务器发出异步请求以获取数据,并在服务器返回时重新加载表数据。 This is my
这是我的
//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;
}
The custom cell works if I populate the table when the view first loads and I don't reload the table data, and it also works if I don't use custom cells, so the problem is specific to reloading my table with custom cells. 如果在视图首次加载并且不重新加载表数据时填充表,则自定义单元格将起作用,如果我不使用自定义单元格,则自定义单元格也将起作用,因此该问题特定于使用自定义单元格重新加载表。 Can someone help please?
有人可以帮忙吗?
EDIT And I'm getting an EXC_BAD_ACCESS code=2 error message, so it's probably something to do with memory 编辑并且我收到EXC_BAD_ACCESS code = 2错误消息,所以它可能与内存有关
EDIT 2 My thread 1: 编辑2我的线程1:
EDIT 3: I think THIS is the problem. 编辑3:我认为这是问题所在。 With custom cells I get null objects :/
使用自定义单元格,我得到空对象:/
Here is my custom cell's implementation file: 这是我的自定义单元的实现文件:
#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
Did you register the tableCell in viewDidLoad
? 您是否在
viewDidLoad
注册了tableCell? [self.tableView registerClass:[FixtureTableViewCell class] forCellReuseIdentifier:@"pastFixtureCell"];
EDIT: 编辑:
Don't forget to check if the cell is nil. 不要忘记检查单元格是否为零。
if (cell == nil) {
cell = [[FixtureTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle @"pastFixtureCell"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.