【问题标题】:UITableViewCellStyle Custom iOS 7UITableViewCellStyle 自定义 iOS 7
【发布时间】:2014-06-26 22:22:39
【问题描述】:

我对 UITableViewCellStyle 有点困惑。我想像这样创建一个自定义 UITableViewCell:

但是当我运行应用程序时,文本和图像没有出现在单元格中。在 Storyboard 中,我将 TableView 样式设置为“自定义”。

我做错了什么?

MainTableViewController

#import "MainTableViewController.h"
#import "CustomMainCell.h"

static NSString *CellIdentifier = @"MainCell";

@interface MainTableViewController ()

//
@property(nonatomic, strong) NSArray *dataSource;

//
@property(nonatomic, strong) NSArray *iconsSource;

@end

@implementation MainTableViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Currículum Vitae";

    // Inicializo los arrays con los datos
    self.dataSource = @[@"PERFIL",@"EXPERIENCIA",@"EDUCACIÓN",@"HABILIDADES",@"INTERESES"];
    self.iconsSource = @[@"perfil.png",@"experiencia.png",@"educacion.png",@"habilidades.png",@"intereses"];

    // Register Class for Cell Reuse Identifier
    [self.tableView registerClass:[CustomMainCell class] forCellReuseIdentifier:CellIdentifier];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    // Eliminio las líneas que separan las celdas
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomMainCell *cell = (CustomMainCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[CustomMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.title.text = self.dataSource[indexPath.row];
    cell.icon.image = self.iconsSource[indexPath.row];

    return cell;
}

CustomMainCell.h

@interface CustomMainCell : UITableViewCell

//
@property (weak, nonatomic) IBOutlet UILabel *title;

//
@property (weak, nonatomic) IBOutlet UIImageView *icon;

@end

CustomMainCell.m

#import "CustomMainCell.h"

@implementation CustomMainCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.textLabel.textColor = [UIColor brownColor];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

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

    // Configure the view for the selected state
}

@end

【问题讨论】:

    标签: ios uitableview ios7


    【解决方案1】:

    你不应该有:

    // Register Class for Cell Reuse Identifier
    [self.tableView registerClass:[CustomMainCell class] forCellReuseIdentifier:CellIdentifier];
    

    因为当视图控制器未归档时,单元格是从情节提要中注册的。通过注册课程,您将删除存档 (NIB) 注册。

    另外:

    if (cell == nil) {
        cell = [[CustomMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    

    不应该是必需的,因为您总是会得到一个有效的单元格(因为标识符已注册)

    【讨论】:

    • 好的,它可以工作,但是如果我删除这些代码行,CustomMainCell.h 中的 (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 方法将不会是执行。哪个是修改单元格外观的最佳位置?在 MainTableViewController 中还是在 CustomMainCell 中?
    • 在情节提要中... 将调用带有编码器方法和从 nib 方法唤醒的 init。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2013-09-20
    • 2014-09-08
    • 2014-03-11
    • 2013-10-11
    相关资源
    最近更新 更多