Ios 自定义单元格从不为零(不使用情节提要)

Ios 自定义单元格从不为零(不使用情节提要),ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,目前我有以下课程: 我的问题是,我的自定义单元格类“GTNewsCustomCell”从未被调用,我在.m文件中设置了一些断点,但什么都没有发生,这时我意识到在我的“CellForRowatineXpath”中,单元格从来都不是零 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentif

目前我有以下课程:

我的问题是,我的自定义单元格类“GTNewsCustomCell”从未被调用,我在.m文件中设置了一些断点,但什么都没有发生,这时我意识到在我的“CellForRowatineXpath”中,单元格从来都不是零

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

static NSString *CellIdentifier1 = @"NewsCell";


GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

if(newsCell == nil){

newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];

}

newsCell.titleLabel.text = [[self.newsList objectAtIndex:indexPath.section]objectForKey:@"title"];
NSAttributedString *attString = [[NSAttributedString alloc]initWithString:[[self.newsList objectAtIndex:indexPath.section]objectForKey:@"previewMessage"]];

newsCell.messageTextView.attributedText = attString;

return newsCell;
}

下面是我的GTNewsCustomCell.m代码的一小部分:

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

    self.messageTextView.textColor = [UIColor blackColor];
    self.messageTextView.backgroundColor = GTDefaultTextBackgroundColor;
    self.messageTextView.editable = NO;
    self.messageTextView.userInteractionEnabled = NO;

    self.messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    NSString *htmlTag = @"<b></b>";
    NSString *html = [NSString stringWithFormat:@"%@%@",htmlTag,self.messageTextView.attributedText];

    NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];

    CGFloat fontSizeMultiplier = 1.1;
    CGFloat const DTCoreTextDefaultFontSize = 12.0;
    NSString * fontName = @"Helvetica";
    DTCSSStylesheet* css;


    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){

        css = [[DTCSSStylesheet alloc] initWithStyleBlock:@"ul li{padding-left: -10px;}"];


        NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:fontSizeMultiplier], NSTextSizeMultiplierDocumentOption,
                                        fontName, DTDefaultFontFamily,
                                        @"purple", DTDefaultLinkColor,
                                        @"red", DTDefaultLinkHighlightColor,
                                        css,DTDefaultStyleSheet,
                                        [NSNumber numberWithBool:YES],DTUseiOS6Attributes,
                                        nil];

.
.
.
.
-(id)initWithStyle:(UITableViewCellStyle)样式重用标识符:(NSString*)重用标识符
{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我){
//初始化代码
self.messageTextView.textColor=[UIColor blackColor];
self.messageTextView.backgroundColor=GTDefaultTextBackgroundColor;
self.messageTextView.editable=否;
self.messageTextView.userInteractionEnabled=否;
self.messageTextView.autoresizingMask=UIViewAutoresizingFlexibleWidth | uiviewautoresizingflexiblewhight;
NSString*htmlTag=@;
NSString*html=[NSString stringWithFormat:@“%@%@”,htmlTag,self.messageTextView.AttributeText];
NSData*data=[html数据使用编码:NSUTF8StringEncoding];
CGFloat-fontSizeMultiplier=1.1;
CGFloat const DTCoreTextDefaultFontSize=12.0;
NSString*fontName=@“Helvetica”;
DTCSSStylesheet*css;
如果(系统版本大于或等于(@“7.0”)){
css=[[DTCSSStylesheet alloc]initWithStyleBlock:@“ul li{padding left:-10px;}”];
NSMutableDictionary*options=[NSMutableDictionary Dictionary WithObjectsAndKeys:[NSNumber numberWithFloat:fontSizeMultiplier],NSTextSizeMultiplierDocumentOption,
fontName,DTDefaultFontFamily,
@“紫色”,DTC颜色,
@“红色”,DTDefaultLinkHighlightColor,
css、DTDefaultStyleSheet、,
[NSNumber numberWithBool:YES],DTUSEIOS6属性,
零];
.
.
.
.
以下是我的xib文件中的一些屏幕截图,以了解更多信息:

我的GTNewsTableViewController:

GTNewsCustomCell(文件所有者):

GTNewsCustomCell(视图):

GTNewsCustomCell:


好的,因为您是通过tableView的方法注册单元格的
RegisterInB:forCellReuseIdentifier:
initWithStyle:reuseIdentifier:
永远不会被调用。如果您想进行样式设置,请使用
UITableViewCell
子类的-awakeFromNib方法

- (void)awakeFromNib {
  [super awakeFromNib];
  self.messageTextView.textColor = [UIColor blackColor];
  self.messageTextView.backgroundColor = GTDefaultTextBackgroundColor;
  self.messageTextView.editable = NO;
  self.messageTextView.userInteractionEnabled = NO;
}

好的,因为您是通过tableView的方法注册单元格的
Registernb:forCellReuseIdentifier:
initWithStyle:reuseIdentifier:
永远不会被调用。如果您想进行样式设置,请使用
UITableViewCell
子类的-awakeFromNib方法

- (void)awakeFromNib {
  [super awakeFromNib];
  self.messageTextView.textColor = [UIColor blackColor];
  self.messageTextView.backgroundColor = GTDefaultTextBackgroundColor;
  self.messageTextView.editable = NO;
  self.messageTextView.userInteractionEnabled = NO;
}
使用GTNewsCustomCell(FileOwner),如果需要,您将无法重用此单元格。您应该将其设置为NSObject(FileOwner)。并将此单元格加载到CellForRowatineXpath中

通过这种方式,您将获得与自定义单元格相同的样式

static NSString *CellIdentifier= @"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==  nil) {
    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
                                                      owner:self//transfer ownership to self
                                                    options:nil];
    cell = [nibViews objectAtIndex:0];
}

