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

时间:2022-09-10 20:04:05

I have written a class for UITableViewCell called TaskCell and designed a .xib file and connected the File's Owner to the custom class. Then in the View Controller class I want to load the custom design, but it is not showing. This is the code for the View Controller with table view:

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

ShowTaskViewController.h file:

ShowTaskViewController.h文件:

@interface ShowTaskViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
    //Database object
    sqlite3 *taskeeDB;
    NSString *databasePath; 
}
@end

ShowTaskViewController.m file with cell for row at index path method:

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

If cell is null from dequeue cell list, I am assigning new cell objects where TaskCell is the custom UITableViewCell class. Am I doing the correct process?

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

Show I try to create the cells as following?

显示我尝试创建如下的单元格?

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

1 个解决方案

#1


1  

I guess the following change would help you!!

我想以下改变会对你有所帮助!!

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

#1


1  

I guess the following change would help you!!

我想以下改变会对你有所帮助!!

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