7

我在使用UITableViewCell.

我的故事板中有一个视图控制器,它连接到我的视图控制器,名为MainViewController. 它包含一个UITableViewCell带有 3 个标签的标签。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 条空白记录的空表视图。

4

10 回答 10

2

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

于 2013-11-14T09:45:27.873 回答
2

您没有从主 Bundle 加载单元格:使用下面的代码,这对您有用:

-(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;
}
于 2015-08-06T07:50:38.553 回答
1

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

    - (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;
 }

在此处阅读 PFQueryTableViewController 不显示自定义单元格

问候 :)

于 2013-11-14T09:35:50.210 回答
1

这对我有用..

-(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;
}
于 2016-08-03T02:48:59.183 回答
1

TableViewcell 以这种方式声明:

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

下面一个对我不起作用:

TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
于 2017-01-25T07:39:44.160 回答
0

重写你cellForRowAtIndexPath:的方法如下

 - (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;
    }
于 2013-11-14T09:49:25.883 回答
0
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    TableViewCell *cell = (TableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil)
    {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];
    }
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
于 2016-02-20T08:26:06.160 回答
0

我遇到了同样的问题,我在 tableview 委托和数据源中缺少连接:

self.yourtableviewname.delegate=self;
self.youtableviewname.datasource=self;

之后它工作得很好。

于 2016-04-19T15:14:15.653 回答
0

选择单元格并在检查器选项卡中,选中“已安装”,它将起作用。

于 2016-08-16T11:41:35.107 回答
-1

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

于 2013-11-14T09:38:38.623 回答