Mar*_*oni 8 subclass storyboard uitableview ios
我已经创建了一个子类,UITableViewCell并且我调用了它DCCustomCell.这是DCCustomCell.h文件.
#import <UIKit/UIKit.h>
@interface DCCustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet UILabel *price;
@end
Run Code Online (Sandbox Code Playgroud)
所有礼节都与我iPhone.storyboard文件中的元素正确连接.我还选择了tableViewCell,iPhone.storyboard并将我的单元格标识符设置为"CustomCell",并将类设置为DCCustomCell.
这是UITableViewController的viewDidLoad方法:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"];
// Do any additional setup after loading the view, typically from a nib.
}
Run Code Online (Sandbox Code Playgroud)
这是tableView:cellForRowAtIndexPath:方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"CustomCell";
DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
NSLog(@"%@" ,cell);
cell.imageView.image = [UIImage imageNamed:@"info_button.png"];
cell.title.text = @"Hello!";
cell.date.text = @"21/12/2013";
cell.price.text = @"€15.00";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
问题是imageView设置正确,但所有标签都是空白的.如果我将imageView属性名称更改为例如eventImageView,则不会设置图像.这是结果:

我无法弄清楚如何解决这个问题.
编辑:如果我删除
[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"];
Run Code Online (Sandbox Code Playgroud)
从viewDidLoad一切似乎工作.为什么?
小智 9
正如您所注意到的那样,当您移除时
[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"];
Run Code Online (Sandbox Code Playgroud)
如果您使用dequeueReusableCellWithIdentifier:indexPath:AND尚未在该单元的标识检查器中设置自定义类,则只需执行此操作.如果您使用registerClass: forCellReuseIdentifier该单元格将被初始化,initWithStyle:reuseIdentifier:而不是initWithCoder:因此搞砸了您在故事板中所做的连接.
尝试:
if (cell == null) {
//create and initialize cell
}
Run Code Online (Sandbox Code Playgroud)
后
DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11596 次 |
| 最近记录: |