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;
        }
    }
}
6
  • TaskCell *cell = (TaskCell*)[tableView dequeueReusableCellWithIdentifier...
    – janusfidel
    Jul 24, 2012 at 6:09
  • Have you set the reuseIdentifier of the UITableViewCell in the xib to "cell"?
    – Pfitz
    Jul 24, 2012 at 7: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, 2012 at 7: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, 2012 at 7:16
  • Have you tried another name for the reuse identifier ? Cell is used by basics cells, it may be conflicted.
    – Oyashiro
    Jul 24, 2012 at 7:20

1 Answer 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;
}
2
  • 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, 2012 at 6:54
  • please refer this ! stackoverflow.com/questions/413993/…
    – manileo86
    Jul 24, 2012 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.