我有以下内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
static NSString *CellID = @"FlourishCustomCell";
FlourishCustomCell *cell = (FlourishCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil) {
cell = [[[FlourishCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID] autorelease];
cell.frame = CGRectMake(0, 0.0, 292.0, 30);
}
id <NSFetchedResultsSectionInfo> sectionInfo =
[[appDelegate.fetchedResultsController sections] objectAtIndex:indexPath.section];
NSLog(@"DATE:%@", [sectionInfo name]); //Output: March 25
cell.dateLabel.text=[sectionInfo name];
NSLog(@"header cell:%@", cell.dateLabel.text); //Output:header cell:(null)
return cell;
}
else {
static NSString *CellIdentifier = @"IdeaCustomCell";
IdeaCustomCell *cell = (IdeaCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[IdeaCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.frame = CGRectMake(0, 0.0, 292.0, 70);
}
[self configureCell:cell atIndexPath:[self newIndexPathForIndexPath:indexPath]];
return cell;
}
}
单元格显示并适用于else
部分(IdeaCustomCell
),但出于某些非常令人沮丧的原因,当我设置dateLabel
的{{1}}时,然后立即尝试访问该值,我得到FlourishCell
值,即使我只是设置它!并且单元格不会显示在屏幕上。
我尝试在null
类中覆盖dateLabel的setter方法,并在那里放置了一个NSLog语句,但由于某种原因它永远不会被调用。
我完全不知道造成这种情况的原因。我的意思是我正在分配当时和那里,但它仍然给我null。有什么想法吗?
答案 0 :(得分:4)
您需要初始化标签
解决方案1:在代码中初始化单元格
@interface FlourishCustomCell : UITableViewCell
@property (nonatomic, retain) UILable * dateLabel;
@end
@implementation FlourishCustomCell
@synthesize dateLabel = _dateLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
_dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,300,50)];
[self.contentView addSubview:_dateLabel]
}
return self;
}
@end
解决方案2:从笔尖初始化单元格
@interface FlourishCustomCell : UITableViewCell
@property (nonatomic, retain) UILable * dateLabel;
@end
@implementation FlourishCustomCell
@synthesize dateLabel = _dateLabel;
- (id)init
{
self = [[[[NSBundle mainBundle] loadNibNamed:@"YOUR_NIB_NAME" owner:nil options:nil]
lastObject]
retain];
return self;
}
@end
编辑:oops,忘了在init方法上返回self,更新了答案
答案 1 :(得分:0)
使用笔尖时遇到同样的问题,这对我有用
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
NSString* nibFile = @"NibForCell";
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed: nibFile owner:nil options: nil];
cell = [topLevelObjects objectAtIndex: 0];
}
cell.cellText.text = @"test";
return cell;
}
只需确保在笔尖的属性
中设置重用标识符