- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 UITableViewCellStyle 有点困惑。我想像这样创建一个自定义 UITableViewCell:
但是当我运行应用程序时,文本和图像没有出现在单元格中。在 Storyboard 中,我将 TableView 样式设置为“自定义”。
我做错了什么?
主 TableView Controller
#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
最佳答案
你不应该:
// Register Class for Cell Reuse Identifier
[self.tableView registerClass:[CustomMainCell class] forCellReuseIdentifier:CellIdentifier];
因为当 View Controller 未归档时,单元格是从 Storyboard 中注册的。通过注册类(class),您将删除存档 (NIB) 注册。
此外:
if (cell == nil) {
cell = [[CustomMainCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
不应该是必需的,因为您总是会得到一个有效的单元格(因为标识符已注册)
关于ios - UITableViewCellStyle 自定义 iOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24432995/
刚刚发生了一件奇怪的事情,我试图构建我的自定义表格单元格,但没有调用我的 initWithStyle。 - (id)initWithStyle:(UITableViewCellStyle)style
我正在使用Xamarin iOS,在iOS 9.2设备上,具有UITableViewCellStyle.Value1单元格样式的UITableView单元格具有与DetailTextLabel重叠的T
我对 UITableViewCellStyle 有点困惑。我想像这样创建一个自定义 UITableViewCell: 但是当我运行应用程序时,文本和图像没有出现在单元格中。在 Storyboard 中
我知道我可以将类或 NIB 文件注册为 UITableViewCell。但我想做 iOS 6 的方式 UITableViewCell *cell = [tableView dequeueReusabl
根据 this Xamarin docs page ,我正在 Xamarin iOS 应用程序中构建 TableView : 自定义 UITableViewSource 类,覆盖 GetCell()、
我想为默认表格单元格使用 UITableViewCellStyle.Subtitle 样式。我在 an SO answer 中找到了答案像这样: func tableView(tableView: U
我有一个tableViewCell let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
使用 iOS 11,我可以成功获得自定尺寸 UITableViewCellStyle.subtitle单元格通过设置 numberOfLines 动态调整其高度detailTextLabel 上的属性
我是一名优秀的程序员,十分优秀!