1

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:

ShowTaskViewController.h file:

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

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

- (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?

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;
        }
    }
}
Shahnawaz
  • 646
  • 1
  • 11
  • 25
  • `TaskCell *cell = (TaskCell*)[tableView dequeueReusableCellWithIdentifier...` – janusfidel Jul 24 '12 at 06:09
  • Have you set the `reuseIdentifier` of the `UITableViewCell` in the xib to "cell"? – Pfitz Jul 24 '12 at 07:00
  • You use the cell identifier "Cell" in `(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath`. If you created a custom cell, give it a custom name. – Oyashiro Jul 24 '12 at 07:08
  • 1
    I have set the custom class to TaskCell in the .xib file and also I have set the reuseIdentifier to "Cell". Btw, the basic view of the cell works, If I set text and subtitle it works but I blank cells when I try to load custom designs. – Shahnawaz Jul 24 '12 at 07:16
  • Have you tried another name for the reuse identifier ? Cell is used by basics cells, it may be conflicted. – Oyashiro Jul 24 '12 at 07:20
  • Okay I renamed reuse identifier to "CustomCell" and I used this bit of code: 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; } } } But it throws an exception saying: 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key labelDate.' – Shahnawaz Jul 24 '12 at 08:04

1 Answers1

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;
}
manileo86
  • 153
  • 7
  • 1
    Its not helping unfortunately, still showing blank rows but I am using NSLog and displaying the different strings and each time different values are displayed. Do you think I should make this change and try it out? 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; } } } – Shahnawaz Jul 24 '12 at 06:54
  • please refer this ! http://stackoverflow.com/questions/413993/loading-a-reusable-uitableviewcell-from-a-nib – manileo86 Jul 24 '12 at 07:18