Muhammad Umar
Muhammad Umar

Reputation: 11782

UITableViewCell not displaying subViews

I am using following code to show Custom UITableViewCell. However the problem is it is showing only backImage and not displaying any sort of other lables or images.

Here is the code and ss.

What is wrong???

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView *maleIcon;
    UIImageView *femaleIcon;
    UIImageView *backImage;

    UILabel *name;
    UILabel *address;
    UILabel *maleCount;
    UILabel *femaleCount;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    VenueClass *venue = [self.venueArray objectAtIndex:[indexPath row]];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];
        backImage =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

        maleIcon   =  [[UIImageView alloc] initWithFrame:CGRectMake(234,143,25,25)];
        femaleIcon =  [[UIImageView alloc] initWithFrame:CGRectMake(234,173,25,25)];

        maleIcon.image    =   [UIImage imageNamed:@"rectangle.png"];
        femaleIcon.image  =   [UIImage imageNamed:@"rectangle.png"];

        name        = [[UILabel alloc] initWithFrame:CGRectMake(10,148,140,22)];
        address     = [[UILabel alloc] initWithFrame:CGRectMake(10,178,140,22)];
        maleCount   = [[UILabel alloc] initWithFrame:CGRectMake(266,145,48,22)];
        femaleCount = [[UILabel alloc] initWithFrame:CGRectMake(266,175,48,22)];

        name.font        = [UIFont boldSystemFontOfSize:16];
        address.font     = [UIFont systemFontOfSize:14];
        maleCount.font   = [UIFont boldSystemFontOfSize:14];
        femaleCount.font = [UIFont boldSystemFontOfSize:14];

        name.backgroundColor        = [UIColor clearColor];
        address.backgroundColor     = [UIColor clearColor];
        maleCount.backgroundColor   = [UIColor clearColor];
        femaleCount.backgroundColor = [UIColor clearColor];

        name.textColor        = [UIColor whiteColor];
        address.textColor     = [UIColor whiteColor];
        maleCount.textColor   = [UIColor whiteColor];
        femaleCount.textColor = [UIColor whiteColor];

        [cell.contentView addSubview:backImage];
        [cell.contentView addSubview:name];
        [cell.contentView addSubview:address];
        [cell.contentView addSubview:maleCount];
        [cell.contentView addSubview:femaleCount];
        [cell.contentView addSubview:femaleIcon];
        [cell.contentView addSubview:maleIcon];

    }

    CLLocation *location = [[CLLocation alloc] initWithLatitude:[venue.latitude doubleValue] longitude:[venue.longitude doubleValue]];

    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    int distance = [location distanceFromLocation:appDelegate.myLocationCtrl.locationManager.location];
    distance = distance / 1000;

    [femaleCount setText:venue.femaleCount];
    [maleCount   setText:venue.maleCount];
    [name        setText:venue.name];
    [address     setText:[NSString stringWithFormat:@"%@/%d Km", venue.address, distance]];

    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor clearColor];


    NSString *path = [[NSString stringWithFormat:@"%@%@" ,IMAGE_URL, venue.imagePath ] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [backImage setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@""]
                  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
     {         
     }];

    return cell;
}

enter image description here

Upvotes: 0

Views: 1524

Answers (2)

Evan Layman
Evan Layman

Reputation: 3721

Yes your back image is way too wide and tall you set it to 320x100 try 60x100

Sorry edit: your other items are off the screen/cell because they are Y coordinate >100. Tr reducing the second argument to your initWithFrame:CGRectMake(X, Y must be less than cell height, width, height)

Upvotes: 1

ryan cumley
ryan cumley

Reputation: 1931

Have you registered a class for the identifier MyIdentifier previously in your code?

If so, then cell will never be nil as the dequeueReusableCellWithIdentifier: method will always return a cell. From the docs:

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

that means that your code will never run the setup that you're performing within your if(cell == nil){} statement

If you did not register a Class or Nib for that reuse identifier, the app should be crashing with an exception, so I suspect that what you are seeing in your app is whatever the default nib or class you registered looks like.

A common approach would be to configure all of these child labels and imageView's within your custom UITableViewCell class or your nib, and then assign relevant values to those properties within this tableView:cellForRowAtIndexPath: method.

Upvotes: 0

Related Questions