【发布时间】:2013-11-14 09:07:04
【问题描述】:
我在使用 UITableViewCell 时遇到问题。
我的故事板中有一个视图控制器,它连接到我的视图控制器,名为MainViewController。它包含一个带有 3 个标签的 UITableViewCell。 UITableViewCell 连接到类 MTTableViewCell。
// MTTableViewCell.h
#import <UIKit/UIKit.h>
@interface MTTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *mainLabel;
@property (strong, nonatomic) IBOutlet UILabel *statusLabel;
@end
// MTTableViewCell.m
#import "MTTableViewCell.h"
@implementation MTTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
// MainViewController.m
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *namecell = @"namecell";
NSString *statuscell = @"statuscell";
[cell.mainLabel setText:namecell];
[cell.statusLabel setText:statuscell];
return cell;
}
问题是当我运行MainViewController 时没有显示任何内容。我错过了什么?我没有得到任何错误,只是一个带有 1 条空白记录的空表视图。
【问题讨论】:
-
您是否正确设置了网点?
-
确保调用了 cellForRowAtIndexPath。也许您错过了一些渠道或代表
-
确保在情节提要中连接表“委托”属性。您还必须将一些数据放入表中。您必须指定每个部分的部分数和行数。
-
网点设置正确。调用函数 cellForRowAtIndexPath。表 'delegate' 属性当前连接到 MainViewController 文件的所有者
-
这个关于自定义表格单元的参考比苹果的要好得多。 apeth.com/iOSBook/ch21.html#_custom_cells 另外,如果您对为什么视图和子视图层次结构未在 UITableView 或 UICollection 视图中呈现而感到头疼,那么调试这些问题的一种好方法是从 Xcode 中删除受影响的 .xib 文件,只删除引用,然后添加他们回来了。通常在狙击和切割 IBOutlets 时,即使没有显示错误,笔尖也会因引用损坏而进入不良状态。 Interface Builder 会很高兴地显示并且什么也不说。 Xcode 的 8 大奇迹之一!
标签: ios objective-c uitableview