iOS:DequeTableViewCell..有点困惑

iOS:DequeTableViewCell..有点困惑,ios,tableview,Ios,Tableview,我想更改我的UITableView单元格的字体大小和颜色等。我在Xcode中设计了定制的单元,并使一切正常工作 首先,我将在这里发布我的代码: UITableViewController: - (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:MainCategoryTableViewCell.class forCellReuseIdentifier:@"MainCategoryCell

我想更改我的
UITableView
单元格的字体大小和颜色等。我在
Xcode
中设计了定制的单元,并使一切正常工作

首先,我将在这里发布我的代码:
UITableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView registerClass:MainCategoryTableViewCell.class forCellReuseIdentifier:@"MainCategoryCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MainCategoryCell";
    MainCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    return cell;
}
和我的自定义手机:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.title.font = [Theme tableCellTitleFont];
        self.title.textColor = [Theme tableCellTitleColor];

        self.subcategories.font = [Theme tableCellSubTitleFont];
        self.subcategories.textColor = [Theme tableCellSubTitleColor];

        self.costs.font = [Theme tableCellValueFont];
        self.costs.textColor = [Theme tableCellValueColor];
    }
    return self;
}
我现在搞不清楚这个排队是如何工作的: 据我所知,如果我在
viewDidLoad
中注册该类,那么只有在没有可重用的单元格时才会调用单元格的
initWithStyle
方法。如果有可重复使用的单元,则将使用该单元。我在其他代码段中见过很多if(cell==nil)调用,但这真的有必要吗?我还以为
registerClass
方法会解决这个问题呢

现在我的手机会显示为空的。在我注册这个类之前,一切都正常,但是没有调用
initWithStyle

完成
cellforrowatinexpathMethod

#pragma mark Delegate methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MainCategoryCell";
    MainCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.title.text = mainCategory.name;
    cell.subcategories.text = [NSString stringWithFormat:@"%i subcategories", [[mainCategory getNumberOfSpendingCategories] integerValue]];
    cell.costs.text = [[mainCategory getMonthlyCostsOfAllSpendingCategories] getLocalizedCurrencyString];
    if(!mainCategory.icon){
        cell.icon.image = [UIImage imageNamed:@"DefaultIcon.png"];
    } else {
        cell.icon.image = [UIImage imageNamed:mainCategory.icon];
    }

    if(!mainCategory.color){
        cell.backgroundColor = [PresetColor colorForPresetColor:PresetColorsWhite];
    } else {
        cell.backgroundColor = [PresetColor colorForPresetColor:(PresetColors)[mainCategory.color intValue]];
    }

    cell.cellBackground.image = [[UIImage imageNamed:@"content-bkg"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];

    return cell;
}

它在文档中清楚地说明了这一点

dequeueReusableCellWithIdentifier:forIndexPath:

出于性能原因,表视图的数据源通常应 当UITableViewCell对象将单元格指定给其 tableView:cellForRowAtIndexPath:method。表视图维护一个 数据源具有的UITableViewCell对象的队列或列表 标记为可重复使用。在以下情况下从数据源对象调用此方法: 要求为表视图提供新单元格。此方法将退出队列 现有单元格(如果可用)或基于 类或以前注册的nib文件

dequeueReusableCellWithIdentifier:

返回值:具有关联标识符的UITableViewCell对象 或者,如果可重用单元队列中不存在此类对象,则为nil

讨论:出于性能原因,表视图的数据源 通常应在分配单元格时重用UITableViewCell对象 到其tableView:cellForRowAtIndexPath:method中的行。表视图 维护包含数据的UITableViewCell对象的队列或列表 源已标记为可重用。从数据源调用此方法 当要求为表视图提供新单元格时。这 方法将现有单元格(如果可用)出列或创建新单元格 使用您以前注册的类或nib文件的。如果没有细胞 可供重用,并且您没有注册类或nib文件, 此方法返回nil

如果为指定的标识符注册了一个类和一个新单元 必须创建,此方法通过调用其 initWithStyle:reuseIdentifier:method。对于基于nib的单元,此 方法从提供的nib文件加载单元格对象。如果 现有单元格可供重用,此方法调用单元格的 改为使用preparefore方法


在引入故事板之前,tableview会检查返回的单元格,该单元格可以为nil。因此,如果为nil,我们必须重新分配该单元格,然后使用datasource方法初始化并提供该单元格,该方法在文档中有明确说明

dequeueReusableCellWithIdentifier:forIndexPath:

出于性能原因,表视图的数据源通常应 当UITableViewCell对象将单元格指定给其 tableView:cellForRowAtIndexPath:method。表视图维护一个 数据源具有的UITableViewCell对象的队列或列表 标记为可重复使用。在以下情况下从数据源对象调用此方法: 要求为表视图提供新单元格。此方法将退出队列 现有单元格(如果可用)或基于 类或以前注册的nib文件

dequeueReusableCellWithIdentifier:

返回值:具有关联标识符的UITableViewCell对象 或者,如果可重用单元队列中不存在此类对象,则为nil

讨论:出于性能原因,表视图的数据源 通常应在分配单元格时重用UITableViewCell对象 到其tableView:cellForRowAtIndexPath:method中的行。表视图 维护包含数据的UITableViewCell对象的队列或列表 源已标记为可重用。从数据源调用此方法 当要求为表视图提供新单元格时。这 方法将现有单元格(如果可用)出列或创建新单元格 使用您以前注册的类或nib文件的。如果没有细胞 可供重用,并且您没有注册类或nib文件, 此方法返回nil

如果为指定的标识符注册了一个类和一个新单元 必须创建,此方法通过调用其 initWithStyle:reuseIdentifier:method。对于基于nib的单元,此 方法从提供的nib文件加载单元格对象。如果 现有单元格可供重用,此方法调用单元格的 改为使用preparefore方法


在引入序列图像板之前,tableview会检查返回的单元格,该单元格可以为nil。因此,如果为nil,我们必须重新分配该单元格,并在datasource方法中初始化并提供该单元格

如果您在xib/序列图像板文件中将该单元格定义为表视图的“原型单元格”,那么您根本不必注册它。如果自定义单元格位于单独的nib文件中,则可以使用
registerNib
而不是
registerClass
注册自定义单元格。例如:

[self.tableView registerNib:[UINib nibWithNibName:@"MainCategoryTableViewCell" bundle:nil]
      forCellReuseIdentifier:@"MainCategoryCell"];
对于从nib文件实例化的单元格,将调用
initWithCoder
,而不是
initWithStyle

要配置自定义单元格的任何出口,请覆盖
awakeFromNib
。连接是
尚未在
initWithCoder

中建立如果已在xib/storyboard文件中将单元格定义为表视图的“原型单元格”,则根本不必注册它。如果自定义单元格位于单独的nib文件中,则使用
registerN注册自定义单元格