0

I have created a custom cell that contain a Label but when I added that in my Table View than it does not display, my view controller is not TableViewController I have seted Table View using IB and seted its delegate and datasource correctly.

I am doing this by:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ExampleAppDataObject* theDataObject = [self theAppDataObject];
    return theDataObject.myAudio.count;
}


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

        static NSString *CellIdentifier = @"Cell";

        ExampleAppDataObject* theDataObject = [self theAppDataObject];

        NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];

        NSArray *parts = [destinationPath componentsSeparatedByString:@"/"];

        NSString *filename = [parts lastObject];

        AudioInfoCell *cell = (AudioInfoCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        }

        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        cell.audioName.text = filename;

        return cell;

    }
David Gray
  • 307
  • 3
  • 11

2 Answers2

0

First check how many row set in tableView:numberOfRowsInSection method

After set the custom cell like bellow..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AudioInfoCell *cell = (AudioInfoCell *) [tableView dequeueReusableCellWithIdentifier:nil];


    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AudioInfoCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (AudioInfoCell *) currentObject;
                break;

            }
        }
    }

    ExampleAppDataObject* theDataObject = [self theAppDataObject];

    NSString *destinationPath = [theDataObject.myAudio objectAtIndex:indexPath.row];

    NSArray *parts = [destinationPath componentsSeparatedByString:@"/"];

    NSString *filename = [parts lastObject];

    cell.audioName.text = filename;
    return cell;
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Why are you going through all that work? That for-in loop will get run for every cell that is displayed, and will get run every time the user scrolls the TV. – LJ Wilson Dec 13 '12 at 13:31
  • it just alloc when cell is nil dude .. i use custem cell with this flow of code dude.. – Paras Joshi Dec 13 '12 at 13:33
  • this give me error "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key audioName.'" – David Gray Dec 13 '12 at 13:38
  • AudioTableViewController is not UITableViewController i named it like this. – David Gray Dec 13 '12 at 13:39
0

Two things to check:

  1. Make sure the reuse identifier matches what is in Interface Builder
  2. Make sure your numberOfRowsInSection and numberOfSectionsInTableView return the right numbers

Also - I don't use:

if (cell == nil) {
        cell = [[AudioInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}

for instantiating the cell. That should be taken care of in the AudioCell TableViewCell subclass you have implemented. An example of how I implement this kind of custom cell is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"FlightPickerCell";
    FlightPickerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.tripIDLabel.text = @"Some TripID";
    cell.hospitalLabel.text = @"Some Hospital";
    cell.cityLabel.text = @"Some City";
    return cell;
}
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62