user594161
user594161

Reputation:

How can I use a custom UITableViewCell

I have a UITableView and I want some cells to be the standard UITableViewCells and I have one custom cell, called ImageTableViewCell. I have set this custom cell in IB and have linked with its header and implementation.

The cell loads fine into my view along with the standard cells, but my data is not being put into the cell at runtime. Here is the code I am using. Do you notice anything wrong?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = nil;
    if (tableView == self.visitTableView && indexPath.section == 0 && indexPath.row == 0){
        CellIdentifier = @"imageCell";
    }
    else{
        CellIdentifier = @"cell";
        }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    ImageTableViewCell *imageCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (tableView == self.visitTableView)
    {
        if (indexPath.section == 0)
        {
            switch (indexPath.row) {
                case 0:
                    imageCell.patientNameLabel = [self.appointmentDictionary objectForKey:@"patient"];
                    [imageCell.patientImage setImageWithURL:[NSURL URLWithString:[self.appointmentDictionary objectForKey:@"patient_small_photo_url"]] placeholderImage:nil];
                    break;
                case 1:
                    cell.textLabel.text = @"Appointment";
                    cell.detailTextLabel.text = [self.appointmentDictionary objectForKey:@"scheduled_time"];
                    break;                
                default:
                    break;
                }
        }

Upvotes: 1

Views: 1682

Answers (1)

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

Well, your last comment is the problem. You dequeue two cells, and then configure the second and return the first.

To get a pointer to ImageTableViewCell, just cast the UITableViewCell pointer, like so:

ImageTableViewCell *imageCell = (ImageTableViewCell*)cell;

And also remove this:

 ImageTableViewCell *imageCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

And then you can go ahead and return 'cell' for either case.

Upvotes: 2

Related Questions