return cell;
}
使用GTNewsCustomCell(FileOwner),如果需要,您将无法重用此单元格。您应该将其设置为NSObject(FileOwner)。并将此单元格加载到CellForRowatineXpath中

通过这种方式,您将获得与自定义单元格相同的样式

static NSString *CellIdentifier= @"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==  nil) {
    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
                                                      owner:self//transfer ownership to self
                                                    options:nil];
    cell = [nibViews objectAtIndex:0];
}

return cell;
}

如果您像这样在表视图中注册单元格,
[[u tableView registerNib:[UINib nibWithNibName:@“CellXib”bundle:nil]forCellReuseIdentifier:@“identifier”];
。如果您这样做,请尝试将所有样式设置放在表cell的-awakeFromNib方法中是的,我做UINib*nib=[UINib nibWithNibName:@“GTNewsCustomCell”bundle:nil];[self.tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];我会测试它……如果您像这样在表视图中注册单元格,可能会发生这种情况
[\u tableView registerNib:[UINib nibWithNibName:@“CellXib”bundle:nil]forCellReuseIdentifier:@“identifier”];
。如果要这样做,请尝试将所有样式设置放入表格cellYes i do UINib*nib=[UINib nibWithNibName:@“GTNewsCustomCell”bundle:nil];[self.tableView注册表项nb:nib forCellReuseIdentifier:CellIdentifier 1];我将测试它…现在调用了我的自定义单元格,但我的单元格看起来与我预期的不一样,是否有机会使用initWithStyle方法?是的,不要使用此方法:
UINib*nib=[UINib nibWithNibName:@“GTNewsCustomCell”bundle:nil];[self.tableView注册表项nb:nib forCellReuseIdentifier:CellIdentifier];
现在我的自定义单元格被调用了,但是我的单元格看起来不像我期望的那样,有没有机会使用initWithStyle方法?是的,不要使用这个:
UINib*nib=[UINib nibWithNibName:@“GTNewsCustomCell”bundle:nil];[self.tableView注册表项nb:nib forCellReuseIdentifier:CellIdentifier];
你的意思是我应该只删除“GTNewsTableViewController”并设置默认值(NSObject)?是的,仅当您想在更多视图控制器中重用此单元格时。在这种情况下,您甚至不需要这样做。在CellForRowatineXpath中,您直接从nib文件加载单元格。kk谢谢提示:),所以您的意思是我应该只删除”GTNewsTableViewController“并设置默认值(NSObject)?是的,仅当您希望在更多视图控制器中重用此单元格时。在这种情况下,您甚至不需要执行此操作。在cellForRowAtIndexPath中,您直接从nib文件加载单元格。谢谢提示:)