Iphone IOS5-自定义UITableViewCell不';不显示内容
我的应用程序遇到了一个问题,尤其是Iphone IOS5-自定义UITableViewCell不';不显示内容,iphone,ios,xcode,uitableview,Iphone,Ios,Xcode,Uitableview,我的应用程序遇到了一个问题,尤其是UITableView及其UITableViewCells。问题是:我在UIView中创建了一个UITableView,使用自定义UITableViewCell。我有两个控制器: ListController.m ListCellController.m 在IB中,我将单元的项目链接到我在单元控制器的头文件中手动创建的IBOutlet 以下是ListCellController.h/m的代码: // .h @class Item; @interface L
UITableView
及其UITableViewCells
。问题是:我在UIView
中创建了一个UITableView
,使用自定义UITableViewCell
。我有两个控制器:
- ListController.m
- ListCellController.m
// .h
@class Item;
@interface ListCellController : UITableViewCell
@property (nonatomic, strong) Item *item;
@property (nonatomic, strong) IBOutlet UILabel *itemName;
@property (nonatomic, strong) IBOutlet UILabel *dates;
@property (nonatomic, strong) IBOutlet UILabel *itemId;
@end
// .m
// @synthetize stuff
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (!self) {
return nil;
}
self.textLabel.adjustsFontSizeToFitWidth = YES;
self.selectionStyle = UITableViewCellSelectionStyleGray;
return self;
}
- (void)setItem:(Item *)item
{
_item = item;
self.itemName.text = _list.itemName;
self.itemId.text = _list.itemId;
self.dates.text = [NSString stringWithFormat:@"Du %@ au %@", _list.date, _list.date];
}
// .h
@interface ListController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *cellList;
}
@property (strong, nonatomic) IBOutlet UITableView *cellList;
@end
// .m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"List cell";
ListCellController *cell = [cellList dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[ListCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.item = [_items objectAtIndex:indexPath.row];
cell.itemId.text = cell.item.itemId;
cell.itemName.text = cell.item.itemName;
cell.dates.text = [NSString stringWithFormat:@"From %@ to %@", cell.item.date, cell.item.date];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[cellList deselectRowAtIndexPath:indexPath animated:YES];
}
下面是ListController.h/m的代码:
// .h
@class Item;
@interface ListCellController : UITableViewCell
@property (nonatomic, strong) Item *item;
@property (nonatomic, strong) IBOutlet UILabel *itemName;
@property (nonatomic, strong) IBOutlet UILabel *dates;
@property (nonatomic, strong) IBOutlet UILabel *itemId;
@end
// .m
// @synthetize stuff
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (!self) {
return nil;
}
self.textLabel.adjustsFontSizeToFitWidth = YES;
self.selectionStyle = UITableViewCellSelectionStyleGray;
return self;
}
- (void)setItem:(Item *)item
{
_item = item;
self.itemName.text = _list.itemName;
self.itemId.text = _list.itemId;
self.dates.text = [NSString stringWithFormat:@"Du %@ au %@", _list.date, _list.date];
}
// .h
@interface ListController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *cellList;
}
@property (strong, nonatomic) IBOutlet UITableView *cellList;
@end
// .m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"List cell";
ListCellController *cell = [cellList dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[ListCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.item = [_items objectAtIndex:indexPath.row];
cell.itemId.text = cell.item.itemId;
cell.itemName.text = cell.item.itemName;
cell.dates.text = [NSString stringWithFormat:@"From %@ to %@", cell.item.date, cell.item.date];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[cellList deselectRowAtIndexPath:indexPath animated:YES];
}
/.h
@接口ListController:UIViewController{
UITableView*手机列表;
}
@属性(强,非原子)IBUITableView*单元格列表;
@结束
//m
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[_列表计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“列表单元格”;
ListCellController*cell=[cellList dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!单元格){
cell=[[ListCellController alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.item=[\u items objectAtIndex:indexath.row];
cell.itemId.text=cell.item.itemId;
cell.itemName.text=cell.item.itemName;
cell.dates.text=[NSString stringWithFormat:@“从%@到%@”,cell.item.date,cell.item.date];
返回单元;
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
[cellList取消RowAtIndexPath:indexPath:YES];
}
我的问题是,当我启动应用程序时,我在手机上什么也看不到。无内容、无错误、无内容、空白单元格
我也在这里使用故事板
有没有人遇到过这个问题?我怎样才能解决它呢
谢谢。如果您在UITableView上有自定义单元格,您必须执行以下操作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"List cell";
ListCellController *cell = (ListCellController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ListCellController" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ListCellController *) currentObject;
break;
}
}
}
cell.item = [_items objectAtIndex:indexPath.row];
cell.itemId.text = cell.item.segmentCode;
cell.itemName.text = cell.item.hotelName;
cell.dates.text = [NSString stringWithFormat:@"Du %@ au %@", cell.item.checkin, cell.item.checkout];
return cell;
}
如果UITableView上有自定义单元格,则必须执行以下操作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"List cell";
ListCellController *cell = (ListCellController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ListCellController" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ListCellController *) currentObject;
break;
}
}
}
cell.item = [_items objectAtIndex:indexPath.row];
cell.itemId.text = cell.item.segmentCode;
cell.itemName.text = cell.item.hotelName;
cell.dates.text = [NSString stringWithFormat:@"Du %@ au %@", cell.item.checkin, cell.item.checkout];
return cell;
}
您是否在IB中链接了委托和数据源?我没有看到您在代码中设置委托。我只在IB中设置了数据源。似乎我没有对委托做任何事情。但我对IOS开发还很陌生,我真的不知道在这种情况下应该链接什么…无论哪个头类包含tableView Outlet,都需要委托集,与数据源的方式相同。它也可能与用作数据源的阵列相关。何时/何地填写
\u列表
我在viewDidLoad函数中调用该函数来填写列表。当我设置委托和数据源时,它没有改变。你在IB中链接委托和数据源了吗?我没有看到您在代码中设置委托。我只在IB中设置了数据源。似乎我没有对委托做任何事情。但我对IOS开发还很陌生,我真的不知道在这种情况下应该链接什么…无论哪个头类包含tableView Outlet,都需要委托集,与数据源的方式相同。它也可能与用作数据源的阵列相关。何时/何地填写\u列表
我在viewDidLoad函数中调用该函数来填写列表。当我设置委托和数据源时,它不做任何更改。@tsabz-您正在使用故事板吗?你必须在问题中提到这一点,因为如果你是,出列将始终返回一个单元格,并且所有单元格都将使用initWithCoder进行初始化。是的,我正在使用故事板,我将在初始帖子中指定它。我已经用这个anwser解决了我的问题。我刚刚创建了一个新的.xib文件并粘贴了上面的代码。现在一切都很顺利。谢谢@tsabz-你在用故事板吗?你必须在问题中提到这一点,因为如果你是,出列将始终返回一个单元格,并且所有单元格都将使用initWithCoder进行初始化。是的,我正在使用故事板,我将在初始帖子中指定它。我已经用这个anwser解决了我的问题。我刚刚创建了一个新的.xib文件并粘贴了上面的代码。现在一切都很顺利。谢谢