Ios UITableViewCell中的内容未显示

Ios UITableViewCell中的内容未显示,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在使用UITableViewCell时遇到问题 // MTTableViewCell.h #import <UIKit/UIKit.h> @interface MTTableViewCell : UITableViewCell @property (strong, nonatomic) IBOutlet UILabel *mainLabel; @property (strong, nonatomic) IBOutlet UILabel *statusLabel; @end

我在使用
UITableViewCell
时遇到问题

// 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
的视图控制器。它包含一个带有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;
}
//MTTableViewCell.h
#进口
@接口MTTableViewCell:UITableViewCell
@属性(强,非原子)IBUILabel*mainLabel;
@属性(强,非原子)IBUILabel*statusLabel;
@结束
//MTTableViewCell.m
#导入“MTTableViewCell.h”
@可视单元的实现
-(id)initWithStyle:(UITableViewCellStyle)样式重用标识符:(NSString*)重用标识符{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我){
//初始化代码
}
回归自我;
}
-(无效)设置选定:(BOOL)选定动画:(BOOL)动画{
[超级设置选定:选定动画:动画];
//为所选状态配置视图
}
@结束
//MainViewController.m
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
MTTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[MTTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}        
NSString*namecell=@“namecell”;
NSString*statuscell=@“statuscell”;
[cell.mainLabel setText:namecell];
[cell.statusLabel setText:statuscell];
返回单元;
}

问题是,当我运行
MainViewController
时,没有显示任何内容。我错过了什么?我没有收到任何arrors,只是一个空的tableview和一条空白记录。

试试看这里。。。你问题的答案可能是

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{

     CustomCell *cell = (CustomCell * )[self.tableView dequeueReusableCellWithIdentifier:@"YOUR CELL NAME" forIndexPath:indexPath];

   // your content cell...


     return cell;
 }
在这里读


您好:)

您没有实例化mainLabel和statusLabel,因此它们现在为零。您必须在initWithStyle方法中实例化它们。

查看您的界面生成器-您应该没有“连接”自定义单元格,您应该将其设置为“文件所有者”,因为您实现了视图,而不想设置委托。也许这个提示有帮助

将您的
cellforrowatinexpath:
方法重写如下

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CUSTOME_CELL_Nib_NAME" owner:nil options:nil] objectAtIndex: 0];;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }        

        NSString *namecell = @"namecell";
        NSString *statuscell = @"statuscell";           
        [cell.mainLabel setText:namecell];
        [cell.statusLabel setText:statuscell];        

        return cell;
    }

您没有从主捆绑中加载单元格: 使用以下代码这将对您有效:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"FleetAccountCell";

FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"FleetAccountCell" owner:nil options:nil] objectAtIndex: 0];;
}
    return cell;
}

我也有同样的问题,我在tableview委托和数据源中缺少一个连接:

self.yourtableviewname.delegate=self;
self.youtableviewname.datasource=self;
在那之后它工作得很好。

这项工作对我来说

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"cellID";

FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"Your_nibName" owner:nil options:nil] objectAtIndex: 0];
}
[cell layoutIfNeeded];
return cell;
}

选择单元格,然后在inspector选项卡中,选中“installed”(已安装),它将工作。

声明
TableViewcell

TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
下面的一个不适合我:

TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

您是否正确设置了插座?请确保调用了CellForRowatineXpath。可能您错过了一些出口或委派请确保连接故事板中的表“委派”属性。此外,您还必须将一些数据放入表中。您必须指定节数和每个节中的行数。出口设置正确。调用函数cellforrowatinexpath。表“delegate”属性当前已连接到MainViewController文件的所有者。自定义表单元格上的引用比Apple的要好得多。此外,如果您对视图和子视图层次结构没有在UITableView或UICollection视图中呈现的原因感到头痛,那么调试这些问题的一个好方法是从Xcode中删除受影响的.xib文件,只删除引用,然后再将其添加回。通常,在剪切IBOutlet时,即使没有显示错误,NIB也会进入错误状态,引用被破坏。界面建设者会很高兴地显示,什么也不说。Xcode八大奇迹之一。!自定义单元格位于我的故事板中。我以为nib名称只在.xib文件中使用?对不起@Arnout,我怎么能错过这个?我想你可以为自定义单元格创建一个nib,并在你的故事板中使用它,不是吗?请检查你的故事板中的自定义单元格标识符,你有没有按照你的代码给出当前的(“单元格”)?你是什么意思?因此,我不需要将代理出口连接到MainViewController?对于“MTTableViewCell”,您需要将其设置为自定义类的文件所有者。查看文件检查器中的实用程序区域,并尝试在选中图形单元格时将“自定义类”设置为“MTTableViewCell”。我解决了这个问题。我使用的是interface MainViewController:UIViewController,而它本应该只是interface MainViewController:UIViewController@Arno,但上面的方法不太可能修复它。原因可能是设计器中的Restoration ID属性与可重用单元标识符不匹配。mainLabel和statusLabel显然已在视图中-他使用Outlet连接了它。所以他不需要隐式初始化它们?