iOS 帮助:UITableViewCell 类的自定义设计未显示在 UITableView 中

Posted

技术标签:

【中文标题】iOS 帮助:UITableViewCell 类的自定义设计未显示在 UITableView 中【英文标题】:iOS Help: Custom design for UITableViewCell class is not showing up in UITableView 【发布时间】:2012-07-24 06:05:00 【问题描述】:

我为 UITableViewCell 编写了一个名为 TaskCell 的类,并设计了一个 .xib 文件并将 File's Owner 连接到自定义类。然后在 View Controller 类中我想加载自定义设计,但它没有显示。这是带有表格视图的视图控制器的代码:

ShowTaskViewController.h 文件:

@interface ShowTaskViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

    //Database object
    sqlite3 *taskeeDB;
    NSString *databasePath; 

@end

ShowTaskViewController.m 文件,在索引路径方法中包含行单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //Get data for cell
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    Task *taskObj1 = (Task *)[appDelegate.taskArray objectAtIndex:indexPath.row];
    NSString *sName = [[NSString alloc] initWithFormat:@"%@",taskObj1.name];
    NSString *sLoc = [[NSString alloc] initWithFormat:@"%@",taskObj1.location];
    NSString *sTime = [[NSString alloc] initWithFormat:@"%@",taskObj1.time];
    NSString *sDate = [[NSString alloc] initWithFormat:@"%@",taskObj1.date];

    //Configure cell if null
    if (cell == nil) 
        //I have written a custom init method to take parameters and assign label text values
        cell = [[TaskCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier name:sName location:sLoc time:sTime date:sDate];
    
return cell;

如果出列单元格列表中的单元格为空,我将分配新的单元格对象,其中 TaskCell 是自定义 UITableViewCell 类。我在做正确的过程吗?

显示我尝试按以下方式创建单元格?

if (cell == nil)
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"TaskCell" owner:self options:nil];
    for (id currentObjects in objects  )  
        if ([currentObjects isKindOfClass:[UITableViewCell class]]) 
            cell = (TaskCell*) currentObjects;
            break;
        
    

【问题讨论】:

TaskCell *cell = (TaskCell*)[tableView dequeueReusableCellWithIdentifier... 你是否将xib中UITableViewCellreuseIdentifier设置为“单元格”? 您在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中使用单元格标识符“Cell”。如果您创建了自定义单元格,请给它一个自定义名称。 我已在 .xib 文件中将自定义类设置为 TaskCell,并将重用标识符设置为“Cell”。顺便说一句,单元格的基本视图有效,如果我设置文本和副标题,它可以工作,但是当我尝试加载自定义设计时我会空白单元格。 您是否为重用标识符尝试了另一个名称?单元格被基本单元格使用,可能会发生冲突。 【参考方案1】:

我想下面的改动会对你有所帮助!!

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    static NSString *CellIdentifier = @"Cell"; 
    TaskCell *cell = (TaskCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //Get data for cell
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    Task *taskObj1 = (Task *)[appDelegate.taskArray objectAtIndex:indexPath.row];
    NSString *sName = [[NSString alloc] initWithFormat:@"%@",taskObj1.name];
    NSString *sLoc = [[NSString alloc] initWithFormat:@"%@",taskObj1.location];
    NSString *sTime = [[NSString alloc] initWithFormat:@"%@",taskObj1.time];
    NSString *sDate = [[NSString alloc] initWithFormat:@"%@",taskObj1.date];

    //Configure cell if null
    if (cell == nil) 
        //I have written a custom init method to take parameters and assign label text values
        cell = [[TaskCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier name:sName location:sLoc time:sTime date:sDate];
    
return cell;

【讨论】:

不幸的是,它没有帮助,仍然显示空白行,但我正在使用 NSLog 并显示不同的字符串,并且每次显示不同的值。你认为我应该做出改变并尝试一下吗? if (cell == nil) NSArray objects = [[NSBundle mainBundle] loadNibNamed:@"TaskCell" owner:self options:nil]; for (id currentObjects in objects ) if ([currentObjects isKindOfClass:[UITableViewCell class]]) cell = (TaskCell) currentObjects;休息; 请参考! ***.com/questions/413993/…

以上是关于iOS 帮助:UITableViewCell 类的自定义设计未显示在 UITableView 中的主要内容,如果未能解决你的问题,请参考以下文章

在自定义 UITableViewCell 上使用图像时 IOS 8 崩溃

在ios中从uitableviewcell拖放到uiimage视图

表视图边界外的 UITableViewCell

iOS UITableViewCell的"滑动出现多个按钮"

如何在ios中的自定义UITableViewCell中添加轮播视图

UIButton 没有响应 UITableViewCell 中的单击事件